Skip to content

Commit b56ae70

Browse files
authored
Add command header and dry-run flag (#340)
* Add command header and dry-run flag * Disable banner by default, for now * Move dryRun check to before login check * fix report create test * Work around auth issue for other test * One more
1 parent ea0ed19 commit b56ae70

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1255
-222
lines changed

src/commands/action/cmd-action.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ const config: CliCommandConfig = {
2828
description: 'After marker'
2929
}
3030
},
31-
help: (parentName, { commandName, flags }) => `
31+
help: (command, { flags }) => `
3232
Usage
33-
$ ${parentName} ${commandName} [options]
33+
$ ${command} [options]
3434
3535
Options
3636
${getFlagListOutput(flags, 6)}
@@ -58,5 +58,7 @@ async function run(
5858
const githubEventBefore = String(cli.flags['githubEventBefore'] || '')
5959
const githubEventAfter = String(cli.flags['githubEventAfter'] || '')
6060

61+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
62+
6163
await runAction(githubEventBefore, githubEventAfter)
6264
}

src/commands/analytics/cmd-analytics.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const config: CliCommandConfig = {
4242
description: 'Path to a local file to save the output'
4343
}
4444
},
45-
help: (parentName, { commandName, flags }) => `
45+
help: (command, { flags }) => `
4646
Usage
47-
$ ${parentName} ${commandName} --scope=<scope> --time=<time filter>
47+
$ ${command} --scope=<scope> --time=<time filter>
4848
4949
Default parameters are set to show the organization-level analytics over the
5050
last 7 days.
@@ -53,9 +53,9 @@ const config: CliCommandConfig = {
5353
${getFlagListOutput(flags, 6)}
5454
5555
Examples
56-
$ ${parentName} ${commandName} --scope=org --time=7
57-
$ ${parentName} ${commandName} --scope=org --time=30
58-
$ ${parentName} ${commandName} --scope=repo --repo=test-repo --time=30
56+
$ ${command} --scope=org --time=7
57+
$ ${command} --scope=org --time=30
58+
$ ${command} --scope=repo --repo=test-repo --time=30
5959
`
6060
}
6161

@@ -89,10 +89,12 @@ async function run(
8989
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}\n
9090
- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}\n
9191
`)
92-
cli.showHelp()
92+
process.exitCode = 2 // bad input
9393
return
9494
}
9595

96+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
97+
9698
const apiToken = getDefaultToken()
9799
if (!apiToken) {
98100
throw new AuthError(

src/commands/audit-log/cmd-audit-log.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ async function run(
7272
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
7373
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
7474
`)
75-
config.help(parentName, config)
75+
process.exitCode = 2 // bad input
7676
return
7777
}
7878

79+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
80+
7981
const apiToken = getDefaultToken()
8082
if (!apiToken) {
8183
throw new AuthError(

src/commands/cdxgen/cmd-cdxgen.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import yargsParse from 'yargs-parser'
66
import { pluralize } from '@socketsecurity/registry/lib/words'
77

88
import { runCycloneDX } from './run-cyclonedx.ts'
9-
import { CliCommandConfig } from '../../utils/meow-with-subcommands.ts'
9+
import { meowOrExit } from '../../utils/meow-with-subcommands.ts'
1010
import { getFlagListOutput } from '../../utils/output-formatting.ts'
1111

12+
import type { CliCommandConfig } from '../../utils/meow-with-subcommands.ts'
13+
1214
// TODO: convert yargs to meow. Or convert all the other things to yargs.
1315
const toLower = (arg: string) => arg.toLowerCase()
1416
const arrayToLower = (arg: string[]) => arg.map(toLower)
@@ -126,16 +128,16 @@ export const cmdCdxgen = {
126128

127129
async function run(
128130
argv: readonly string[],
129-
_importMeta: ImportMeta,
130-
{ parentName: _parentName }: { parentName: string }
131+
importMeta: ImportMeta,
132+
{ parentName }: { parentName: string }
131133
): Promise<void> {
132-
// const cli = meowOrExit({
133-
// allowUnknownFlags: true,
134-
// argv,
135-
// config,
136-
// importMeta,
137-
// parentName,
138-
// })
134+
const cli = meowOrExit({
135+
allowUnknownFlags: true,
136+
argv: argv.filter(s => s !== '--help' && s !== '-h'), // Don't let meow take over --help
137+
config,
138+
importMeta,
139+
parentName
140+
})
139141
//
140142
//
141143
// if (cli.input.length)
@@ -154,16 +156,18 @@ async function run(
154156
const unknown: Array<string> = yargv._
155157
const { length: unknownLength } = unknown
156158
if (unknownLength) {
157-
process.exitCode = 1
158159
console.error(
159160
`Unknown ${pluralize('argument', unknownLength)}: ${yargv._.join(', ')}`
160161
)
162+
process.exitCode = 2 // bad input
161163
return
162164
}
163165

164166
if (yargv.output === undefined) {
165167
yargv.output = 'socket-cdx.json'
166168
}
167169

170+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
171+
168172
await runCycloneDX(yargv)
169173
}

src/commands/dependencies/cmd-dependencies.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ async function run(
5656
parentName
5757
})
5858

59+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
60+
5961
// TODO: markdown flag is ignored
6062
await findDependencies({
6163
limit: Number(cli.flags['limit'] || 0) || 0,

src/commands/diff-scan/cmd-diff-scan-get.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ async function run(
8181
- To get full scans IDs, you can run the command "socket scan list <your org slug>".
8282
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n}
8383
`)
84-
config.help(parentName, config)
84+
process.exitCode = 2 // bad input
8585
return
8686
}
8787

88+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
89+
8890
const apiToken = getDefaultToken()
8991
if (!apiToken) {
9092
throw new AuthError(

src/commands/fix/cmd-fix.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { runFix } from './run-fix.ts'
2+
import { commonFlags } from '../../flags.ts'
23
import { meowOrExit } from '../../utils/meow-with-subcommands'
34
import { getFlagListOutput } from '../../utils/output-formatting.ts'
45

@@ -8,7 +9,9 @@ const config: CliCommandConfig = {
89
commandName: 'fix',
910
description: 'Fix "fixable" Socket alerts',
1011
hidden: true,
11-
flags: {},
12+
flags: {
13+
...commonFlags
14+
},
1215
help: (command, config) => `
1316
Usage
1417
$ ${command}
@@ -29,12 +32,14 @@ async function run(
2932
importMeta: ImportMeta,
3033
{ parentName }: { parentName: string }
3134
): Promise<void> {
32-
meowOrExit({
35+
const cli = meowOrExit({
3336
argv,
3437
config,
3538
importMeta,
3639
parentName
3740
})
3841

42+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
43+
3944
await runFix()
4045
}

src/commands/info/cmd-info.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import colors from 'yoctocolors-cjs'
2+
13
import { getPackageInfo } from './get-package-info.ts'
24
import { commonFlags, outputFlags, validationFlags } from '../../flags'
3-
import { InputError } from '../../utils/errors'
45
import { meowOrExit } from '../../utils/meow-with-subcommands'
56
import { getFlagListOutput } from '../../utils/output-formatting'
67

@@ -46,24 +47,25 @@ async function run(
4647
parentName
4748
})
4849

49-
if (cli.input.length > 1) {
50-
throw new InputError('Only one package lookup supported at once')
51-
}
52-
const { 0: rawPkgName = '' } = cli.input
53-
let showHelp = cli.flags['help']
54-
if (!rawPkgName) {
55-
showHelp = true
56-
}
57-
if (showHelp) {
58-
cli.showHelp()
50+
const [rawPkgName = ''] = cli.input
51+
52+
if (!rawPkgName || cli.input.length > 1) {
53+
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
54+
- Expecting a package name ${!rawPkgName ? colors.red('(missing!)') : colors.green('(ok)')}\n
55+
- Can only accept one package at a time ${cli.input.length > 1 ? colors.red('(got ' + cli.input.length + '!)') : colors.green('(ok)')}\n
56+
`)
57+
process.exitCode = 2 // bad input
5958
return
6059
}
60+
6161
const versionSeparator = rawPkgName.lastIndexOf('@')
6262
const pkgName =
6363
versionSeparator < 1 ? rawPkgName : rawPkgName.slice(0, versionSeparator)
6464
const pkgVersion =
6565
versionSeparator < 1 ? 'latest' : rawPkgName.slice(versionSeparator + 1)
6666

67+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
68+
6769
await getPackageInfo({
6870
commandName: `${parentName} ${config.commandName}`,
6971
includeAllIssues: Boolean(cli.flags['all']),

src/commands/login/cmd-login.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import isInteractive from '@socketregistry/is-interactive/index.cjs'
22

33
import { attemptLogin } from './attempt-login.ts'
4+
import { commonFlags } from '../../flags.ts'
45
import { InputError } from '../../utils/errors'
56
import { meowOrExit } from '../../utils/meow-with-subcommands'
67
import { getFlagListOutput } from '../../utils/output-formatting'
@@ -12,6 +13,7 @@ const config: CliCommandConfig = {
1213
description: 'Socket API login',
1314
hidden: false,
1415
flags: {
16+
...commonFlags,
1517
apiBaseUrl: {
1618
type: 'string',
1719
description: 'API server to connect to for login'
@@ -54,14 +56,16 @@ async function run(
5456
parentName
5557
})
5658

59+
let apiBaseUrl = cli.flags['apiBaseUrl'] as string | undefined
60+
let apiProxy = cli.flags['apiProxy'] as string | undefined
61+
62+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
63+
5764
if (!isInteractive()) {
5865
throw new InputError(
5966
'Cannot prompt for credentials in a non-interactive shell'
6067
)
6168
}
6269

63-
let apiBaseUrl = cli.flags['apiBaseUrl'] as string | undefined
64-
let apiProxy = cli.flags['apiProxy'] as string | undefined
65-
6670
await attemptLogin(apiBaseUrl, apiProxy)
6771
}

src/commands/logout/cmd-logout.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { attemptLogout } from './attempt-logout.ts'
2+
import { commonFlags } from '../../flags.ts'
23
import { meowOrExit } from '../../utils/meow-with-subcommands'
34

45
import type { CliCommandConfig } from '../../utils/meow-with-subcommands.ts'
@@ -7,7 +8,9 @@ const config: CliCommandConfig = {
78
commandName: 'logout',
89
description: 'Socket API logout',
910
hidden: false,
10-
flags: {},
11+
flags: {
12+
...commonFlags
13+
},
1114
help: (command, _config) => `
1215
Usage
1316
$ ${command}
@@ -27,12 +30,14 @@ async function run(
2730
importMeta: ImportMeta,
2831
{ parentName }: { parentName: string }
2932
): Promise<void> {
30-
meowOrExit({
33+
const cli = meowOrExit({
3134
argv,
3235
config,
3336
importMeta,
3437
parentName
3538
})
3639

40+
if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')
41+
3742
attemptLogout()
3843
}

0 commit comments

Comments
 (0)