mirror of
https://github.com/bitwarden/browser
synced 2025-12-13 14:53:33 +00:00
pretty print json
This commit is contained in:
@@ -25,7 +25,8 @@ export class Program {
|
||||
|
||||
run() {
|
||||
program
|
||||
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version');
|
||||
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version')
|
||||
.option('--pretty', 'Format stdout.');
|
||||
|
||||
program
|
||||
.command('login <email> <password>')
|
||||
@@ -35,7 +36,7 @@ export class Program {
|
||||
.action(async (email: string, password: string, cmd: program.Command) => {
|
||||
const command = new LoginCommand(this.main.authService);
|
||||
const response = await command.run(email, password, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -52,7 +53,7 @@ export class Program {
|
||||
.action(async (cmd) => {
|
||||
const command = new SyncCommand(this.main.syncService);
|
||||
const response = await command.run(cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -62,7 +63,7 @@ export class Program {
|
||||
const command = new ListCommand(this.main.cipherService, this.main.folderService,
|
||||
this.main.collectionService);
|
||||
const response = await command.run(object, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -72,7 +73,7 @@ export class Program {
|
||||
const command = new GetCommand(this.main.cipherService, this.main.folderService,
|
||||
this.main.collectionService, this.main.totpService);
|
||||
const response = await command.run(object, id, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -81,7 +82,7 @@ export class Program {
|
||||
.action(async (object, encodedData, cmd) => {
|
||||
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
|
||||
const response = await command.run(object, encodedData, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -90,7 +91,7 @@ export class Program {
|
||||
.action(async (object, id, encodedData, cmd) => {
|
||||
const command = new EditCommand(this.main.cipherService, this.main.folderService);
|
||||
const response = await command.run(object, id, encodedData, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -99,7 +100,7 @@ export class Program {
|
||||
.action(async (object, id, cmd) => {
|
||||
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
|
||||
const response = await command.run(object, id, cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
@@ -108,24 +109,24 @@ export class Program {
|
||||
.action(async (object, id, cmd) => {
|
||||
const command = new EncodeCommand();
|
||||
const response = await command.run(cmd);
|
||||
this.processResponse(response);
|
||||
this.processResponse(response, cmd);
|
||||
});
|
||||
|
||||
program
|
||||
.parse(process.argv);
|
||||
}
|
||||
|
||||
private processResponse(response: Response) {
|
||||
private processResponse(response: Response, cmd: program.Command) {
|
||||
if (response.success) {
|
||||
if (response.data != null) {
|
||||
if (response.data.object === 'string') {
|
||||
process.stdout.write((response.data as StringResponse).data);
|
||||
} else if (response.data.object === 'list') {
|
||||
process.stdout.write(JSON.stringify((response.data as ListResponse).data));
|
||||
this.printJson((response.data as ListResponse).data, cmd);
|
||||
} else if (response.data.object === 'template') {
|
||||
process.stdout.write(JSON.stringify((response.data as TemplateResponse).template));
|
||||
this.printJson((response.data as TemplateResponse).template, cmd);
|
||||
} else {
|
||||
process.stdout.write(JSON.stringify(response.data));
|
||||
this.printJson(response.data, cmd);
|
||||
}
|
||||
}
|
||||
process.exit();
|
||||
@@ -134,4 +135,24 @@ export class Program {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private printJson(obj: any, cmd: program.Command) {
|
||||
if (this.hasGlobalOption('pretty', cmd)) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (cmd.parent == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return this.hasGlobalOption(option, cmd.parent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user