From 33effb1a4de8ddd17992de82949481ce676c8cef Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 15 May 2018 22:47:52 -0400 Subject: [PATCH] message response --- src/commands/login.command.ts | 10 +++++- src/models/response/messageResponse.ts | 14 ++++++++ src/program.ts | 45 ++++++++++++++++++-------- 3 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 src/models/response/messageResponse.ts diff --git a/src/commands/login.command.ts b/src/commands/login.command.ts index 3e8319b3fd..c3f92bae02 100644 --- a/src/commands/login.command.ts +++ b/src/commands/login.command.ts @@ -11,6 +11,7 @@ import { AuthService } from 'jslib/abstractions/auth.service'; import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service'; import { Response } from '../models/response'; +import { MessageResponse } from '../models/response/messageResponse'; import { Utils } from 'jslib/misc/utils'; @@ -106,7 +107,14 @@ export class LoginCommand { } } } - return Response.success(); + + const res = new MessageResponse('You are logged in!', '\n' + + 'To unlock your vault, set your session key to the `BW_SESSION` environment variable. ex:\n' + + '$ export BW_SESSION="' + process.env.BW_SESSION + '"\n\n' + + 'You can also pass the session key to any command with the `--session` option. ex:\n' + + '$ bw get items --session ' + process.env.BW_SESSION); + res.raw = process.env.BW_SESSION; + return Response.success(res); } catch (e) { return Response.error(e); } diff --git a/src/models/response/messageResponse.ts b/src/models/response/messageResponse.ts new file mode 100644 index 0000000000..50e28052a3 --- /dev/null +++ b/src/models/response/messageResponse.ts @@ -0,0 +1,14 @@ +import { BaseResponse } from './baseResponse'; + +export class MessageResponse implements BaseResponse { + object: string; + title: string; + message: string; + raw: string; + + constructor(title: string, message: string) { + this.object = 'message'; + this.title = title; + this.message = message; + } +} diff --git a/src/program.ts b/src/program.ts index 1ca1969e13..e11c7a26a4 100644 --- a/src/program.ts +++ b/src/program.ts @@ -12,12 +12,12 @@ import { ListCommand } from './commands/list.command'; import { LoginCommand } from './commands/login.command'; import { SyncCommand } from './commands/sync.command'; +import { Response } from './models/response'; import { ListResponse } from './models/response/listResponse'; +import { MessageResponse } from './models/response/messageResponse'; import { StringResponse } from './models/response/stringResponse'; import { TemplateResponse } from './models/response/templateResponse'; -import { Response } from './models/response'; - const chalk = chk.default; export class Program { @@ -27,8 +27,17 @@ export class Program { program .version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version') .option('--pretty', 'Format stdout.') + .option('--raw', 'Raw output instead a descriptive message.') .option('--session ', 'Session key.'); + program.on('option:pretty', () => { + process.env.BW_PRETTY = 'true'; + }); + + program.on('option:raw', () => { + process.env.BW_RAW = 'true'; + }); + program.on('option:session', (key) => { process.env.BW_SESSION = key; }); @@ -39,7 +48,7 @@ export class Program { .option('-m, --method ', 'Two-step login method.') .option('-c, --code ', 'Two-step login code.') .action(async (email: string, password: string, cmd: program.Command) => { - await this.exitIfAuthed(); + // await this.exitIfAuthed(); const command = new LoginCommand(this.main.authService, this.main.apiService, this.main.cryptoFunctionService); const response = await command.run(email, password, cmd); @@ -57,14 +66,16 @@ export class Program { program .command('lock') .description('Lock the vault and destroy the current session token.') - .action((cmd) => { + .action(async (cmd) => { + await this.exitIfNotAuthed(); // TODO }); program .command('unlock [password]') .description('Unlock the vault and obtain a new session token.') - .action((cmd) => { + .action(async (cmd) => { + await this.exitIfNotAuthed(); // TODO }); @@ -161,6 +172,8 @@ export class Program { this.printJson((response.data as ListResponse).data, cmd); } else if (response.data.object === 'template') { this.printJson((response.data as TemplateResponse).template, cmd); + } else if (response.data.object === 'message') { + this.printMessage(response); } else { this.printJson(response.data, cmd); } @@ -169,23 +182,29 @@ export class Program { } private printJson(obj: any, cmd: program.Command) { - if (this.hasGlobalOption('pretty', cmd)) { + if (process.env.BW_PRETTY === 'true') { process.stdout.write(JSON.stringify(obj, null, ' ')); } else { process.stdout.write(JSON.stringify(obj)); } } - private hasGlobalOption(option: string, cmd: program.Command): boolean { - if (cmd[option] || false) { - return true; + private printMessage(response: Response) { + const message = (response.data as MessageResponse); + if (process.env.BW_RAW === 'true' && message.raw != null) { + process.stdout.write(message.raw); + return; } - if (cmd.parent == null) { - return false; + if (message.title != null) { + process.stdout.write(chalk.greenBright(message.title)); + } + if (message.message != null) { + if (message.title != null) { + process.stdout.write('\n'); + } + process.stdout.write(message.message); } - - return this.hasGlobalOption(option, cmd.parent); } private async exitIfLocked() {