From f9d09888ced97e1898798180a2745cce50e89b64 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 19 Jan 2024 18:02:16 +0000 Subject: [PATCH] Add ability to output the CLI in JSON format (#124) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Added a JSON flag (`-j`, `--json`) to the CLI to allow you to output in JSON #73 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - Have added tests to ensure that the JSON is valid # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that provde my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/124 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/cli.ts | 7 +++++++ src/contracts/ICliOptions.ts | 1 + tests/cli.test.ts | 20 +++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 6f0639f..d103e86 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -10,6 +10,7 @@ program .description('Get a random image url from a subreddit of your choosing') .version('2.2') .option('-s, --subreddit ', 'The subreddit to search', 'rabbits') + .option('-j, --json', 'Output as JSON') .option('-q, --query-metadata', 'Include query metadata in result') .addOption(new Option('--sort ', 'Sort by').default('hot').choices(['hot', 'new', 'top'])); @@ -23,6 +24,12 @@ randomBunny(options.subreddit, options.sort) const result = response.Result!; const outputLines: string[] = []; + + if (options.json) { + console.log(JSON.stringify(result)); + return; + } + outputLines.push(`Archived = ${result.Archived}`); outputLines.push(`Downvotes = ${result.Downs}`); outputLines.push(`Hidden = ${result.Hidden}`); diff --git a/src/contracts/ICliOptions.ts b/src/contracts/ICliOptions.ts index ce5ada4..cc3af03 100644 --- a/src/contracts/ICliOptions.ts +++ b/src/contracts/ICliOptions.ts @@ -1,5 +1,6 @@ export default interface ICliOptions { subreddit: string, + json?: boolean, sort: string, queryMetadata?: boolean, } \ No newline at end of file diff --git a/tests/cli.test.ts b/tests/cli.test.ts index 1b04e47..9671235 100644 --- a/tests/cli.test.ts +++ b/tests/cli.test.ts @@ -90,6 +90,24 @@ describe('subreddit', () => { }, 5000); }); +describe('json', () => { + test('GIVEN -j is supplied, EXPECT output to be valid JSON', async () => { + const result = await cli(['-j'], '.'); + + const json = JSON.parse(result.stdout); + + expect(json).toBeDefined(); + }, 5000); + + test('GIVEN --json is supplied, EXPECT output to be valid JSON', async () => { + const result = await cli(['--json'], '.'); + + const json = JSON.parse(result.stdout); + + expect(json).toBeDefined(); + }, 5000); +}); + describe('sort', () => { test('GIVEN --sort is not supplied, EXPECT sort to be defaulted', async () => { const result = await cli(['-q'], '.'); @@ -116,7 +134,7 @@ describe('sort', () => { expect(result.code).toBe(1); expect(result.stderr).toBe("error: option '--sort ' argument 'invalid' is invalid. Allowed choices are hot, new, top.\n"); - }); + }, 5000); }); describe('query-metadata', () => {