mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-19 20:51:35 +01:00
base cli program
This commit is contained in:
parent
69dd40b4d5
commit
87f7ad680a
2
jslib
2
jslib
@ -1 +1 @@
|
|||||||
Subproject commit 13a160fb795a69bad1edbb3fc5fd5c7c15396e03
|
Subproject commit b5b4222b325a5fab7fabfd53f23de1876ffccec8
|
106
src/program.ts
106
src/program.ts
@ -23,18 +23,20 @@ import { UnlockCommand } from './commands/unlock.command';
|
|||||||
import { UpdateCommand } from 'jslib/cli/commands/update.command';
|
import { UpdateCommand } from 'jslib/cli/commands/update.command';
|
||||||
|
|
||||||
import { Response } from 'jslib/cli/models/response';
|
import { Response } from 'jslib/cli/models/response';
|
||||||
import { ListResponse } from 'jslib/cli/models/response/listResponse';
|
|
||||||
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
|
import { MessageResponse } from 'jslib/cli/models/response/messageResponse';
|
||||||
import { StringResponse } from 'jslib/cli/models/response/stringResponse';
|
|
||||||
|
|
||||||
import { TemplateResponse } from './models/response/templateResponse';
|
import { TemplateResponse } from './models/response/templateResponse';
|
||||||
import { CliUtils } from './utils';
|
import { CliUtils } from './utils';
|
||||||
|
|
||||||
|
import { BaseProgram } from 'jslib/cli/baseProgram';
|
||||||
|
|
||||||
const chalk = chk.default;
|
const chalk = chk.default;
|
||||||
const writeLn = CliUtils.writeLn;
|
const writeLn = CliUtils.writeLn;
|
||||||
|
|
||||||
export class Program {
|
export class Program extends BaseProgram {
|
||||||
constructor(private main: Main) { }
|
constructor(private main: Main) {
|
||||||
|
super(main.userService, writeLn);
|
||||||
|
}
|
||||||
|
|
||||||
run() {
|
run() {
|
||||||
program
|
program
|
||||||
@ -568,82 +570,13 @@ export class Program {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private processResponse(response: Response, exitImmediately = false) {
|
protected processResponse(response: Response, exitImmediately = false) {
|
||||||
if (!response.success) {
|
super.processResponse(response, exitImmediately, () => {
|
||||||
if (process.env.BW_QUIET !== 'true') {
|
if (response.data.object === 'template') {
|
||||||
if (process.env.BW_RESPONSE === 'true') {
|
return this.getJson((response.data as TemplateResponse).template);
|
||||||
writeLn(this.getJson(response), true);
|
|
||||||
} else {
|
|
||||||
writeLn(chalk.redBright(response.message), true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (exitImmediately) {
|
return null;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async exitIfLocked() {
|
private async exitIfLocked() {
|
||||||
@ -653,19 +586,4 @@ export class Program {
|
|||||||
this.processResponse(Response.error('Vault is locked.'), true);
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user