--response option

This commit is contained in:
Kyle Spearrin 2018-05-18 11:34:14 -04:00
parent 80eb38c915
commit 24f01b4006
1 changed files with 17 additions and 9 deletions

View File

@ -40,8 +40,9 @@ export class Program {
run() { run() {
program program
.option('--pretty', 'Format output. JSON is tabbed with two spaces.') .option('--pretty', 'Format output. JSON is tabbed with two spaces.')
.option('--quiet', 'Don\'t return anything to stdout.')
.option('--raw', 'Return raw output instead of a descriptive message.') .option('--raw', 'Return raw output instead of a descriptive message.')
.option('--response', 'Return a JSON formatted version of response output.')
.option('--quiet', 'Don\'t return anything to stdout.')
.option('--session <session>', 'Pass session key instead of reading from env.') .option('--session <session>', 'Pass session key instead of reading from env.')
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version'); .version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version');
@ -57,6 +58,10 @@ export class Program {
process.env.BW_QUIET = 'true'; process.env.BW_QUIET = 'true';
}); });
program.on('option:response', () => {
process.env.BW_RESPONSE = 'true';
});
program.on('option:session', (key) => { program.on('option:session', (key) => {
process.env.BW_SESSION = key; process.env.BW_SESSION = key;
}); });
@ -485,13 +490,19 @@ export class Program {
private processResponse(response: Response) { private processResponse(response: Response) {
if (!response.success) { if (!response.success) {
if (process.env.BW_QUIET !== 'true') { if (process.env.BW_QUIET !== 'true') {
writeLn(chalk.redBright(response.message), true); if (process.env.BW_RESPONSE === 'true') {
writeLn(this.getJson(response), true);
} else {
writeLn(chalk.redBright(response.message), true);
}
} }
process.exit(1); process.exit(1);
return; return;
} }
if (response.data != null) { if (process.env.BW_RESPONSE === 'true') {
writeLn(this.getJson(response), true);
} else if (response.data != null) {
let out: string = null; let out: string = null;
if (response.data.object === 'string') { if (response.data.object === 'string') {
const data = (response.data as StringResponse).data; const data = (response.data as StringResponse).data;
@ -546,8 +557,7 @@ export class Program {
await this.exitIfNotAuthed(); await this.exitIfNotAuthed();
const key = await this.main.cryptoService.getKey(); const key = await this.main.cryptoService.getKey();
if (key == null) { if (key == null) {
writeLn(chalk.redBright('Vault is locked.'), true); this.processResponse(Response.error('Vault is locked.'));
process.exit(1);
} }
} }
@ -555,16 +565,14 @@ export class Program {
const authed = await this.main.userService.isAuthenticated(); const authed = await this.main.userService.isAuthenticated();
if (authed) { if (authed) {
const email = await this.main.userService.getEmail(); const email = await this.main.userService.getEmail();
writeLn(chalk.redBright('You are already logged in as ' + email + '.'), true); this.processResponse(Response.error('You are already logged in as ' + email + '.'));
process.exit(1);
} }
} }
private async exitIfNotAuthed() { private async exitIfNotAuthed() {
const authed = await this.main.userService.isAuthenticated(); const authed = await this.main.userService.isAuthenticated();
if (!authed) { if (!authed) {
writeLn(chalk.redBright('You are not logged in.'), true); this.processResponse(Response.error('You are not logged in.'));
process.exit(1);
} }
} }
} }