Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/commands/config/cmd-config-auto.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('socket config auto', async () => {
- apiToken -- The API token required to access most API endpoints
- 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.
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
- org -- Alias for defaultOrg

For certain keys it will request the value from server, for others it will
Expand All @@ -44,6 +45,7 @@ describe('socket config auto', async () => {
- apiToken -- The API token required to access most API endpoints
- 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.
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
- org -- Alias for defaultOrg

Examples
Expand Down
1 change: 1 addition & 0 deletions src/commands/config/cmd-config-get.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('socket config get', async () => {
- apiToken -- The API token required to access most API endpoints
- 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.
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
- org -- Alias for defaultOrg

Examples
Expand Down
1 change: 1 addition & 0 deletions src/commands/config/cmd-config-set.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('socket config get', async () => {
- apiToken -- The API token required to access most API endpoints
- 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.
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
- org -- Alias for defaultOrg

Examples
Expand Down
1 change: 1 addition & 0 deletions src/commands/config/cmd-config-unset.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('socket config unset', async () => {
- apiToken -- The API token required to access most API endpoints
- 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.
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
- skipAskToPersistDefaultOrg -- This flag prevents the CLI from asking you to persist the org slug when you selected one interactively
- org -- Alias for defaultOrg

Examples
Expand Down
4 changes: 3 additions & 1 deletion src/commands/repository/output-list-repos.mts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export async function outputListRepos(
logger.info(
`This is page ${page}. Server indicated there are more results available on page ${nextPage}...`,
)
logger.info(`(Hint: you can use \`socket repos list --page ${nextPage}\`)`)
logger.info(
`(Hint: you can use \`socket repository list --page ${nextPage}\`)`,
)
} else if (perPage === Infinity) {
logger.info(`This should be the entire list available on the server.`)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/scan/cmd-scan-create.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { logger } from '@socketsecurity/registry/lib/logger'

import { handleCreateNewScan } from './handle-create-new-scan.mts'
import { outputCreateNewScan } from './output-create-new-scan.mjs'
import { outputCreateNewScan } from './output-create-new-scan.mts'
import { suggestOrgSlug } from './suggest-org-slug.mts'
import { suggestTarget } from './suggest_target.mts'
import constants from '../../constants.mts'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/scan/cmd-scan-github.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import { logger } from '@socketsecurity/registry/lib/logger'

import { handleCreateGithubScan } from './handle-create-github-scan.mts'
import { outputScanGithub } from './output-scan-github.mjs'
import { outputScanGithub } from './output-scan-github.mts'
import { suggestOrgSlug } from './suggest-org-slug.mts'
import constants from '../../constants.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
Expand Down
54 changes: 54 additions & 0 deletions src/commands/scan/suggest-to-persist-orgslug.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { logger } from '@socketsecurity/registry/lib/logger'
import { select } from '@socketsecurity/registry/lib/prompts'

import { getConfigValue, updateConfigValue } from '../../utils/config.mts'

export async function suggestToPersistOrgSlug(orgSlug: string): Promise<void> {
const skipAsk = getConfigValue('skipAskToPersistDefaultOrg')
if (!skipAsk.ok || skipAsk.data) {
// Don't ask to store it when disabled before, or when reading config fails.
return
}
Comment thread
pvdz marked this conversation as resolved.

const result = await select<string>({
message: `Would you like to use this org (${orgSlug}) as the default org for future calls?`,
choices: [
{
name: 'Yes',
value: 'yes',
description: 'Stores it in your config',
},
{
name: 'No',
value: 'no',
description: 'Do not persist this org as default org',
},
{
name: "No and don't ask again",
value: 'sush',
description:
'Do not store as default org and do not ask again to persist it',
},
],
})
if (result === 'yes') {
const updateResult = updateConfigValue('defaultOrg', orgSlug)
if (updateResult.ok) {
logger.success('Updated default org config to:', orgSlug)
} else {
logger.fail(
'(Non blocking) Failed to update default org in config:',
updateResult.cause,
)
}
} else if (result === 'sush') {
const updateResult = updateConfigValue('skipAskToPersistDefaultOrg', true)
if (updateResult.ok) {
logger.info('Default org not changed. Will not ask to persist again.')
} else {
logger.fail(
`(Non blocking) Failed to store preference; will ask to persist again next time. Reason: ${updateResult.cause}`,
)
}
}
}
31 changes: 26 additions & 5 deletions src/utils/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface LocalConfig {
apiToken?: string | null | undefined
defaultOrg?: string
enforcedOrgs?: string[] | readonly string[] | null | undefined
skipAskToPersistDefaultOrg?: boolean
org?: string // convenience alias for defaultOrg
}

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

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

let _pendingSave = false
export function updateConfigValue<Key extends keyof LocalConfig>(
key: keyof LocalConfig,
configKey: keyof LocalConfig,
value: LocalConfig[Key],
): CResult<undefined | string> {
const localConfig = getConfigValues()
const keyResult = normalizeConfigKey(key)
const keyResult = normalizeConfigKey(configKey)
if (!keyResult.ok) {
return keyResult
}
localConfig[keyResult.data as Key] = value
const key: Key = keyResult.data as Key
let wasDeleted = value === undefined // implicitly when serializing
if (key === 'skipAskToPersistDefaultOrg') {
if (value === 'true' || value === 'false') {
localConfig['skipAskToPersistDefaultOrg'] = value === 'true'
} else {
delete localConfig['skipAskToPersistDefaultOrg']
wasDeleted = true
}
} else {
if (value === 'undefined' || value === 'true' || value === 'false') {
logger.warn(
`Note: The value is set to "${value}", as a string (!). Use \`socket config unset\` to reset a key.`,
)
}
localConfig[key] = value
}
if (_readOnlyConfig) {
return {
ok: true,
message: `Config key '${keyResult.data}' was updated`,
message: `Config key '${key}' was ${wasDeleted ? 'deleted' : `updated`}`,
data: 'Change applied but not persisted; current config is overridden through env var or flag',
}
}
Expand All @@ -240,7 +261,7 @@ export function updateConfigValue<Key extends keyof LocalConfig>(

return {
ok: true,
message: `Config key '${keyResult.data}' was updated`,
message: `Config key '${key}' was ${wasDeleted ? 'deleted' : `updated`}`,
data: undefined,
}
}
5 changes: 5 additions & 0 deletions src/utils/determine-org-slug.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'

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

export async function determineOrgSlug(
orgFlag: string,
Expand Down Expand Up @@ -55,6 +56,10 @@ export async function determineOrgSlug(
logger.fail('Skipping auto-discovery of org in dry-run mode')
} else {
orgSlug = (await suggestOrgSlug()) || ''

if (orgSlug) {
await suggestToPersistOrgSlug(orgSlug)
}
}
}

Expand Down