Skip to content

Commit

Permalink
add error handling in exec command (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol1696 committed Jul 26, 2024
1 parent 92136d3 commit 1e8f248
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions clients/js/packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export interface PodStatus {
reason?: string;
}

export interface ExecOptions {
log?: boolean;
silent?: boolean;
ignoreError?: boolean;
}

const defaultExecOptions: ExecOptions = {
log: true,
silent: false,
ignoreError: true,
}

export const formatChainID = (input: string): string => {
// Replace underscores with hyphens
let formattedName = input.replace(/_/g, '-');
Expand Down Expand Up @@ -122,11 +134,20 @@ export class StarshipClient implements StarshipClientI {
this.version = readAndParsePackageJson().version;
}

private exec(cmd: string[], log: boolean = true, silent: boolean = false): shell.ShellString {
private exec(cmd: string[], options: Partial<ExecOptions> = {}): shell.ShellString {
const opts = {...defaultExecOptions, ...options};
this.checkDependencies();
const str = cmd.join(' ');
if (log) this.log(str);
return shell.exec(str, { silent });
if (opts.log) this.log(str);

const result = shell.exec(str, { silent: opts.silent });

if (result.code !== 0 && !opts.ignoreError) {
this.log(chalk.red('Error: ') + result.stderr);
this.exit(result.code);
}

return result;
}

private log(str: string): void {
Expand Down Expand Up @@ -311,16 +332,16 @@ export class StarshipClient implements StarshipClientI {
'add',
this.ctx.repo,
this.ctx.repoUrl
]);
this.exec(['helm', 'repo', 'update']);
], { ignoreError: false });
this.exec(['helm', 'repo', 'update'], { ignoreError: false });
this.exec([
'helm',
'search',
'repo',
this.ctx.chart,
'--version',
this.config.version
]);
], { ignoreError: false });
}

private ensureFileExists(filename: string): void {
Expand Down Expand Up @@ -363,7 +384,7 @@ export class StarshipClient implements StarshipClientI {
}
});

this.exec(cmd);
this.exec(cmd, { ignoreError: false });
this.log("Run \"starship get-pods\" to check the status of the cluster");
}

Expand All @@ -386,7 +407,7 @@ export class StarshipClient implements StarshipClientI {
}

public checkConnection(): void {
const result = this.exec(['kubectl', 'get', 'nodes'], false, true);
const result = this.exec(['kubectl', 'get', 'nodes'], { log: false, silent: true });

if (result.code !== 0) {
this.log(chalk.red('Error: Unable to connect to the Kubernetes cluster.'));
Expand All @@ -406,7 +427,7 @@ export class StarshipClient implements StarshipClientI {
'-o',
'custom-columns=:metadata.name',
...this.getArgs(),
], false, true)
], { log: false, silent: true })

// Split the output by new lines and filter out any empty lines
const podNames = result.split('\n').filter(name => name.trim() !== '');
Expand Down Expand Up @@ -434,7 +455,7 @@ export class StarshipClient implements StarshipClientI {
'-o',
'custom-columns=:status.phase,:status.containerStatuses[*].ready,:status.containerStatuses[*].restartCount,:status.containerStatuses[*].state.waiting.reason',
...this.getArgs(),
], false, true).trim();
], { log: false, silent: true }).trim();

const [phase, readyList, restartCountList, reason] = result.split(/\s+/);
const ready = readyList.split(',').every(state => state === 'true');
Expand Down

0 comments on commit 1e8f248

Please sign in to comment.