From 8e2384239912a7541b080edfa3816f5915bbe5a3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 15 May 2018 12:44:10 -0400 Subject: [PATCH] pretty print json --- src/models/cipher.ts | 11 ++---- src/models/collection.ts | 11 ++---- src/models/folder.ts | 7 +--- src/models/response/cipherResponse.ts | 3 +- src/models/response/collectionResponse.ts | 3 +- src/models/response/folderResponse.ts | 3 +- src/program.ts | 47 ++++++++++++++++------- 7 files changed, 50 insertions(+), 35 deletions(-) diff --git a/src/models/cipher.ts b/src/models/cipher.ts index 930c62ca3f..23e9f5e8e8 100644 --- a/src/models/cipher.ts +++ b/src/models/cipher.ts @@ -12,9 +12,9 @@ import { SecureNote } from './secureNote'; export class Cipher { static template(): Cipher { const req = new Cipher(); - req.type = CipherType.Login; - req.folderId = null; req.organizationId = null; + req.folderId = null; + req.type = CipherType.Login; req.name = 'Item name'; req.notes = 'Some notes about this item.'; req.favorite = false; @@ -70,11 +70,8 @@ export class Cipher { card: Card; identity: Identity; - constructor(o?: CipherView) { - if (o == null) { - return; - } - + // Use build method instead of ctor so that we can control order of JSON stringify for pretty print + build(o: CipherView) { this.organizationId = o.organizationId; this.folderId = o.folderId; this.type = o.type; diff --git a/src/models/collection.ts b/src/models/collection.ts index 20e707f399..27bb9e71e2 100644 --- a/src/models/collection.ts +++ b/src/models/collection.ts @@ -9,15 +9,12 @@ export class Collection { return view; } - name: string; organizationId: string; + name: string; - constructor(o?: CollectionView) { - if (o == null) { - return; - } - - this.name = o.name; + // Use build method instead of ctor so that we can control order of JSON stringify for pretty print + build(o: CollectionView) { this.organizationId = o.organizationId; + this.name = o.name; } } diff --git a/src/models/folder.ts b/src/models/folder.ts index 010b5fa88c..d8638057c8 100644 --- a/src/models/folder.ts +++ b/src/models/folder.ts @@ -8,11 +8,8 @@ export class Folder { name: string; - constructor(o?: FolderView) { - if (o == null) { - return; - } - + // Use build method instead of ctor so that we can control order of JSON stringify for pretty print + build(o: FolderView) { this.name = o.name; } } diff --git a/src/models/response/cipherResponse.ts b/src/models/response/cipherResponse.ts index 683e0aa77d..e487f46908 100644 --- a/src/models/response/cipherResponse.ts +++ b/src/models/response/cipherResponse.ts @@ -9,8 +9,9 @@ export class CipherResponse extends Cipher implements BaseResponse { id: string; constructor(o: CipherView) { - super(o); + super(); this.object = 'item'; this.id = o.id; + this.build(o); } } diff --git a/src/models/response/collectionResponse.ts b/src/models/response/collectionResponse.ts index 627c10c3fc..c83ecff63a 100644 --- a/src/models/response/collectionResponse.ts +++ b/src/models/response/collectionResponse.ts @@ -9,8 +9,9 @@ export class CollectionResponse extends Collection implements BaseResponse { id: string; constructor(o: CollectionView) { - super(o); + super(); this.object = 'collection'; this.id = o.id; + this.build(o); } } diff --git a/src/models/response/folderResponse.ts b/src/models/response/folderResponse.ts index 66875abe19..0c80f730eb 100644 --- a/src/models/response/folderResponse.ts +++ b/src/models/response/folderResponse.ts @@ -9,8 +9,9 @@ export class FolderResponse extends Folder implements BaseResponse { id: string; constructor(o: FolderView) { - super(o); + super(); this.object = 'folder'; this.id = o.id; + this.build(o); } } diff --git a/src/program.ts b/src/program.ts index d31eb46930..dfff131d3f 100644 --- a/src/program.ts +++ b/src/program.ts @@ -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 ') @@ -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); + } }