Skip to content

Commit 1e8f248

Browse files
authored
add error handling in exec command (#537)
1 parent 92136d3 commit 1e8f248

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

clients/js/packages/client/src/client.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ export interface PodStatus {
9191
reason?: string;
9292
}
9393

94+
export interface ExecOptions {
95+
log?: boolean;
96+
silent?: boolean;
97+
ignoreError?: boolean;
98+
}
99+
100+
const defaultExecOptions: ExecOptions = {
101+
log: true,
102+
silent: false,
103+
ignoreError: true,
104+
}
105+
94106
export const formatChainID = (input: string): string => {
95107
// Replace underscores with hyphens
96108
let formattedName = input.replace(/_/g, '-');
@@ -122,11 +134,20 @@ export class StarshipClient implements StarshipClientI {
122134
this.version = readAndParsePackageJson().version;
123135
}
124136

125-
private exec(cmd: string[], log: boolean = true, silent: boolean = false): shell.ShellString {
137+
private exec(cmd: string[], options: Partial<ExecOptions> = {}): shell.ShellString {
138+
const opts = {...defaultExecOptions, ...options};
126139
this.checkDependencies();
127140
const str = cmd.join(' ');
128-
if (log) this.log(str);
129-
return shell.exec(str, { silent });
141+
if (opts.log) this.log(str);
142+
143+
const result = shell.exec(str, { silent: opts.silent });
144+
145+
if (result.code !== 0 && !opts.ignoreError) {
146+
this.log(chalk.red('Error: ') + result.stderr);
147+
this.exit(result.code);
148+
}
149+
150+
return result;
130151
}
131152

132153
private log(str: string): void {
@@ -311,16 +332,16 @@ export class StarshipClient implements StarshipClientI {
311332
'add',
312333
this.ctx.repo,
313334
this.ctx.repoUrl
314-
]);
315-
this.exec(['helm', 'repo', 'update']);
335+
], { ignoreError: false });
336+
this.exec(['helm', 'repo', 'update'], { ignoreError: false });
316337
this.exec([
317338
'helm',
318339
'search',
319340
'repo',
320341
this.ctx.chart,
321342
'--version',
322343
this.config.version
323-
]);
344+
], { ignoreError: false });
324345
}
325346

326347
private ensureFileExists(filename: string): void {
@@ -363,7 +384,7 @@ export class StarshipClient implements StarshipClientI {
363384
}
364385
});
365386

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

@@ -386,7 +407,7 @@ export class StarshipClient implements StarshipClientI {
386407
}
387408

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

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

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

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

0 commit comments

Comments
 (0)