From 24f01b400659fa7b8fcab87b1ab8d56fb8c32128 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 18 May 2018 11:34:14 -0400 Subject: [PATCH] --response option --- src/program.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/program.ts b/src/program.ts index d4e80fbe4c..e31790ece0 100644 --- a/src/program.ts +++ b/src/program.ts @@ -40,8 +40,9 @@ export class Program { run() { program .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('--response', 'Return a JSON formatted version of response output.') + .option('--quiet', 'Don\'t return anything to stdout.') .option('--session ', 'Pass session key instead of reading from env.') .version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version'); @@ -57,6 +58,10 @@ export class Program { process.env.BW_QUIET = 'true'; }); + program.on('option:response', () => { + process.env.BW_RESPONSE = 'true'; + }); + program.on('option:session', (key) => { process.env.BW_SESSION = key; }); @@ -485,13 +490,19 @@ export class Program { private processResponse(response: Response) { if (!response.success) { 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); 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; if (response.data.object === 'string') { const data = (response.data as StringResponse).data; @@ -546,8 +557,7 @@ export class Program { await this.exitIfNotAuthed(); const key = await this.main.cryptoService.getKey(); if (key == null) { - writeLn(chalk.redBright('Vault is locked.'), true); - process.exit(1); + this.processResponse(Response.error('Vault is locked.')); } } @@ -555,16 +565,14 @@ export class Program { const authed = await this.main.userService.isAuthenticated(); if (authed) { const email = await this.main.userService.getEmail(); - writeLn(chalk.redBright('You are already logged in as ' + email + '.'), true); - process.exit(1); + this.processResponse(Response.error('You are already logged in as ' + email + '.')); } } private async exitIfNotAuthed() { const authed = await this.main.userService.isAuthenticated(); if (!authed) { - writeLn(chalk.redBright('You are not logged in.'), true); - process.exit(1); + this.processResponse(Response.error('You are not logged in.')); } } }