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/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'
import { cmdAnalytics } from './commands/analytics/cmd-analytics'
import { cmdAuditLog } from './commands/audit-log/cmd-audit-log'
import { cmdCdxgen } from './commands/cdxgen/cmd-cdxgen'
import { cmdConfig } from './commands/config/cmd-config'
import { cmdScanCreate } from './commands/dependencies/cmd-dependencies'
import { cmdDiffScan } from './commands/diff-scan/cmd-diff-scan'
import { cmdFix } from './commands/fix/cmd-fix'
Expand Down Expand Up @@ -51,6 +52,7 @@ void (async () => {
await meowWithSubcommands(
{
cdxgen: cmdCdxgen,
config: cmdConfig,
fix: cmdFix,
info: cmdInfo,
login: cmdLogin,
Expand Down
93 changes: 93 additions & 0 deletions src/commands/config/cmd-config-get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import path from 'node:path'

import { describe, expect } from 'vitest'

import constants from '../../../dist/constants.js'
import { cmdit, invokeNpm } from '../../../test/utils'

const { CLI } = constants

describe('socket config get', async () => {
// Lazily access constants.rootBinPath.
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)

cmdit(['config', 'get', '--help'], 'should support --help', async cmd => {
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
expect(stdout).toMatchInlineSnapshot(
`
"Get the value of a local CLI config item

Usage
$ socket config get <org slug>

Options
--dryRun Do input validation for a command and exit 0 when input is ok
--help Print this help.
--json Output result as json
--markdown Output result as markdown

Keys:

- apiBaseUrl -- Base URL of the API endpoint
- apiToken -- The API token required to access most API endpoints
- apiProxy -- A proxy through which to access the API
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine

Examples
$ socket config get FakeOrg --repoName=test-repo"
`
)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
`)

expect(code, 'help should exit with code 2').toBe(2)
expect(stderr, 'header should include command (without params)').toContain(
'`socket config get`'
)
})

cmdit(
['config', 'get', '--dry-run'],
'should require args with just dry-run',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
expect(stdout).toMatchInlineSnapshot(`""`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>

\\x1b[31m\\xd7\\x1b[39m \\x1b[41m\\x1b[37mInput error\\x1b[39m\\x1b[49m: Please provide the required fields:

- Config key should be the first arg \\x1b[31m(missing!)\\x1b[39m"
`)

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

cmdit(
['config', 'get', 'test', '--dry-run'],
'should require args with just dry-run',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config get\`, cwd: <redacted>"
`)

expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
}
)
})
86 changes: 86 additions & 0 deletions src/commands/config/cmd-config-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { stripIndents } from 'common-tags'
import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { handleConfigGet } from './handle-config-get'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { supportedConfigKeys } from '../../utils/config'
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: 'get',
description: 'Get the value of a local CLI config item',
hidden: false,
flags: {
...commonFlags,
...outputFlags
},
help: (command, config) => `
Usage
$ ${command} <org slug>

Options
${getFlagListOutput(config.flags, 6)}

Keys:

${Array.from(supportedConfigKeys.entries())
.map(([key, desc]) => ` - ${key} -- ${desc}`)
.join('\n')}

Examples
$ ${command} FakeOrg --repoName=test-repo
`
}

export const cmdConfigGet = {
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 = ''] = cli.input

if (!supportedConfigKeys.has(key as keyof LocalConfig) && key !== 'test') {
// Use exit status of 2 to indicate incorrect usage, generally invalid
// options or missing arguments.
// https://www.gnu.org/software/bash/manual/html_node/Exit-Status.html
process.exitCode = 2
logger.fail(stripIndents`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:

- Config key should be the first arg ${!key ? colors.red('(missing!)') : !supportedConfigKeys.has(key as any) ? colors.red('(invalid config key!)') : colors.green('(ok)')}
`)
return
}

if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}

await handleConfigGet({
key: key as keyof LocalConfig,
outputKind: json ? 'json' : markdown ? 'markdown' : 'text'
})
}
72 changes: 72 additions & 0 deletions src/commands/config/cmd-config-list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import path from 'node:path'

import { describe, expect } from 'vitest'

import constants from '../../../dist/constants.js'
import { cmdit, invokeNpm } from '../../../test/utils'

const { CLI } = constants

describe('socket config get', async () => {
// Lazily access constants.rootBinPath.
const entryPath = path.join(constants.rootBinPath, `${CLI}.js`)

cmdit(['config', 'list', '--help'], 'should support --help', async cmd => {
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
expect(stdout).toMatchInlineSnapshot(
`
"Show all local CLI config items and their values

Usage
$ socket config list <org slug>

Options
--dryRun Do input validation for a command and exit 0 when input is ok
--full Show full tokens in plaintext (unsafe)
--help Print this help.
--json Output result as json
--markdown Output result as markdown

Keys:

- apiBaseUrl -- Base URL of the API endpoint
- apiToken -- The API token required to access most API endpoints
- apiProxy -- A proxy through which to access the API
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine

Examples
$ socket config list FakeOrg --repoName=test-repo"
`
)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config list\`, cwd: <redacted>"
`)

expect(code, 'help should exit with code 2').toBe(2)
expect(stderr, 'header should include command (without params)').toContain(
'`socket config list`'
)
})

cmdit(
['config', 'list', '--dry-run'],
'should require args with just dry-run',
async cmd => {
const { code, stderr, stdout } = await invokeNpm(entryPath, cmd)
expect(stdout).toMatchInlineSnapshot(`"[DryRun]: Bailing now"`)
expect(`\n ${stderr}`).toMatchInlineSnapshot(`
"
_____ _ _ /---------------
| __|___ ___| |_ ___| |_ | Socket.dev CLI ver <redacted>
|__ | . | _| '_| -_| _| | Node: <redacted>, API token set: <redacted>
|_____|___|___|_,_|___|_|.dev | Command: \`socket config list\`, cwd: <redacted>"
`)

expect(code, 'dry-run should exit with code 0 if input ok').toBe(0)
}
)
})
74 changes: 74 additions & 0 deletions src/commands/config/cmd-config-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { logger } from '@socketsecurity/registry/lib/logger'

import { outputConfigList } from './output-config-list'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { supportedConfigKeys } from '../../utils/config'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'

const { DRY_RUN_BAIL_TEXT } = constants

const config: CliCommandConfig = {
commandName: 'list',
description: 'Show all local CLI config items and their values',
hidden: false,
flags: {
...commonFlags,
...outputFlags,
full: {
type: 'boolean',
default: false,
description: 'Show full tokens in plaintext (unsafe)'
}
},
help: (command, config) => `
Usage
$ ${command} <org slug>

Options
${getFlagListOutput(config.flags, 6)}

Keys:

${Array.from(supportedConfigKeys.entries())
.map(([key, desc]) => ` - ${key} -- ${desc}`)
.join('\n')}

Examples
$ ${command} FakeOrg --repoName=test-repo
`
}

export const cmdConfigList = {
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 { full, json, markdown } = cli.flags

if (cli.flags['dryRun']) {
logger.log(DRY_RUN_BAIL_TEXT)
return
}

await outputConfigList({
full: !!full,
outputKind: json ? 'json' : markdown ? 'markdown' : 'text'
})
}
Loading