Skip to content

Commit

Permalink
feat(cli): add JSON output option (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoonoh committed Oct 28, 2019
1 parent cd2f920 commit fe85751
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

#### <a name="accessKeyId"></a>--accessKeyId

Specific AWS Access Key ID. Requires `--secretAccessKey` option to be used together.
Expand Down
13 changes: 13 additions & 0 deletions src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export const main = (argvInput?: string[]): Promise<void> =>
return val;
},
},
json: {
alias: 'j',
describe: 'Outputs in JSON format',
type: 'boolean',
default: false,
},
accessKeyId: {
describe: 'AWS Access Key ID.',
type: 'string',
Expand All @@ -120,9 +126,10 @@ export const main = (argvInput?: string[]): Promise<void> =>
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<InstanceFamilyType>;
Expand Down Expand Up @@ -191,7 +198,7 @@ export const main = (argvInput?: string[]): Promise<void> =>
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,
Expand All @@ -203,8 +210,11 @@ export const main = (argvInput?: string[]): Promise<void> =>
: undefined,
accessKeyId,
secretAccessKey,
silent: json,
});

if (json) console.log(JSON.stringify(results, null, 2));

res();
} catch (error) {
/* istanbul ignore else */
Expand Down
77 changes: 77 additions & 0 deletions test/__snapshots__/cli.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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) ║
Expand Down Expand Up @@ -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]"
`;
Expand Down Expand Up @@ -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]
"
Expand Down

0 comments on commit fe85751

Please sign in to comment.