From 82b115b97446467299158226e1eb9be6fe9274fd Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Wed, 28 Aug 2024 14:38:47 -0700 Subject: [PATCH] improvements --- .dep-stats.json | 1 - src/commands/diff-scan/get.ts | 38 +++++++++++++++++++++++------------ src/utils/api-helpers.ts | 4 +++- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/.dep-stats.json b/.dep-stats.json index fb76f10e..f4b40772 100644 --- a/.dep-stats.json +++ b/.dep-stats.json @@ -54,7 +54,6 @@ "latest-version": "^9.0.0", "log-symbols": "^6.0.0", "meow": "^13.2.0", - "node-fetch": "3.3.2", "npm-run-path": "^5.2.0", "open": "^10.1.0", "ora": "^8.0.1", diff --git a/src/commands/diff-scan/get.ts b/src/commands/diff-scan/get.ts index 74d1da40..c02ce0a7 100644 --- a/src/commands/diff-scan/get.ts +++ b/src/commands/diff-scan/get.ts @@ -1,4 +1,5 @@ import chalk from 'chalk' +import fs from 'fs' import meow from 'meow' import ora from 'ora' import util from 'util' @@ -24,7 +25,7 @@ export const get: CliSubcommand = { } const spinnerText = 'Getting diff scan... \n' const spinner = ora(spinnerText).start() - await getDiffScan(input.before, input.after, spinner, apiKey, input.orgSlug) + await getDiffScan(input, spinner, apiKey) } } } @@ -48,6 +49,12 @@ const getDiffScanFlags: { [key: string]: any } = { default: true, description: 'A boolean flag to persist or not the diff scan result' }, + file: { + type: 'string', + shortFlag: 'f', + default: '', + description: 'Path to a local file where the output should be saved' + } } // Internal functions @@ -59,6 +66,7 @@ type CommandContext = { after: string preview: boolean orgSlug: string + file: string } function setupCommand( @@ -75,13 +83,13 @@ function setupCommand( const cli = meow( ` Usage - $ ${name} + $ ${name} --before= --after= Options ${printFlagList(flags, 6)} Examples - $ ${name} + $ ${name} FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 `, { argv, @@ -97,6 +105,7 @@ function setupCommand( before, after, preview, + file } = cli.flags if (!before || !after) { @@ -123,34 +132,37 @@ function setupCommand( before, after, preview, - orgSlug + orgSlug, + file } } async function getDiffScan( - before: string, - after: string, + { before, after, orgSlug, file }: CommandContext, spinner: Ora, apiKey: string, - orgSlug: string ): Promise { const response = await queryAPI(`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`, apiKey) const data = await response.json(); - if(response.status !== 200){ + if(!response.ok){ spinner.stop() const err = await handleAPIError(response.status) - console.error(err) + console.error( + `${chalk.bgRed.white(response.statusText)}: ${err} \n` + ) return } spinner.stop() - // before: dfc4cf0c-aefd-4081-9e4e-7385257f26e2 - // after: 922e45f5-8a7b-4b16-95a5-e98ad00470f1 + if(file){ + fs.writeFile(file, JSON.stringify(data), err => { + err ? console.error(err) : console.log(`Data successfully written to ${file}`) + }) + return + } console.log(`\n Diff scan result: \n`) -// console.log(data); - console.log(util.inspect(data, {showHidden: false, depth: null, colors: true})) } diff --git a/src/utils/api-helpers.ts b/src/utils/api-helpers.ts index 6819b85c..68defebd 100644 --- a/src/utils/api-helpers.ts +++ b/src/utils/api-helpers.ts @@ -48,7 +48,9 @@ export async function handleApiCall( export async function handleAPIError(code: number) { if(code === 400){ - return `Bad request` + return `One of the options passed might be incorrect.` + } else if (code === 403){ + return `You might be trying to access an organization that is not linked to the API key you are logged in with.` } }