From fe85751f7e82050f55a56aed3ad5c3195ad25380 Mon Sep 17 00:00:00 2001 From: Hoon Oh Date: Mon, 28 Oct 2019 15:44:59 +0900 Subject: [PATCH] feat(cli): add JSON output option (#14) --- README.md | 4 ++ src/cli.spec.ts | 13 +++++ src/cli.ts | 14 +++++- test/__snapshots__/cli.spec.ts.snap | 77 +++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d4e66b8c..ece11069 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,10 @@ You can use `linux` or `windows` (all in lowercase) as wildcard. Limits list of price information items to be returned. +#### --json | -j + +Outputs in JSON format. This option will silence any progress output. + #### --accessKeyId Specific AWS Access Key ID. Requires `--secretAccessKey` option to be used together. diff --git a/src/cli.spec.ts b/src/cli.spec.ts index de1d3af6..46699123 100644 --- a/src/cli.spec.ts +++ b/src/cli.spec.ts @@ -75,6 +75,19 @@ describe('cli', () => { expect(consoleMockCallJoin()).toMatchSnapshot(); }); + it('should handle JSON output option', async () => { + await main(['--json', '-r', 'us-east-1', '-l', '10']); + const results = consoleMockCallJoin(); + const resultsObject = JSON.parse(results); + expect(results).toMatchSnapshot(); + expect(Object.keys(resultsObject).length).toEqual(10); + Object.keys(resultsObject).forEach(key => { + expect( + (resultsObject[key].AvailabilityZone as string).startsWith('us-east-1'), + ).toBeTruthy(); + }); + }); + it('should handle missing accessKeyId', async () => { let caughtError = false; try { diff --git a/src/cli.ts b/src/cli.ts index e63ef4b6..906845df 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -99,6 +99,12 @@ export const main = (argvInput?: string[]): Promise => return val; }, }, + json: { + alias: 'j', + describe: 'Outputs in JSON format', + type: 'boolean', + default: false, + }, accessKeyId: { describe: 'AWS Access Key ID.', type: 'string', @@ -120,9 +126,10 @@ export const main = (argvInput?: string[]): Promise => limit, priceMax, productDescription, + json, accessKeyId, secretAccessKey, - } = args.ui ? { ...(await ui()), instanceType: undefined } : args; + } = args.ui ? { ...(await ui()), instanceType: undefined, json: false } : args; // process instance families let familyTypeSet: Set; @@ -191,7 +198,7 @@ export const main = (argvInput?: string[]): Promise => const familyTypeSetArray = Array.from(familyTypeSet); const sizeSetArray = Array.from(sizeSet); - await getGlobalSpotPrices({ + const results = await getGlobalSpotPrices({ regions: region as Region[], instanceTypes: instanceType as InstanceType[], familyTypes: familyTypeSetArray.length ? familyTypeSetArray : undefined, @@ -203,8 +210,11 @@ export const main = (argvInput?: string[]): Promise => : undefined, accessKeyId, secretAccessKey, + silent: json, }); + if (json) console.log(JSON.stringify(results, null, 2)); + res(); } catch (error) { /* istanbul ignore else */ diff --git a/test/__snapshots__/cli.spec.ts.snap b/test/__snapshots__/cli.spec.ts.snap index 3698c176..eb99fa30 100644 --- a/test/__snapshots__/cli.spec.ts.snap +++ b/test/__snapshots__/cli.spec.ts.snap @@ -1,5 +1,80 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`cli test by import should handle JSON output option 1`] = ` +"[ + { + \\"AvailabilityZone\\": \\"us-east-1a\\", + \\"InstanceType\\": \\"t3.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001600\\", + \\"Timestamp\\": \\"2019-10-15T17:09:43.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1b\\", + \\"InstanceType\\": \\"t3.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001600\\", + \\"Timestamp\\": \\"2019-10-15T17:09:43.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1c\\", + \\"InstanceType\\": \\"t3.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001600\\", + \\"Timestamp\\": \\"2019-10-15T17:09:43.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1d\\", + \\"InstanceType\\": \\"t3.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001600\\", + \\"Timestamp\\": \\"2019-10-15T17:09:43.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1f\\", + \\"InstanceType\\": \\"t3.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001600\\", + \\"Timestamp\\": \\"2019-10-15T17:09:43.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1a\\", + \\"InstanceType\\": \\"t3a.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001700\\", + \\"Timestamp\\": \\"2019-10-15T22:11:29.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1c\\", + \\"InstanceType\\": \\"t3a.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001700\\", + \\"Timestamp\\": \\"2019-10-15T22:11:29.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1b\\", + \\"InstanceType\\": \\"t3a.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001800\\", + \\"Timestamp\\": \\"2019-10-15T22:11:29.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1d\\", + \\"InstanceType\\": \\"t3a.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001800\\", + \\"Timestamp\\": \\"2019-10-15T20:21:10.000Z\\" + }, + { + \\"AvailabilityZone\\": \\"us-east-1f\\", + \\"InstanceType\\": \\"t3a.nano\\", + \\"ProductDescription\\": \\"Linux/UNIX (Amazon VPC)\\", + \\"SpotPrice\\": \\"0.001800\\", + \\"Timestamp\\": \\"2019-10-15T22:11:29.000Z\\" + } +]" +`; + exports[`cli test by import should handle instance family option 1`] = ` "╔═══════════╤══════════╤═════════════════════════╤═════════════════╤═════════════════════════╗ ║ c1.medium │ 0.013000 │ Linux/UNIX │ us-east-1a │ US East (N. Virginia) ║ @@ -131,6 +206,7 @@ Options: Linux (Amazon VPC)\\", \\"Red Hat Enterprise Linux\\", \\"Red Hat Enterprise Linux (Amazon VPC)\\", \\"Windows\\", \\"Windows (Amazon VPC)\\", \\"linux\\", \\"windows\\"] --limit, -l Limit results output length [number] [default: 20] + --json, -j Outputs in JSON format [boolean] [default: false] --accessKeyId AWS Access Key ID. [string] --secretAccessKey AWS Secret Access Key. [string]" `; @@ -398,6 +474,7 @@ Options: Linux (Amazon VPC)\\", \\"Red Hat Enterprise Linux\\", \\"Red Hat Enterprise Linux (Amazon VPC)\\", \\"Windows\\", \\"Windows (Amazon VPC)\\", \\"linux\\", \\"windows\\"] --limit, -l Limit results output length [number] [default: 20] + --json, -j Outputs in JSON format [boolean] [default: false] --accessKeyId AWS Access Key ID. [string] --secretAccessKey AWS Secret Access Key. [string] "