Skip to content

Commit 2e54edf

Browse files
committed
Use getConfigValueOrUndef in constants
1 parent fada95d commit 2e54edf

File tree

10 files changed

+172
-128
lines changed

10 files changed

+172
-128
lines changed

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

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { handleConfigAuto } from './handle-config-auto.mts'
44
import constants from '../../constants.mts'
55
import { commonFlags, outputFlags } from '../../flags.mts'
66
import { checkCommandInput } from '../../utils/check-input.mts'
7-
import { supportedConfigKeys } from '../../utils/config.mts'
7+
import {
8+
getSupportedConfigEntries,
9+
isSupportedConfigKey,
10+
} from '../../utils/config.mts'
811
import { getOutputKind } from '../../utils/get-output-kind.mts'
912
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
1013
import { getFlagListOutput } from '../../utils/output-formatting.mts'
@@ -14,54 +17,48 @@ import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts'
1417

1518
const { DRY_RUN_BAILING_NOW } = constants
1619

17-
const config: CliCommandConfig = {
18-
commandName: 'auto',
19-
description: 'Automatically discover and set the correct value config item',
20-
hidden: false,
21-
flags: {
22-
...commonFlags,
23-
...outputFlags,
24-
},
25-
help: (command, config) => `
20+
const description =
21+
'Automatically discover and set the correct value config item'
22+
const hidden = false
23+
24+
export const cmdConfigAuto = {
25+
description,
26+
hidden,
27+
run,
28+
}
29+
30+
async function run(
31+
argv: string[] | readonly string[],
32+
importMeta: ImportMeta,
33+
{ parentName }: { parentName: string },
34+
): Promise<void> {
35+
const config: CliCommandConfig = {
36+
commandName: 'auto',
37+
description,
38+
hidden,
39+
flags: {
40+
...commonFlags,
41+
...outputFlags,
42+
},
43+
help: (command, config) => `
2644
Usage
2745
$ ${command} [options] KEY
2846
2947
Options
3048
${getFlagListOutput(config.flags)}
3149
32-
Attempt to automatically discover the correct value for given config KEY.
33-
34-
Keys:
35-
36-
${Array.from(supportedConfigKeys.entries())
37-
.map(([key, desc]) => ` - ${key} -- ${desc}`)
38-
.join('\n')}
50+
Attempt to automatically discover the correct value for a given config KEY.
3951
40-
For certain keys it will request the value from server, for others it will
41-
reset the value to the default. For some keys this has no effect.
52+
Examples
53+
$ ${command} defaultOrg
4254
4355
Keys:
44-
45-
${Array.from(supportedConfigKeys.entries())
56+
${getSupportedConfigEntries()
4657
.map(([key, desc]) => ` - ${key} -- ${desc}`)
4758
.join('\n')}
48-
49-
Examples
50-
$ ${command} defaultOrg
5159
`,
52-
}
53-
54-
export const cmdConfigAuto = {
55-
description: config.description,
56-
hidden: config.hidden,
57-
run,
58-
}
60+
}
5961

60-
async function run(
61-
argv: string[] | readonly string[],
62-
importMeta: ImportMeta,
63-
{ parentName }: { parentName: string },
64-
): Promise<void> {
6562
const cli = meowOrExit({
6663
argv,
6764
config,
@@ -78,7 +75,7 @@ async function run(
7875
const wasValidInput = checkCommandInput(
7976
outputKind,
8077
{
81-
test: supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test',
78+
test: key !== 'test' && isSupportedConfigKey(key),
8279
message: 'Config key should be the first arg',
8380
pass: 'ok',
8481
fail: key ? 'invalid config key' : 'missing',

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { handleConfigGet } from './handle-config-get.mts'
44
import constants from '../../constants.mts'
55
import { commonFlags, outputFlags } from '../../flags.mts'
66
import { checkCommandInput } from '../../utils/check-input.mts'
7-
import { supportedConfigKeys } from '../../utils/config.mts'
7+
import {
8+
getSupportedConfigEntries,
9+
isSupportedConfigKey,
10+
} from '../../utils/config.mts'
811
import { getOutputKind } from '../../utils/get-output-kind.mts'
912
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
1013
import { getFlagListOutput } from '../../utils/output-formatting.mts'
@@ -34,7 +37,7 @@ const config: CliCommandConfig = {
3437
3538
KEY is an enum. Valid keys:
3639
37-
${Array.from(supportedConfigKeys.entries())
40+
${getSupportedConfigEntries()
3841
.map(([key, desc]) => ` - ${key} -- ${desc}`)
3942
.join('\n')}
4043
@@ -70,7 +73,7 @@ async function run(
7073
const wasValidInput = checkCommandInput(
7174
outputKind,
7275
{
73-
test: supportedConfigKeys.has(key as keyof LocalConfig) || key === 'test',
76+
test: key === 'test' || isSupportedConfigKey(key),
7477
message: 'Config key should be the first arg',
7578
pass: 'ok',
7679
fail: key ? 'invalid config key' : 'missing',

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { handleConfigSet } from './handle-config-set.mts'
44
import constants from '../../constants.mts'
55
import { commonFlags, outputFlags } from '../../flags.mts'
66
import { checkCommandInput } from '../../utils/check-input.mts'
7-
import { supportedConfigKeys } from '../../utils/config.mts'
7+
import {
8+
getSupportedConfigEntries,
9+
isSupportedConfigKey,
10+
} from '../../utils/config.mts'
811
import { getOutputKind } from '../../utils/get-output-kind.mts'
912
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
1013
import { getFlagListOutput } from '../../utils/output-formatting.mts'
@@ -14,15 +17,29 @@ import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts'
1417

1518
const { DRY_RUN_BAILING_NOW } = constants
1619

17-
const config: CliCommandConfig = {
18-
commandName: 'set',
19-
description: 'Update the value of a local CLI config item',
20-
hidden: false,
21-
flags: {
22-
...commonFlags,
23-
...outputFlags,
24-
},
25-
help: (command, config) => `
20+
const description = 'Update the value of a local CLI config item'
21+
const hidden = false
22+
23+
export const cmdConfigSet = {
24+
description,
25+
hidden,
26+
run,
27+
}
28+
29+
async function run(
30+
argv: string[] | readonly string[],
31+
importMeta: ImportMeta,
32+
{ parentName }: { parentName: string },
33+
): Promise<void> {
34+
const config: CliCommandConfig = {
35+
commandName: 'set',
36+
description,
37+
hidden,
38+
flags: {
39+
...commonFlags,
40+
...outputFlags,
41+
},
42+
help: (command, config) => `
2643
Usage
2744
$ ${command} [options] <KEY> <VALUE>
2845
@@ -40,26 +57,15 @@ const config: CliCommandConfig = {
4057
4158
Keys:
4259
43-
${Array.from(supportedConfigKeys.entries())
60+
${getSupportedConfigEntries()
4461
.map(([key, desc]) => ` - ${key} -- ${desc}`)
4562
.join('\n')}
4663
4764
Examples
4865
$ ${command} apiProxy https://example.com
4966
`,
50-
}
51-
52-
export const cmdConfigSet = {
53-
description: config.description,
54-
hidden: config.hidden,
55-
run,
56-
}
67+
}
5768

58-
async function run(
59-
argv: string[] | readonly string[],
60-
importMeta: ImportMeta,
61-
{ parentName }: { parentName: string },
62-
): Promise<void> {
6369
const cli = meowOrExit({
6470
argv,
6571
config,
@@ -78,7 +84,7 @@ async function run(
7884
const wasValidInput = checkCommandInput(
7985
outputKind,
8086
{
81-
test: key === 'test' || supportedConfigKeys.has(key as keyof LocalConfig),
87+
test: key === 'test' || isSupportedConfigKey(key),
8288
message: 'Config key should be the first arg',
8389
pass: 'ok',
8490
fail: key ? 'invalid config key' : 'missing',

src/commands/config/cmd-config-unset.mts

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { handleConfigUnset } from './handle-config-unset.mts'
44
import constants from '../../constants.mts'
55
import { commonFlags, outputFlags } from '../../flags.mts'
66
import { checkCommandInput } from '../../utils/check-input.mts'
7-
import { supportedConfigKeys } from '../../utils/config.mts'
7+
import {
8+
getSupportedConfigEntries,
9+
isSupportedConfigKey,
10+
} from '../../utils/config.mts'
811
import { getOutputKind } from '../../utils/get-output-kind.mts'
912
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
1013
import { getFlagListOutput } from '../../utils/output-formatting.mts'
@@ -14,15 +17,29 @@ import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts'
1417

1518
const { DRY_RUN_BAILING_NOW } = constants
1619

17-
const config: CliCommandConfig = {
18-
commandName: 'unset',
19-
description: 'Clear the value of a local CLI config item',
20-
hidden: false,
21-
flags: {
22-
...commonFlags,
23-
...outputFlags,
24-
},
25-
help: (command, config) => `
20+
const description = 'Clear the value of a local CLI config item'
21+
const hidden = false
22+
23+
export const cmdConfigUnset = {
24+
description,
25+
hidden,
26+
run,
27+
}
28+
29+
async function run(
30+
argv: string[] | readonly string[],
31+
importMeta: ImportMeta,
32+
{ parentName }: { parentName: string },
33+
): Promise<void> {
34+
const config: CliCommandConfig = {
35+
commandName: 'unset',
36+
description,
37+
hidden,
38+
flags: {
39+
...commonFlags,
40+
...outputFlags,
41+
},
42+
help: (command, config) => `
2643
Usage
2744
$ ${command} [options] <KEY> <VALUE>
2845
@@ -34,26 +51,15 @@ const config: CliCommandConfig = {
3451
3552
Keys:
3653
37-
${Array.from(supportedConfigKeys.entries())
54+
${getSupportedConfigEntries()
3855
.map(([key, desc]) => ` - ${key} -- ${desc}`)
3956
.join('\n')}
4057
4158
Examples
4259
$ ${command} defaultOrg
4360
`,
44-
}
45-
46-
export const cmdConfigUnset = {
47-
description: config.description,
48-
hidden: config.hidden,
49-
run,
50-
}
61+
}
5162

52-
async function run(
53-
argv: string[] | readonly string[],
54-
importMeta: ImportMeta,
55-
{ parentName }: { parentName: string },
56-
): Promise<void> {
5763
const cli = meowOrExit({
5864
argv,
5965
config,
@@ -70,7 +76,7 @@ async function run(
7076
const wasValidInput = checkCommandInput(
7177
outputKind,
7278
{
73-
test: key === 'test' || supportedConfigKeys.has(key as keyof LocalConfig),
79+
test: key === 'test' || isSupportedConfigKey(key),
7480
message: 'Config key should be the first arg',
7581
pass: 'ok',
7682
fail: key ? 'invalid config key' : 'missing',

src/commands/config/discover-config-value.mts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { handleApiCall } from '../../utils/api.mts'
2-
import { supportedConfigKeys } from '../../utils/config.mts'
2+
import { isSupportedConfigKey } from '../../utils/config.mts'
33
import { hasDefaultToken, setupSdk } from '../../utils/sdk.mts'
44

55
import type { CResult } from '../../types.mts'
6-
import type { LocalConfig } from '../../utils/config.mts'
76

87
export async function discoverConfigValue(
98
key: string,
@@ -12,7 +11,7 @@ export async function discoverConfigValue(
1211
// keys should request information from particular API endpoints while
1312
// others should simply return their default value, like endpoint URL.
1413

15-
if (!supportedConfigKeys.has(key as keyof LocalConfig)) {
14+
if (key !== 'test' && !isSupportedConfigKey(key)) {
1615
return {
1716
ok: false,
1817
message: 'Auto discover failed',

src/commands/config/output-config-list.mts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { logger } from '@socketsecurity/registry/lib/logger'
22

33
import {
44
getConfigValue,
5+
getSupportedConfigKeys,
56
isReadOnlyConfig,
6-
sensitiveConfigKeys,
7-
supportedConfigKeys,
7+
isSensitiveConfigKey,
88
} from '../../utils/config.mts'
99
import { serializeResultJson } from '../../utils/serialize-result-json.mts'
1010

@@ -18,16 +18,17 @@ export async function outputConfigList({
1818
outputKind: OutputKind
1919
}) {
2020
const readOnly = isReadOnlyConfig()
21+
const supportedConfigKeys = getSupportedConfigKeys()
2122
if (outputKind === 'json') {
2223
let failed = false
2324
const obj: Record<string, unknown> = {}
24-
for (const key of supportedConfigKeys.keys()) {
25+
for (const key of supportedConfigKeys) {
2526
const result = getConfigValue(key)
2627
let value = result.data
2728
if (!result.ok) {
2829
value = `Failed to retrieve: ${result.message}`
2930
failed = true
30-
} else if (!full && sensitiveConfigKeys.has(key)) {
31+
} else if (!full && isSensitiveConfigKey(key)) {
3132
value = '********'
3233
}
3334
if (full || value !== undefined) {
@@ -60,7 +61,7 @@ export async function outputConfigList({
6061
),
6162
)
6263
} else {
63-
const maxWidth = Array.from(supportedConfigKeys.keys()).reduce(
64+
const maxWidth = supportedConfigKeys.reduce(
6465
(a, b) => Math.max(a, b.length),
6566
0,
6667
)
@@ -69,13 +70,13 @@ export async function outputConfigList({
6970
logger.log('')
7071
logger.log(`This is the local CLI config (full=${!!full}):`)
7172
logger.log('')
72-
for (const key of supportedConfigKeys.keys()) {
73+
for (const key of supportedConfigKeys) {
7374
const result = getConfigValue(key)
7475
if (!result.ok) {
7576
logger.log(`- ${key}: failed to read: ${result.message}`)
7677
} else {
7778
let value = result.data
78-
if (!full && sensitiveConfigKeys.has(key)) {
79+
if (!full && isSensitiveConfigKey(key)) {
7980
value = '********'
8081
}
8182
if (full || value !== undefined) {

0 commit comments

Comments
 (0)