base cli program

This commit is contained in:
Kyle Spearrin 2019-03-16 11:27:27 -04:00
parent 69dd40b4d5
commit 87f7ad680a
2 changed files with 13 additions and 95 deletions

2
jslib

@ -1 +1 @@
Subproject commit 13a160fb795a69bad1edbb3fc5fd5c7c15396e03
Subproject commit b5b4222b325a5fab7fabfd53f23de1876ffccec8

View File

@ -23,18 +23,20 @@ import { UnlockCommand } from './commands/unlock.command';
import { UpdateCommand } from 'jslib/cli/commands/update.command';
import { Response } from 'jslib/cli/models/response';
import { ListResponse } from 'jslib/cli/models/response/listResponse';
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
import { TemplateResponse } from './models/response/templateResponse';
import { CliUtils } from './utils';
import { BaseProgram } from 'jslib/cli/baseProgram';
const chalk = chk.default;
const writeLn = CliUtils.writeLn;
export class Program {
constructor(private main: Main) { }
export class Program extends BaseProgram {
constructor(private main: Main) {
super(main.userService, writeLn);
}
run() {
program
@ -568,82 +570,13 @@ export class Program {
}
}
private processResponse(response: Response, exitImmediately = false) {
if (!response.success) {
if (process.env.BW_QUIET !== 'true') {
if (process.env.BW_RESPONSE === 'true') {
writeLn(this.getJson(response), true);
} else {
writeLn(chalk.redBright(response.message), true);
}
protected processResponse(response: Response, exitImmediately = false) {
super.processResponse(response, exitImmediately, () => {
if (response.data.object === 'template') {
return this.getJson((response.data as TemplateResponse).template);
}
if (exitImmediately) {
process.exit(1);
} else {
process.exitCode = 1;
}
return;
}
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;
if (data != null) {
out = data;
}
} else if (response.data.object === 'list') {
out = this.getJson((response.data as ListResponse).data);
} else if (response.data.object === 'template') {
out = this.getJson((response.data as TemplateResponse).template);
} else if (response.data.object === 'message') {
out = this.getMessage(response);
} else {
out = this.getJson(response.data);
}
if (out != null && process.env.BW_QUIET !== 'true') {
writeLn(out, true);
}
}
if (exitImmediately) {
process.exit(0);
} else {
process.exitCode = 0;
}
}
private getJson(obj: any): string {
if (process.env.BW_PRETTY === 'true') {
return JSON.stringify(obj, null, ' ');
} else {
return JSON.stringify(obj);
}
}
private getMessage(response: Response) {
const message = (response.data as MessageResponse);
if (process.env.BW_RAW === 'true' && message.raw != null) {
return message.raw;
}
let out: string = '';
if (message.title != null) {
if (message.noColor) {
out = message.title;
} else {
out = chalk.greenBright(message.title);
}
}
if (message.message != null) {
if (message.title != null) {
out += '\n';
}
out += message.message;
}
return out.trim() === '' ? null : out;
return null;
});
}
private async exitIfLocked() {
@ -653,19 +586,4 @@ export class Program {
this.processResponse(Response.error('Vault is locked.'), true);
}
}
private async exitIfAuthed() {
const authed = await this.main.userService.isAuthenticated();
if (authed) {
const email = await this.main.userService.getEmail();
this.processResponse(Response.error('You are already logged in as ' + email + '.'), true);
}
}
private async exitIfNotAuthed() {
const authed = await this.main.userService.isAuthenticated();
if (!authed) {
this.processResponse(Response.error('You are not logged in.'), true);
}
}
}