Skip to content

Commit 6bbdcfd

Browse files
committed
improvements
1 parent d33a286 commit 6bbdcfd

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

.dep-stats.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"latest-version": "^9.0.0",
5757
"log-symbols": "^6.0.0",
5858
"meow": "^13.2.0",
59-
"node-fetch": "3.3.2",
6059
"npm-run-path": "^5.2.0",
6160
"open": "^10.1.0",
6261
"ora": "^8.0.1",

src/commands/diff-scan/get.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import chalk from 'chalk'
2+
import fs from 'fs'
23
import meow from 'meow'
34
import ora from 'ora'
45
import util from 'util'
@@ -24,7 +25,7 @@ export const get: CliSubcommand = {
2425
}
2526
const spinnerText = 'Getting diff scan... \n'
2627
const spinner = ora(spinnerText).start()
27-
await getDiffScan(input.before, input.after, spinner, apiKey, input.orgSlug)
28+
await getDiffScan(input, spinner, apiKey)
2829
}
2930
}
3031
}
@@ -48,6 +49,12 @@ const getDiffScanFlags: { [key: string]: any } = {
4849
default: true,
4950
description: 'A boolean flag to persist or not the diff scan result'
5051
},
52+
file: {
53+
type: 'string',
54+
shortFlag: 'f',
55+
default: '',
56+
description: 'Path to a local file where the output should be saved'
57+
}
5158
}
5259

5360
// Internal functions
@@ -59,6 +66,7 @@ type CommandContext = {
5966
after: string
6067
preview: boolean
6168
orgSlug: string
69+
file: string
6270
}
6371

6472
function setupCommand(
@@ -75,13 +83,13 @@ function setupCommand(
7583
const cli = meow(
7684
`
7785
Usage
78-
$ ${name}
86+
$ ${name} <org slug> --before=<before> --after=<after>
7987
8088
Options
8189
${printFlagList(flags, 6)}
8290
8391
Examples
84-
$ ${name}
92+
$ ${name} FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
8593
`,
8694
{
8795
argv,
@@ -97,6 +105,7 @@ function setupCommand(
97105
before,
98106
after,
99107
preview,
108+
file
100109
} = cli.flags
101110

102111
if (!before || !after) {
@@ -123,34 +132,37 @@ function setupCommand(
123132
before,
124133
after,
125134
preview,
126-
orgSlug
135+
orgSlug,
136+
file
127137
}
128138
}
129139

130140
async function getDiffScan(
131-
before: string,
132-
after: string,
141+
{ before, after, orgSlug, file }: CommandContext,
133142
spinner: Ora,
134143
apiKey: string,
135-
orgSlug: string
136144
): Promise<void> {
137145
const response = await queryAPI(`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`, apiKey)
138146
const data = await response.json();
139147

140-
if(response.status !== 200){
148+
if(!response.ok){
141149
spinner.stop()
142150
const err = await handleAPIError(response.status)
143-
console.error(err)
151+
console.error(
152+
`${chalk.bgRed.white(response.statusText)}: ${err} \n`
153+
)
144154
return
145155
}
146156

147157
spinner.stop()
148158

149-
// before: dfc4cf0c-aefd-4081-9e4e-7385257f26e2
150-
// after: 922e45f5-8a7b-4b16-95a5-e98ad00470f1
159+
if(file){
160+
fs.writeFile(file, JSON.stringify(data), err => {
161+
err ? console.error(err) : console.log(`Data successfully written to ${file}`)
162+
})
163+
return
164+
}
151165

152166
console.log(`\n Diff scan result: \n`)
153-
// console.log(data);
154-
155167
console.log(util.inspect(data, {showHidden: false, depth: null, colors: true}))
156168
}

src/utils/api-helpers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ export async function handleApiCall<T>(
4848

4949
export async function handleAPIError(code: number) {
5050
if(code === 400){
51-
return `Bad request`
51+
return `One of the options passed might be incorrect.`
52+
} else if (code === 403){
53+
return `You might be trying to access an organization that is not linked to the API key you are logged in with.`
5254
}
5355
}
5456

0 commit comments

Comments
 (0)