message response

This commit is contained in:
Kyle Spearrin 2018-05-15 22:47:52 -04:00
parent 892f13f8ee
commit 33effb1a4d
3 changed files with 55 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import { AuthService } from 'jslib/abstractions/auth.service';
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service'; import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
import { Response } from '../models/response'; import { Response } from '../models/response';
import { MessageResponse } from '../models/response/messageResponse';
import { Utils } from 'jslib/misc/utils'; 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) { } catch (e) {
return Response.error(e); return Response.error(e);
} }

View File

@ -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;
}
}

View File

@ -12,12 +12,12 @@ import { ListCommand } from './commands/list.command';
import { LoginCommand } from './commands/login.command'; import { LoginCommand } from './commands/login.command';
import { SyncCommand } from './commands/sync.command'; import { SyncCommand } from './commands/sync.command';
import { Response } from './models/response';
import { ListResponse } from './models/response/listResponse'; import { ListResponse } from './models/response/listResponse';
import { MessageResponse } from './models/response/messageResponse';
import { StringResponse } from './models/response/stringResponse'; import { StringResponse } from './models/response/stringResponse';
import { TemplateResponse } from './models/response/templateResponse'; import { TemplateResponse } from './models/response/templateResponse';
import { Response } from './models/response';
const chalk = chk.default; const chalk = chk.default;
export class Program { export class Program {
@ -27,8 +27,17 @@ export class Program {
program program
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version') .version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version')
.option('--pretty', 'Format stdout.') .option('--pretty', 'Format stdout.')
.option('--raw', 'Raw output instead a descriptive message.')
.option('--session <session>', 'Session key.'); .option('--session <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) => { program.on('option:session', (key) => {
process.env.BW_SESSION = key; process.env.BW_SESSION = key;
}); });
@ -39,7 +48,7 @@ export class Program {
.option('-m, --method <method>', 'Two-step login method.') .option('-m, --method <method>', 'Two-step login method.')
.option('-c, --code <code>', 'Two-step login code.') .option('-c, --code <code>', 'Two-step login code.')
.action(async (email: string, password: string, cmd: program.Command) => { .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, const command = new LoginCommand(this.main.authService, this.main.apiService,
this.main.cryptoFunctionService); this.main.cryptoFunctionService);
const response = await command.run(email, password, cmd); const response = await command.run(email, password, cmd);
@ -57,14 +66,16 @@ export class Program {
program program
.command('lock') .command('lock')
.description('Lock the vault and destroy the current session token.') .description('Lock the vault and destroy the current session token.')
.action((cmd) => { .action(async (cmd) => {
await this.exitIfNotAuthed();
// TODO // TODO
}); });
program program
.command('unlock [password]') .command('unlock [password]')
.description('Unlock the vault and obtain a new session token.') .description('Unlock the vault and obtain a new session token.')
.action((cmd) => { .action(async (cmd) => {
await this.exitIfNotAuthed();
// TODO // TODO
}); });
@ -161,6 +172,8 @@ export class Program {
this.printJson((response.data as ListResponse).data, cmd); this.printJson((response.data as ListResponse).data, cmd);
} else if (response.data.object === 'template') { } else if (response.data.object === 'template') {
this.printJson((response.data as TemplateResponse).template, cmd); this.printJson((response.data as TemplateResponse).template, cmd);
} else if (response.data.object === 'message') {
this.printMessage(response);
} else { } else {
this.printJson(response.data, cmd); this.printJson(response.data, cmd);
} }
@ -169,23 +182,29 @@ export class Program {
} }
private printJson(obj: any, cmd: program.Command) { 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, ' ')); process.stdout.write(JSON.stringify(obj, null, ' '));
} else { } else {
process.stdout.write(JSON.stringify(obj)); process.stdout.write(JSON.stringify(obj));
} }
} }
private hasGlobalOption(option: string, cmd: program.Command): boolean { private printMessage(response: Response) {
if (cmd[option] || false) { const message = (response.data as MessageResponse);
return true; if (process.env.BW_RAW === 'true' && message.raw != null) {
process.stdout.write(message.raw);
return;
} }
if (cmd.parent == null) { if (message.title != null) {
return false; 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() { private async exitIfLocked() {