Skip to content

Commit f7bf1f9

Browse files
committed
Update threat-feed command to allow filtering through command args instead of flags
1 parent 5896964 commit f7bf1f9

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

src/commands/threat-feed/cmd-threat-feed.mts

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts'
1515
const { DRY_RUN_BAILING_NOW } = constants
1616

1717
const ECOSYSTEMS = new Set(['gem', 'golang', 'maven', 'npm', 'nuget', 'pypi'])
18+
const TYPE_FILTERS = new Set([
19+
'anom',
20+
'c',
21+
'fp',
22+
'joke',
23+
'mal',
24+
'secret',
25+
'spy',
26+
'tp',
27+
'typo',
28+
'u',
29+
'vuln',
30+
])
1831

1932
const config: CliCommandConfig = {
2033
commandName: 'threat-feed',
@@ -30,13 +43,11 @@ const config: CliCommandConfig = {
3043
},
3144
eco: {
3245
type: 'string',
33-
shortFlag: 'e',
3446
default: '',
3547
description: 'Only show threats for a particular ecosystem',
3648
},
3749
filter: {
3850
type: 'string',
39-
shortFlag: 'f',
4051
default: 'mal',
4152
description: 'Filter what type of threats to return',
4253
},
@@ -53,7 +64,6 @@ const config: CliCommandConfig = {
5364
},
5465
page: {
5566
type: 'string',
56-
shortFlag: 'p',
5767
default: '1',
5868
description: 'Page token',
5969
},
@@ -65,10 +75,12 @@ const config: CliCommandConfig = {
6575
},
6676
pkg: {
6777
type: 'string',
78+
default: '',
6879
description: 'Filter by this package name',
6980
},
7081
version: {
7182
type: 'string',
83+
default: '',
7284
description: 'Filter by this package version',
7385
},
7486
},
@@ -115,11 +127,20 @@ const config: CliCommandConfig = {
115127
doubt, look at the threat-feed and see the names in the name/version
116128
column. That's what you want to search for.
117129
130+
You can put filters as args instead, we'll try to match the strings with the
131+
correct filter type but since this would not allow you to search for a package
132+
called "mal", you can also specify the filters through flags.
133+
134+
First arg that matches a typo, eco, or version enum is used as such. First arg
135+
that matches none of them becomes the package name filter. Rest is ignored.
136+
137+
Note: The version filter is a prefix search, pkg name is a substring search.
138+
118139
Examples
119140
$ ${command}
120141
$ ${command} maven --json
121142
$ ${command} typo
122-
$ ${command} npm joke --perPage=5 --page=2 --direction=asc
143+
$ ${command} npm joke 1.0.0 --perPage=5 --page=2 --direction=asc
123144
`,
124145
}
125146

@@ -143,17 +164,57 @@ async function run(
143164

144165
const {
145166
dryRun,
167+
eco,
146168
interactive,
147169
json,
148170
markdown,
149171
org: orgFlag,
150172
pkg,
173+
type: typef,
151174
version,
152175
} = cli.flags
153176
const outputKind = getOutputKind(json, markdown)
154-
const [filter1 = '', filter2 = ''] = cli.input
155-
const ecoFilter = ECOSYSTEMS.has(filter1) ? filter1 : ''
156-
const typeFilter = (ecoFilter ? filter2 : filter1) || ''
177+
178+
const argSet = new Set(cli.input)
179+
let ecoFilter = String(eco || '')
180+
let versionFilter = String(version || '')
181+
let typeFilter = String(typef || '')
182+
let nameFilter = String(pkg || '')
183+
cli.input.some(str => {
184+
if (ECOSYSTEMS.has(str)) {
185+
ecoFilter = str
186+
argSet.delete(str)
187+
return true
188+
}
189+
})
190+
cli.input.some(str => {
191+
if (/^v?\d+\.\d+\.\d+$/.test(str)) {
192+
versionFilter = str
193+
argSet.delete(str)
194+
return true
195+
}
196+
})
197+
cli.input.some(str => {
198+
if (TYPE_FILTERS.has(str)) {
199+
typeFilter = str
200+
argSet.delete(str)
201+
return true
202+
}
203+
})
204+
const haves = new Set([ecoFilter, versionFilter, typeFilter])
205+
cli.input.some(str => {
206+
if (!haves.has(str)) {
207+
nameFilter = str
208+
argSet.delete(str)
209+
return true
210+
}
211+
})
212+
213+
if (argSet.size) {
214+
logger.info(
215+
`Warning: ignoring these excessive args: ${Array.from(argSet).join(', ')}`,
216+
)
217+
}
157218

158219
const [orgSlug] = await determineOrgSlug(
159220
String(orgFlag || ''),
@@ -172,14 +233,6 @@ async function run(
172233
pass: 'ok',
173234
fail: 'missing',
174235
},
175-
{
176-
nook: true,
177-
test: !!typeFilter || !filter2,
178-
message:
179-
'Second arg should only be given with first arg being a valid ecosystem',
180-
pass: 'ok',
181-
fail: 'first arg was not ecosystem and second arg received too',
182-
},
183236
{
184237
nook: true,
185238
test: !json || !markdown,
@@ -213,7 +266,7 @@ async function run(
213266
orgSlug,
214267
page: String(cli.flags['page'] || '1'),
215268
perPage: Number(cli.flags['perPage']) || 30,
216-
pkg: String(pkg || ''),
217-
version: String(version || ''),
269+
pkg: nameFilter,
270+
version: versionFilter,
218271
})
219272
}

src/commands/threat-feed/cmd-threat-feed.test.mts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,20 @@ describe('socket threat-feed', async () => {
6868
doubt, look at the threat-feed and see the names in the name/version
6969
column. That's what you want to search for.
7070
71+
You can put filters as args instead, we'll try to match the strings with the
72+
correct filter type but since this would not allow you to search for a package
73+
called "mal", you can also specify the filters through flags.
74+
75+
First arg that matches a typo, eco, or version enum is used as such. First arg
76+
that matches none of them becomes the package name filter. Rest is ignored.
77+
78+
Note: The version filter is a prefix search, pkg name is a substring search.
79+
7180
Examples
7281
$ socket threat-feed
7382
$ socket threat-feed maven --json
7483
$ socket threat-feed typo
75-
$ socket threat-feed npm joke --perPage=5 --page=2 --direction=asc"
84+
$ socket threat-feed npm joke 1.0.0 --perPage=5 --page=2 --direction=asc"
7685
`,
7786
)
7887
expect(`\n ${stderr}`).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)