-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcmd-config-set.ts
96 lines (80 loc) · 2.48 KB
/
cmd-config-set.ts
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
import { logger } from '@socketsecurity/registry/lib/logger'
import { handleConfigSet } from './handle-config-set'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { supportedConfigKeys } from '../../utils/config'
import { handleBadInput } from '../../utils/handle-bad-input'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
import type { LocalConfig } from '../../utils/config'
import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
const { DRY_RUN_BAIL_TEXT } = constants
const config: CliCommandConfig = {
commandName: 'set',
description: 'Update the value of a local CLI config item',
hidden: false,
flags: {
...commonFlags,
...outputFlags
},
help: (command, config) => `
Usage
$ ${command} <key> <value>
Options
${getFlagListOutput(config.flags, 6)}
This is a crude way of updating the local configuration for this CLI tool.
Note that updating a value here is nothing more than updating a key/value
store entry. No validation is happening. The server may reject your config.
Keys:
${Array.from(supportedConfigKeys.entries())
.map(([key, desc]) => ` - ${key} -- ${desc}`)
.join('\n')}
Examples
$ ${command} apiProxy https://example.com
`
}
export const cmdConfigSet = {
description: config.description,
hidden: config.hidden,
run
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})
const { json, markdown } = cli.flags
const [key = '', ...rest] = cli.input
const value = rest.join(' ')
const wasBadInput = handleBadInput(
{
test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test',
message: 'Config key should be the first arg',
pass: 'ok',
fail: key ? 'invalid config key' : 'missing'
},
{
test: value, // This is a string, empty string is not ok
message:
'Key value should be the remaining args (use `unset` to unset a value)',
pass: 'ok',
fail: 'missing'
}
)
if (wasBadInput) return
if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}
await handleConfigSet({
key: key as keyof LocalConfig,
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
value
})
}