Skip to content

Commit 9548309

Browse files
pvdzjdalton
authored andcommitted
Abstract bad input reporter to make it more consistent (#384)
1 parent 2f34aae commit 9548309

Some content is hidden

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

52 files changed

+648
-606
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ describe('socket audit-log', async () => {
6767
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
6868
|_____|___|___|_,_|___|_|.dev | Command: \`socket audit-log\`, cwd: <redacted>
6969
70-
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields:
70+
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m:
7171
72-
- Org name as the first argument \\x1b[31m(missing!)\\x1b[39m"
72+
- Org name should be the first arg (\\x1b[31mmissing\\x1b[39m)"
7373
`)
7474

7575
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { stripIndents } from 'common-tags'
2-
import colors from 'yoctocolors-cjs'
3-
41
import { logger } from '@socketsecurity/registry/lib/logger'
52

63
import { handleAuditLog } from './handle-audit-log'
74
import constants from '../../constants'
85
import { commonFlags, outputFlags } from '../../flags'
96
import { getConfigValue } from '../../utils/config'
7+
import { handleBadInput } from '../../utils/handle-bad-input'
108
import { meowOrExit } from '../../utils/meow-with-subcommands'
119
import { getFlagListOutput } from '../../utils/output-formatting'
1210

@@ -79,19 +77,13 @@ async function run(
7977
const defaultOrgSlug = getConfigValue('defaultOrg')
8078
const orgSlug = defaultOrgSlug || cli.input[0] || ''
8179

82-
if (!orgSlug) {
83-
// Use exit status of 2 to indicate incorrect usage, generally invalid
84-
// options or missing arguments.
85-
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
86-
process.exitCode = 2
87-
logger.fail(
88-
stripIndents`
89-
${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
90-
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}
91-
`
92-
)
93-
return
94-
}
80+
const waswasInput = handleBadInput({
81+
test: orgSlug,
82+
message: 'Org name should be the first arg',
83+
pass: 'ok',
84+
fail: 'missing'
85+
})
86+
if (waswasInput) return
9587

9688
if (cli.flags['dryRun']) {
9789
logger.log(DRY_RUN_BAIL_TEXT)

src/commands/config/cmd-config-auto.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { stripIndents } from 'common-tags'
2-
import colors from 'yoctocolors-cjs'
3-
41
import { logger } from '@socketsecurity/registry/lib/logger'
52

63
import { handleConfigAuto } from './handle-config-auto'
74
import constants from '../../constants'
85
import { commonFlags, outputFlags } from '../../flags'
96
import { supportedConfigKeys } from '../../utils/config'
7+
import { handleBadInput } from '../../utils/handle-bad-input'
108
import { meowOrExit } from '../../utils/meow-with-subcommands'
119
import { getFlagListOutput } from '../../utils/output-formatting'
1210

@@ -67,17 +65,13 @@ async function run(
6765
const { json, markdown } = cli.flags
6866
const [key = ''] = cli.input
6967

70-
if (!supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test') {
71-
// Use exit status of 2 to indicate incorrect usage, generally invalid
72-
// options or missing arguments.
73-
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
74-
process.exitCode = 2
75-
logger.fail(stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
76-
77-
- Config key should be the first arg ${!key ? colors.red('(missing!)') : !supportedConfigKeys.has(key as any) ? colors.red('(invalid config key!)') : colors.green('(ok)')}
78-
`)
79-
return
80-
}
68+
const wasBadInput = handleBadInput({
69+
test: supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test',
70+
message: 'Config key should be the first arg',
71+
pass: 'ok',
72+
fail: key ? 'invalid config key' : 'missing'
73+
})
74+
if (wasBadInput) return
8175

8276
if (cli.flags['dryRun']) {
8377
logger.log(DRY_RUN_BAIL_TEXT)

src/commands/config/cmd-config-get.test.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ describe('socket config get', async () => {
1818
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
1919
expect(stdout).toMatchInlineSnapshot(
2020
`
21-
"Get the value of a local CLI config item
21+
"Get the value of a local CLI config item
2222
23-
Usage
24-
$ socket config get <org slug>
23+
Usage
24+
$ socket config get <org slug>
2525
26-
Options
27-
--dryRun Do input validation for a command and exit 0 when input is ok
28-
--help Print this help.
29-
--json Output result as json
30-
--markdown Output result as markdown
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--help Print this help.
29+
--json Output result as json
30+
--markdown Output result as markdown
3131
32-
Keys:
32+
Keys:
3333
34-
- apiBaseUrl -- Base URL of the API endpoint
35-
- apiProxy -- A proxy through which to access the API
36-
- apiToken -- The API token required to access most API endpoints
37-
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
38-
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
34+
- apiBaseUrl -- Base URL of the API endpoint
35+
- apiProxy -- A proxy through which to access the API
36+
- apiToken -- The API token required to access most API endpoints
37+
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
38+
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
3939
40-
Examples
41-
$ socket config get FakeOrg --repoName=test-repo"
42-
`
40+
Examples
41+
$ socket config get FakeOrg --repoName=test-repo"
42+
`
4343
)
4444
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
4545
"
@@ -69,27 +69,29 @@ describe('socket config get', async () => {
6969
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
7070
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>
7171
72-
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields:
72+
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m:
7373
74-
- Config key should be the first arg \\x1b[31m(missing!)\\x1b[39m"
74+
- Config key should be the first arg (\\x1b[31mmissing\\x1b[39m)"
7575
`)
7676

7777
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)
7878
}
7979
)
8080

8181
cmdit(
82-
['config', 'get', 'test', '--dry-run', '--config', '{}'],
82+
['config', 'test', 'test', '--dry-run', '--config', '{}'],
8383
'should require args with just dry-run',
8484
async cmd => {
8585
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
86-
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
86+
expect(stdout).toMatchInlineSnapshot(
87+
`"[DryRun]: No-op, call a sub-command; ok"`
88+
)
8789
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
8890
"
8991
_____ _ _ /---------------
9092
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
9193
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
92-
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
94+
|_____|___|___|_,_|___|_|.dev | Command: \`socket config\`, cwd: <redacted>"
9395
`)
9496

9597
expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)

src/commands/config/cmd-config-get.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { stripIndents } from 'common-tags'
2-
import colors from 'yoctocolors-cjs'
3-
41
import { logger } from '@socketsecurity/registry/lib/logger'
52

63
import { handleConfigGet } from './handle-config-get'
74
import constants from '../../constants'
85
import { commonFlags, outputFlags } from '../../flags'
96
import { supportedConfigKeys } from '../../utils/config'
7+
import { handleBadInput } from '../../utils/handle-bad-input'
108
import { meowOrExit } from '../../utils/meow-with-subcommands'
119
import { getFlagListOutput } from '../../utils/output-formatting'
1210

@@ -62,17 +60,13 @@ async function run(
6260
const { json, markdown } = cli.flags
6361
const [key = ''] = cli.input
6462

65-
if (!supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test') {
66-
// Use exit status of 2 to indicate incorrect usage, generally invalid
67-
// options or missing arguments.
68-
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
69-
process.exitCode = 2
70-
logger.fail(stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
71-
72-
- Config key should be the first arg ${!key ? colors.red('(missing!)') : !supportedConfigKeys.has(key as any) ? colors.red('(invalid config key!)') : colors.green('(ok)')}
73-
`)
74-
return
75-
}
63+
const wasBadInput = handleBadInput({
64+
test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test',
65+
message: 'Config key should be the first arg',
66+
pass: 'ok',
67+
fail: key ? 'invalid config key' : 'missing'
68+
})
69+
if (wasBadInput) return
7670

7771
if (cli.flags['dryRun']) {
7872
logger.log(DRY_RUN_BAIL_TEXT)

src/commands/config/cmd-config-list.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,29 @@ describe('socket config get', async () => {
1818
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
1919
expect(stdout).toMatchInlineSnapshot(
2020
`
21-
"Show all local CLI config items and their values
21+
"Show all local CLI config items and their values
2222
23-
Usage
24-
$ socket config list <org slug>
23+
Usage
24+
$ socket config list <org slug>
2525
26-
Options
27-
--dryRun Do input validation for a command and exit 0 when input is ok
28-
--full Show full tokens in plaintext (unsafe)
29-
--help Print this help.
30-
--json Output result as json
31-
--markdown Output result as markdown
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--full Show full tokens in plaintext (unsafe)
29+
--help Print this help.
30+
--json Output result as json
31+
--markdown Output result as markdown
3232
33-
Keys:
33+
Keys:
3434
35-
- apiBaseUrl -- Base URL of the API endpoint
36-
- apiProxy -- A proxy through which to access the API
37-
- apiToken -- The API token required to access most API endpoints
38-
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
39-
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
35+
- apiBaseUrl -- Base URL of the API endpoint
36+
- apiProxy -- A proxy through which to access the API
37+
- apiToken -- The API token required to access most API endpoints
38+
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
39+
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
4040
41-
Examples
42-
$ socket config list FakeOrg --repoName=test-repo"
43-
`
41+
Examples
42+
$ socket config list FakeOrg --repoName=test-repo"
43+
`
4444
)
4545
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
4646
"

src/commands/config/cmd-config-set.test.ts

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,33 @@ describe('socket config get', async () => {
1818
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
1919
expect(stdout).toMatchInlineSnapshot(
2020
`
21-
"Update the value of a local CLI config item
21+
"Update the value of a local CLI config item
2222
23-
Usage
24-
$ socket config set <key> <value>
23+
Usage
24+
$ socket config set <key> <value>
2525
26-
Options
27-
--dryRun Do input validation for a command and exit 0 when input is ok
28-
--help Print this help.
29-
--json Output result as json
30-
--markdown Output result as markdown
26+
Options
27+
--dryRun Do input validation for a command and exit 0 when input is ok
28+
--help Print this help.
29+
--json Output result as json
30+
--markdown Output result as markdown
3131
32-
This is a crude way of updating the local configuration for this CLI tool.
32+
This is a crude way of updating the local configuration for this CLI tool.
3333
34-
Note that updating a value here is nothing more than updating a key/value
35-
store entry. No validation is happening. The server may reject your config.
34+
Note that updating a value here is nothing more than updating a key/value
35+
store entry. No validation is happening. The server may reject your config.
3636
37-
Keys:
37+
Keys:
3838
39-
- apiBaseUrl -- Base URL of the API endpoint
40-
- apiProxy -- A proxy through which to access the API
41-
- apiToken -- The API token required to access most API endpoints
42-
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
43-
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
39+
- apiBaseUrl -- Base URL of the API endpoint
40+
- apiProxy -- A proxy through which to access the API
41+
- apiToken -- The API token required to access most API endpoints
42+
- defaultOrg -- The default org slug to use when appropriate; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
43+
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
4444
45-
Examples
46-
$ socket config set apiProxy https://example.com"
47-
`
45+
Examples
46+
$ socket config set apiProxy https://example.com"
47+
`
4848
)
4949
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
5050
"
@@ -74,11 +74,11 @@ describe('socket config get', async () => {
7474
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
7575
|_____|___|___|_,_|___|_|.dev | Command: \`socket config set\`, cwd: <redacted>
7676
77-
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields:
77+
\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[1m\\x1b[37m Input error: \\x1b[39m\\x1b[22m\\x1b[49m \\x1b[1mPlease review the input requirements and try again\\x1b[22m:
7878
79-
- Config key should be the first arg \\x1b[31m(missing!)\\x1b[39m
79+
- Config key should be the first arg (\\x1b[31mmissing\\x1b[39m)
8080
81-
- Key value should be the remaining args (use \`del\` to unset a value) \\x1b[31m(missing!)\\x1b[39m"
81+
- Key value should be the remaining args (use \`unset\` to unset a value) (\\x1b[31mmissing\\x1b[39m)"
8282
`)
8383

8484
expect(code, 'dry-run should exit with code 2 if missing input').toBe(2)

src/commands/config/cmd-config-set.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import { stripIndents } from 'common-tags'
2-
import colors from 'yoctocolors-cjs'
3-
41
import { logger } from '@socketsecurity/registry/lib/logger'
52

63
import { handleConfigSet } from './handle-config-set'
74
import constants from '../../constants'
85
import { commonFlags, outputFlags } from '../../flags'
96
import { supportedConfigKeys } from '../../utils/config'
7+
import { handleBadInput } from '../../utils/handle-bad-input'
108
import { meowOrExit } from '../../utils/meow-with-subcommands'
119
import { getFlagListOutput } from '../../utils/output-formatting'
1210

@@ -68,21 +66,22 @@ async function run(
6866
const [key = '', ...rest] = cli.input
6967
const value = rest.join(' ')
7068

71-
if (
72-
(!supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test') ||
73-
!value
74-
) {
75-
// Use exit status of 2 to indicate incorrect usage, generally invalid
76-
// options or missing arguments.
77-
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
78-
process.exitCode = 2
79-
logger.fail(stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:
80-
81-
- Config key should be the first arg ${!key ? colors.red('(missing!)') : !supportedConfigKeys.has(key as any) ? colors.red('(invalid config key!)') : colors.green('(ok)')}
82-
83-
- Key value should be the remaining args (use \`del\` to unset a value) ${!value ? colors.red('(missing!)') : colors.green('(ok)')}`)
84-
return
85-
}
69+
const wasBadInput = handleBadInput(
70+
{
71+
test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test',
72+
message: 'Config key should be the first arg',
73+
pass: 'ok',
74+
fail: key ? 'invalid config key' : 'missing'
75+
},
76+
{
77+
test: value, // This is a string, empty string is not ok
78+
message:
79+
'Key value should be the remaining args (use `unset` to unset a value)',
80+
pass: 'ok',
81+
fail: 'missing'
82+
}
83+
)
84+
if (wasBadInput) return
8685

8786
if (cli.flags['dryRun']) {
8887
logger.log(DRY_RUN_BAIL_TEXT)

0 commit comments

Comments
 (0)