Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
charliegerard committed Aug 28, 2024
1 parent 2ab6308 commit 82b115b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
1 change: 0 additions & 1 deletion .dep-stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 25 additions & 13 deletions src/commands/diff-scan/get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import chalk from 'chalk'
import fs from 'fs'
import meow from 'meow'
import ora from 'ora'
import util from 'util'
Expand All @@ -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)
}
}
}
Expand All @@ -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
Expand All @@ -59,6 +66,7 @@ type CommandContext = {
after: string
preview: boolean
orgSlug: string
file: string
}

function setupCommand(
Expand All @@ -75,13 +83,13 @@ function setupCommand(
const cli = meow(
`
Usage
$ ${name}
$ ${name} <org slug> --before=<before> --after=<after>
Options
${printFlagList(flags, 6)}
Examples
$ ${name}
$ ${name} FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
`,
{
argv,
Expand All @@ -97,6 +105,7 @@ function setupCommand(
before,
after,
preview,
file
} = cli.flags

if (!before || !after) {
Expand All @@ -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<void> {
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}))
}
4 changes: 3 additions & 1 deletion src/utils/api-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export async function handleApiCall<T>(

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.`
}
}

Expand Down

0 comments on commit 82b115b

Please sign in to comment.