Skip to content

Commit 1377c5f

Browse files
authored
When selecting org, ask to persist it (#664)
1 parent 61215e9 commit 1377c5f

File tree

10 files changed

+95
-8
lines changed

10 files changed

+95
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('socket config auto', async () => {
3232
- apiToken -- The API token required to access most API endpoints
3333
- defaultOrg -- The default org slug to use; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
3434
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
35+
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
3536
- org -- Alias for defaultOrg
3637
3738
For certain keys it will request the value from server, for others it will
@@ -44,6 +45,7 @@ describe('socket config auto', async () => {
4445
- apiToken -- The API token required to access most API endpoints
4546
- defaultOrg -- The default org slug to use; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
4647
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
48+
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
4749
- org -- Alias for defaultOrg
4850
4951
Examples

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('socket config get', async () => {
3434
- apiToken -- The API token required to access most API endpoints
3535
- defaultOrg -- The default org slug to use; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
3636
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
37+
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
3738
- org -- Alias for defaultOrg
3839
3940
Examples

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ describe('socket config get', async () => {
4141
- apiToken -- The API token required to access most API endpoints
4242
- defaultOrg -- The default org slug to use; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
4343
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
44+
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
4445
- org -- Alias for defaultOrg
4546
4647
Examples

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('socket config unset', async () => {
3535
- apiToken -- The API token required to access most API endpoints
3636
- defaultOrg -- The default org slug to use; usually the org your API token has access to. When set, all orgSlug arguments are implied to be this value.
3737
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
38+
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
3839
- org -- Alias for defaultOrg
3940
4041
Examples

src/commands/repository/output-list-repos.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ export async function outputListRepos(
6767
logger.info(
6868
`This is page ${page}. Server indicated there are more results available on page ${nextPage}...`,
6969
)
70-
logger.info(`(Hint: you can use \`socket repos list --page ${nextPage}\`)`)
70+
logger.info(
71+
`(Hint: you can use \`socket repository list --page ${nextPage}\`)`,
72+
)
7173
} else if (perPage === Infinity) {
7274
logger.info(`This should be the entire list available on the server.`)
7375
} else {

src/commands/scan/cmd-scan-create.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path'
33
import { logger } from '@socketsecurity/registry/lib/logger'
44

55
import { handleCreateNewScan } from './handle-create-new-scan.mts'
6-
import { outputCreateNewScan } from './output-create-new-scan.mjs'
6+
import { outputCreateNewScan } from './output-create-new-scan.mts'
77
import { suggestOrgSlug } from './suggest-org-slug.mts'
88
import { suggestTarget } from './suggest_target.mts'
99
import constants from '../../constants.mts'

src/commands/scan/cmd-scan-github.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path'
33
import { logger } from '@socketsecurity/registry/lib/logger'
44

55
import { handleCreateGithubScan } from './handle-create-github-scan.mts'
6-
import { outputScanGithub } from './output-scan-github.mjs'
6+
import { outputScanGithub } from './output-scan-github.mts'
77
import { suggestOrgSlug } from './suggest-org-slug.mts'
88
import constants from '../../constants.mts'
99
import { commonFlags, outputFlags } from '../../flags.mts'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { logger } from '@socketsecurity/registry/lib/logger'
2+
import { select } from '@socketsecurity/registry/lib/prompts'
3+
4+
import { getConfigValue, updateConfigValue } from '../../utils/config.mts'
5+
6+
export async function suggestToPersistOrgSlug(orgSlug: string): Promise<void> {
7+
const skipAsk = getConfigValue('skipAskToPersistDefaultOrg')
8+
if (!skipAsk.ok || skipAsk.data) {
9+
// Don't ask to store it when disabled before, or when reading config fails.
10+
return
11+
}
12+
13+
const result = await select<string>({
14+
message: `Would you like to use this org (${orgSlug}) as the default org for future calls?`,
15+
choices: [
16+
{
17+
name: 'Yes',
18+
value: 'yes',
19+
description: 'Stores it in your config',
20+
},
21+
{
22+
name: 'No',
23+
value: 'no',
24+
description: 'Do not persist this org as default org',
25+
},
26+
{
27+
name: "No and don't ask again",
28+
value: 'sush',
29+
description:
30+
'Do not store as default org and do not ask again to persist it',
31+
},
32+
],
33+
})
34+
if (result === 'yes') {
35+
const updateResult = updateConfigValue('defaultOrg', orgSlug)
36+
if (updateResult.ok) {
37+
logger.success('Updated default org config to:', orgSlug)
38+
} else {
39+
logger.fail(
40+
'(Non blocking) Failed to update default org in config:',
41+
updateResult.cause,
42+
)
43+
}
44+
} else if (result === 'sush') {
45+
const updateResult = updateConfigValue('skipAskToPersistDefaultOrg', true)
46+
if (updateResult.ok) {
47+
logger.info('Default org not changed. Will not ask to persist again.')
48+
} else {
49+
logger.fail(
50+
`(Non blocking) Failed to store preference; will ask to persist again next time. Reason: ${updateResult.cause}`,
51+
)
52+
}
53+
}
54+
}

src/utils/config.mts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface LocalConfig {
2020
apiToken?: string | null | undefined
2121
defaultOrg?: string
2222
enforcedOrgs?: string[] | readonly string[] | null | undefined
23+
skipAskToPersistDefaultOrg?: boolean
2324
org?: string // convenience alias for defaultOrg
2425
}
2526

@@ -37,6 +38,10 @@ export const supportedConfigKeys: Map<keyof LocalConfig, string> = new Map([
3738
'enforcedOrgs',
3839
'Orgs in this list have their security policies enforced on this machine',
3940
],
41+
[
42+
'skipAskToPersistDefaultOrg',
43+
'This flag prevents the CLI from asking you to persist the org slug when you selected one interactively',
44+
],
4045
['org', 'Alias for defaultOrg'],
4146
])
4247

@@ -206,19 +211,35 @@ export function overrideConfigApiToken(apiToken: unknown) {
206211

207212
let _pendingSave = false
208213
export function updateConfigValue<Key extends keyof LocalConfig>(
209-
key: keyof LocalConfig,
214+
configKey: keyof LocalConfig,
210215
value: LocalConfig[Key],
211216
): CResult<undefined | string> {
212217
const localConfig = getConfigValues()
213-
const keyResult = normalizeConfigKey(key)
218+
const keyResult = normalizeConfigKey(configKey)
214219
if (!keyResult.ok) {
215220
return keyResult
216221
}
217-
localConfig[keyResult.data as Key] = value
222+
const key: Key = keyResult.data as Key
223+
let wasDeleted = value === undefined // implicitly when serializing
224+
if (key === 'skipAskToPersistDefaultOrg') {
225+
if (value === 'true' || value === 'false') {
226+
localConfig['skipAskToPersistDefaultOrg'] = value === 'true'
227+
} else {
228+
delete localConfig['skipAskToPersistDefaultOrg']
229+
wasDeleted = true
230+
}
231+
} else {
232+
if (value === 'undefined' || value === 'true' || value === 'false') {
233+
logger.warn(
234+
`Note: The value is set to "${value}", as a string (!). Use \`socket config unset\` to reset a key.`,
235+
)
236+
}
237+
localConfig[key] = value
238+
}
218239
if (_readOnlyConfig) {
219240
return {
220241
ok: true,
221-
message: `Config key '${keyResult.data}' was updated`,
242+
message: `Config key '${key}' was ${wasDeleted ? 'deleted' : `updated`}`,
222243
data: 'Change applied but not persisted; current config is overridden through env var or flag',
223244
}
224245
}
@@ -240,7 +261,7 @@ export function updateConfigValue<Key extends keyof LocalConfig>(
240261

241262
return {
242263
ok: true,
243-
message: `Config key '${keyResult.data}' was updated`,
264+
message: `Config key '${key}' was ${wasDeleted ? 'deleted' : `updated`}`,
244265
data: undefined,
245266
}
246267
}

src/utils/determine-org-slug.mts

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

33
import { getConfigValueOrUndef } from './config.mts'
44
import { suggestOrgSlug } from '../commands/scan/suggest-org-slug.mts'
5+
import { suggestToPersistOrgSlug } from '../commands/scan/suggest-to-persist-orgslug.mts'
56

67
export async function determineOrgSlug(
78
orgFlag: string,
@@ -55,6 +56,10 @@ export async function determineOrgSlug(
5556
logger.fail('Skipping auto-discovery of org in dry-run mode')
5657
} else {
5758
orgSlug = (await suggestOrgSlug()) || ''
59+
60+
if (orgSlug) {
61+
await suggestToPersistOrgSlug(orgSlug)
62+
}
5863
}
5964
}
6065

0 commit comments

Comments
 (0)