-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcmd-scan-diff.mts
More file actions
197 lines (174 loc) · 5.31 KB
/
Copy pathcmd-scan-diff.mts
File metadata and controls
197 lines (174 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { handleDiffScan } from './handle-diff-scan.mts'
import { FLAG_JSON, FLAG_MARKDOWN } from '../../constants/cli.mts'
import { outputDryRunFetch } from '../../util/dry-run/output.mts'
import { SOCKET_WEBSITE_URL } from '../../constants/socket.mts'
import { defineFlags } from '../../meow.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { meowOrExit } from '../../util/cli/with-subcommands.mjs'
import {
getFlagApiRequirementsOutput,
getFlagListOutput,
} from '../../util/output/formatting.mts'
import { getOutputKind } from '../../util/output/mode.mjs'
import { determineOrgSlug } from '../../util/socket/org-slug.mjs'
import { hasDefaultApiToken } from '../../util/socket/sdk.mjs'
import { checkCommandInput } from '../../util/validation/check-input.mts'
import type { CliCommandContext } from '../../util/cli/with-subcommands.mjs'
import type { MeowFlags } from '../../flags.mts'
// Flags interface for type safety.
export interface ScanDiffFlags {
depth: number
dryRun: boolean
file: string
json: boolean
markdown: boolean
org: string
}
export const CMD_NAME = 'diff'
const description = 'See what changed between two Scans'
const hidden = false
export const cmdScanDiff = {
description,
hidden,
run,
}
export async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: CliCommandContext,
): Promise<void> {
const config = {
commandName: CMD_NAME,
description,
hidden,
flags: defineFlags({
...commonFlags,
...outputFlags,
depth: {
type: 'number',
default: 2,
description:
'Max depth of JSON to display before truncating, use zero for no limit (without --json/--file)',
},
file: {
type: 'string',
shortFlag: 'f',
default: '',
description:
'Path to a local file where the output should be saved. Use `-` to force stdout.',
},
interactive: {
type: 'boolean',
default: true,
description:
'Allow for interactive elements, asking for input. Use --no-interactive to prevent any input questions, defaulting them to cancel/no.',
},
org: {
type: 'string',
description:
'Force override the organization slug, overrides the default org from config',
},
}),
help: (command: string, helpConfig: { flags: MeowFlags }) => `
Usage
$ ${command} [options] <SCAN_ID1> <SCAN_ID2>
API Token Requirements
${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)}
This command displays the package changes between two scans. The full output
can be pretty large depending on the size of your repo and time range. It is
best stored to disk (with --json) to be further analyzed by other tools.
Note: While it will work in any order, the first Scan ID is assumed to be the
older ID, even if it is a newer Scan. This is only relevant for the
added/removed list (similar to diffing two files with git).
Options
${getFlagListOutput(helpConfig.flags)}
Examples
$ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
$ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json
`,
}
const cli = meowOrExit({
argv,
config,
importMeta,
parentName,
})
const SOCKET_SBOM_URL_PREFIX = `${SOCKET_WEBSITE_URL}/dashboard/org/SocketDev/sbom/`
const SOCKET_SBOM_URL_PREFIX_LENGTH = SOCKET_SBOM_URL_PREFIX.length
const {
depth,
dryRun,
file,
json,
markdown,
org: orgFlag,
} = cli.flags as unknown as ScanDiffFlags
const interactive = cli.flags['interactive']
let { 0: id1 = '', 1: id2 = '' } = cli.input
// Support dropping in full socket urls to an sbom.
if (id1.startsWith(SOCKET_SBOM_URL_PREFIX)) {
id1 = id1.slice(SOCKET_SBOM_URL_PREFIX_LENGTH)
}
if (id2.startsWith(SOCKET_SBOM_URL_PREFIX)) {
id2 = id2.slice(SOCKET_SBOM_URL_PREFIX_LENGTH)
}
const hasApiToken = hasDefaultApiToken()
const { 0: orgSlug } = await determineOrgSlug(
orgFlag || '',
interactive,
dryRun,
)
const outputKind = getOutputKind(json, markdown)
const wasValidInput = checkCommandInput(
outputKind,
{
test: !!(id1 && id2),
message:
'Specify two Scan IDs.\nA Scan ID looks like `aaa0aa0a-aaaa-0000-0a0a-0000000a00a0`.',
fail:
!id1 && !id2
? 'missing both Scan IDs'
: !id1
? 'missing first Scan ID'
: 'missing second Scan ID',
},
{
test: !!orgSlug,
nook: true,
message: 'Org name by default setting, --org, or auto-discovered',
fail: 'missing',
},
{
nook: true,
test: !json || !markdown,
message: `The \`${FLAG_JSON}\` and \`${FLAG_MARKDOWN}\` flags can not be used at the same time`,
fail: 'bad',
},
{
nook: true,
test: hasApiToken,
message: 'This command requires a Socket API token for access',
fail: 'try `socket login`',
},
)
if (!wasValidInput) {
return
}
if (dryRun) {
outputDryRunFetch('scan differences', {
organization: orgSlug,
scanId1: id1,
scanId2: id2,
depth,
})
return
}
await handleDiffScan({
id1,
id2,
depth,
orgSlug,
outputKind,
file,
})
}