pretty print json
This commit is contained in:
parent
07cc64c0b8
commit
8e23842399
|
@ -12,9 +12,9 @@ import { SecureNote } from './secureNote';
|
||||||
export class Cipher {
|
export class Cipher {
|
||||||
static template(): Cipher {
|
static template(): Cipher {
|
||||||
const req = new Cipher();
|
const req = new Cipher();
|
||||||
req.type = CipherType.Login;
|
|
||||||
req.folderId = null;
|
|
||||||
req.organizationId = null;
|
req.organizationId = null;
|
||||||
|
req.folderId = null;
|
||||||
|
req.type = CipherType.Login;
|
||||||
req.name = 'Item name';
|
req.name = 'Item name';
|
||||||
req.notes = 'Some notes about this item.';
|
req.notes = 'Some notes about this item.';
|
||||||
req.favorite = false;
|
req.favorite = false;
|
||||||
|
@ -70,11 +70,8 @@ export class Cipher {
|
||||||
card: Card;
|
card: Card;
|
||||||
identity: Identity;
|
identity: Identity;
|
||||||
|
|
||||||
constructor(o?: CipherView) {
|
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
|
||||||
if (o == null) {
|
build(o: CipherView) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.organizationId = o.organizationId;
|
this.organizationId = o.organizationId;
|
||||||
this.folderId = o.folderId;
|
this.folderId = o.folderId;
|
||||||
this.type = o.type;
|
this.type = o.type;
|
||||||
|
|
|
@ -9,15 +9,12 @@ export class Collection {
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
name: string;
|
|
||||||
organizationId: string;
|
organizationId: string;
|
||||||
|
name: string;
|
||||||
|
|
||||||
constructor(o?: CollectionView) {
|
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
|
||||||
if (o == null) {
|
build(o: CollectionView) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.name = o.name;
|
|
||||||
this.organizationId = o.organizationId;
|
this.organizationId = o.organizationId;
|
||||||
|
this.name = o.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,11 +8,8 @@ export class Folder {
|
||||||
|
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
constructor(o?: FolderView) {
|
// Use build method instead of ctor so that we can control order of JSON stringify for pretty print
|
||||||
if (o == null) {
|
build(o: FolderView) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.name = o.name;
|
this.name = o.name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,9 @@ export class CipherResponse extends Cipher implements BaseResponse {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
constructor(o: CipherView) {
|
constructor(o: CipherView) {
|
||||||
super(o);
|
super();
|
||||||
this.object = 'item';
|
this.object = 'item';
|
||||||
this.id = o.id;
|
this.id = o.id;
|
||||||
|
this.build(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,9 @@ export class CollectionResponse extends Collection implements BaseResponse {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
constructor(o: CollectionView) {
|
constructor(o: CollectionView) {
|
||||||
super(o);
|
super();
|
||||||
this.object = 'collection';
|
this.object = 'collection';
|
||||||
this.id = o.id;
|
this.id = o.id;
|
||||||
|
this.build(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,8 +9,9 @@ export class FolderResponse extends Folder implements BaseResponse {
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
constructor(o: FolderView) {
|
constructor(o: FolderView) {
|
||||||
super(o);
|
super();
|
||||||
this.object = 'folder';
|
this.object = 'folder';
|
||||||
this.id = o.id;
|
this.id = o.id;
|
||||||
|
this.build(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,8 @@ export class Program {
|
||||||
|
|
||||||
run() {
|
run() {
|
||||||
program
|
program
|
||||||
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version');
|
.version(this.main.platformUtilsService.getApplicationVersion(), '-v, --version')
|
||||||
|
.option('--pretty', 'Format stdout.');
|
||||||
|
|
||||||
program
|
program
|
||||||
.command('login <email> <password>')
|
.command('login <email> <password>')
|
||||||
|
@ -35,7 +36,7 @@ export class Program {
|
||||||
.action(async (email: string, password: string, cmd: program.Command) => {
|
.action(async (email: string, password: string, cmd: program.Command) => {
|
||||||
const command = new LoginCommand(this.main.authService);
|
const command = new LoginCommand(this.main.authService);
|
||||||
const response = await command.run(email, password, cmd);
|
const response = await command.run(email, password, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -52,7 +53,7 @@ export class Program {
|
||||||
.action(async (cmd) => {
|
.action(async (cmd) => {
|
||||||
const command = new SyncCommand(this.main.syncService);
|
const command = new SyncCommand(this.main.syncService);
|
||||||
const response = await command.run(cmd);
|
const response = await command.run(cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -62,7 +63,7 @@ export class Program {
|
||||||
const command = new ListCommand(this.main.cipherService, this.main.folderService,
|
const command = new ListCommand(this.main.cipherService, this.main.folderService,
|
||||||
this.main.collectionService);
|
this.main.collectionService);
|
||||||
const response = await command.run(object, cmd);
|
const response = await command.run(object, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -72,7 +73,7 @@ export class Program {
|
||||||
const command = new GetCommand(this.main.cipherService, this.main.folderService,
|
const command = new GetCommand(this.main.cipherService, this.main.folderService,
|
||||||
this.main.collectionService, this.main.totpService);
|
this.main.collectionService, this.main.totpService);
|
||||||
const response = await command.run(object, id, cmd);
|
const response = await command.run(object, id, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -81,7 +82,7 @@ export class Program {
|
||||||
.action(async (object, encodedData, cmd) => {
|
.action(async (object, encodedData, cmd) => {
|
||||||
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
|
const command = new CreateCommand(this.main.cipherService, this.main.folderService);
|
||||||
const response = await command.run(object, encodedData, cmd);
|
const response = await command.run(object, encodedData, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -90,7 +91,7 @@ export class Program {
|
||||||
.action(async (object, id, encodedData, cmd) => {
|
.action(async (object, id, encodedData, cmd) => {
|
||||||
const command = new EditCommand(this.main.cipherService, this.main.folderService);
|
const command = new EditCommand(this.main.cipherService, this.main.folderService);
|
||||||
const response = await command.run(object, id, encodedData, cmd);
|
const response = await command.run(object, id, encodedData, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -99,7 +100,7 @@ export class Program {
|
||||||
.action(async (object, id, cmd) => {
|
.action(async (object, id, cmd) => {
|
||||||
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
|
const command = new DeleteCommand(this.main.cipherService, this.main.folderService);
|
||||||
const response = await command.run(object, id, cmd);
|
const response = await command.run(object, id, cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
|
@ -108,24 +109,24 @@ export class Program {
|
||||||
.action(async (object, id, cmd) => {
|
.action(async (object, id, cmd) => {
|
||||||
const command = new EncodeCommand();
|
const command = new EncodeCommand();
|
||||||
const response = await command.run(cmd);
|
const response = await command.run(cmd);
|
||||||
this.processResponse(response);
|
this.processResponse(response, cmd);
|
||||||
});
|
});
|
||||||
|
|
||||||
program
|
program
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
private processResponse(response: Response) {
|
private processResponse(response: Response, cmd: program.Command) {
|
||||||
if (response.success) {
|
if (response.success) {
|
||||||
if (response.data != null) {
|
if (response.data != null) {
|
||||||
if (response.data.object === 'string') {
|
if (response.data.object === 'string') {
|
||||||
process.stdout.write((response.data as StringResponse).data);
|
process.stdout.write((response.data as StringResponse).data);
|
||||||
} else if (response.data.object === 'list') {
|
} 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') {
|
} 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 {
|
} else {
|
||||||
process.stdout.write(JSON.stringify(response.data));
|
this.printJson(response.data, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
process.exit();
|
process.exit();
|
||||||
|
@ -134,4 +135,24 @@ export class Program {
|
||||||
process.exit(1);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue