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
6 changes: 4 additions & 2 deletions src/commands/action/cmd-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const config: CliCommandConfig = {
description: 'After marker'
}
},
help: (parentName, { commandName, flags }) => `
help: (command, { flags }) => `
Usage
$ ${parentName} ${commandName} [options]
$ ${command} [options]

Options
${getFlagListOutput(flags, 6)}
Expand Down Expand Up @@ -58,5 +58,7 @@ async function run(
const githubEventBefore = String(cli.flags['githubEventBefore'] || '')
const githubEventAfter = String(cli.flags['githubEventAfter'] || '')

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await runAction(githubEventBefore, githubEventAfter)
}
14 changes: 8 additions & 6 deletions src/commands/analytics/cmd-analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ const config: CliCommandConfig = {
description: 'Path to a local file to save the output'
}
},
help: (parentName, { commandName, flags }) => `
help: (command, { flags }) => `
Usage
$ ${parentName} ${commandName} --scope=<scope> --time=<time filter>
$ ${command} --scope=<scope> --time=<time filter>

Default parameters are set to show the organization-level analytics over the
last 7 days.
Expand All @@ -53,9 +53,9 @@ const config: CliCommandConfig = {
${getFlagListOutput(flags, 6)}

Examples
$ ${parentName} ${commandName} --scope=org --time=7
$ ${parentName} ${commandName} --scope=org --time=30
$ ${parentName} ${commandName} --scope=repo --repo=test-repo --time=30
$ ${command} --scope=org --time=7
$ ${command} --scope=org --time=30
$ ${command} --scope=repo --repo=test-repo --time=30
`
}

Expand Down Expand Up @@ -89,10 +89,12 @@ async function run(
- The time filter must either be 7, 30 or 90 ${badTime ? colors.red('(bad!)') : colors.green('(ok)')}\n
- Repository name using --repo when scope is "repo" ${badRepo ? colors.red('(bad!)') : colors.green('(ok)')}\n
`)
cli.showHelp()
process.exitCode = 2 // bad input
return
}

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
Expand Down
4 changes: 3 additions & 1 deletion src/commands/audit-log/cmd-audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ async function run(
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n
`)
config.help(parentName, config)
process.exitCode = 2 // bad input
return
}

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
Expand Down
26 changes: 15 additions & 11 deletions src/commands/cdxgen/cmd-cdxgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import yargsParse from 'yargs-parser'
import { pluralize } from '@socketsecurity/registry/lib/words'

import { runCycloneDX } from './run-cyclonedx.ts'
import { CliCommandConfig } from '../../utils/meow-with-subcommands.ts'
import { meowOrExit } from '../../utils/meow-with-subcommands.ts'
import { getFlagListOutput } from '../../utils/output-formatting.ts'

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

// TODO: convert yargs to meow. Or convert all the other things to yargs.
const toLower = (arg: string) => arg.toLowerCase()
const arrayToLower = (arg: string[]) => arg.map(toLower)
Expand Down Expand Up @@ -126,16 +128,16 @@ export const cmdCdxgen = {

async function run(
argv: readonly string[],
_importMeta: ImportMeta,
{ parentName: _parentName }: { parentName: string }
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
// const cli = meowOrExit({
// allowUnknownFlags: true,
// argv,
// config,
// importMeta,
// parentName,
// })
const cli = meowOrExit({
allowUnknownFlags: true,
argv: argv.filter(s => s !== '--help' && s !== '-h'), // Don't let meow take over --help
config,
importMeta,
parentName
})
//
//
// if (cli.input.length)
Expand All @@ -154,16 +156,18 @@ async function run(
const unknown: Array<string> = yargv._
const { length: unknownLength } = unknown
if (unknownLength) {
process.exitCode = 1
console.error(
`Unknown ${pluralize('argument', unknownLength)}: ${yargv._.join(', ')}`
)
process.exitCode = 2 // bad input
return
}

if (yargv.output === undefined) {
yargv.output = 'socket-cdx.json'
}

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await runCycloneDX(yargv)
}
2 changes: 2 additions & 0 deletions src/commands/dependencies/cmd-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ async function run(
parentName
})

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

// TODO: markdown flag is ignored
await findDependencies({
limit: Number(cli.flags['limit'] || 0) || 0,
Expand Down
4 changes: 3 additions & 1 deletion src/commands/diff-scan/cmd-diff-scan-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@ async function run(
- To get full scans IDs, you can run the command "socket scan list <your org slug>".
- Org name as the first argument ${!orgSlug ? colors.red('(missing!)') : colors.green('(ok)')}\n}
`)
config.help(parentName, config)
process.exitCode = 2 // bad input
return
}

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
Expand Down
9 changes: 7 additions & 2 deletions src/commands/fix/cmd-fix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { runFix } from './run-fix.ts'
import { commonFlags } from '../../flags.ts'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting.ts'

Expand All @@ -8,7 +9,9 @@ const config: CliCommandConfig = {
commandName: 'fix',
description: 'Fix "fixable" Socket alerts',
hidden: true,
flags: {},
flags: {
...commonFlags
},
help: (command, config) => `
Usage
$ ${command}
Expand All @@ -29,12 +32,14 @@ async function run(
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
meowOrExit({
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await runFix()
}
24 changes: 13 additions & 11 deletions src/commands/info/cmd-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import colors from 'yoctocolors-cjs'

import { getPackageInfo } from './get-package-info.ts'
import { commonFlags, outputFlags, validationFlags } from '../../flags'
import { InputError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'

Expand Down Expand Up @@ -46,24 +47,25 @@ async function run(
parentName
})

if (cli.input.length > 1) {
throw new InputError('Only one package lookup supported at once')
}
const { 0: rawPkgName = '' } = cli.input
let showHelp = cli.flags['help']
if (!rawPkgName) {
showHelp = true
}
if (showHelp) {
cli.showHelp()
const [rawPkgName = ''] = cli.input

if (!rawPkgName || cli.input.length > 1) {
console.error(`${colors.bgRed(colors.white('Input error'))}: Please provide the required fields:\n
- Expecting a package name ${!rawPkgName ? colors.red('(missing!)') : colors.green('(ok)')}\n
- Can only accept one package at a time ${cli.input.length > 1 ? colors.red('(got ' + cli.input.length + '!)') : colors.green('(ok)')}\n
`)
process.exitCode = 2 // bad input
return
}

const versionSeparator = rawPkgName.lastIndexOf('@')
const pkgName =
versionSeparator < 1 ? rawPkgName : rawPkgName.slice(0, versionSeparator)
const pkgVersion =
versionSeparator < 1 ? 'latest' : rawPkgName.slice(versionSeparator + 1)

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await getPackageInfo({
commandName: `${parentName} ${config.commandName}`,
includeAllIssues: Boolean(cli.flags['all']),
Expand Down
10 changes: 7 additions & 3 deletions src/commands/login/cmd-login.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import isInteractive from '@socketregistry/is-interactive/index.cjs'

import { attemptLogin } from './attempt-login.ts'
import { commonFlags } from '../../flags.ts'
import { InputError } from '../../utils/errors'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting'
Expand All @@ -12,6 +13,7 @@ const config: CliCommandConfig = {
description: 'Socket API login',
hidden: false,
flags: {
...commonFlags,
apiBaseUrl: {
type: 'string',
description: 'API server to connect to for login'
Expand Down Expand Up @@ -54,14 +56,16 @@ async function run(
parentName
})

let apiBaseUrl = cli.flags['apiBaseUrl'] as string | undefined
let apiProxy = cli.flags['apiProxy'] as string | undefined

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

if (!isInteractive()) {
throw new InputError(
'Cannot prompt for credentials in a non-interactive shell'
)
}

let apiBaseUrl = cli.flags['apiBaseUrl'] as string | undefined
let apiProxy = cli.flags['apiProxy'] as string | undefined

await attemptLogin(apiBaseUrl, apiProxy)
}
9 changes: 7 additions & 2 deletions src/commands/logout/cmd-logout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { attemptLogout } from './attempt-logout.ts'
import { commonFlags } from '../../flags.ts'
import { meowOrExit } from '../../utils/meow-with-subcommands'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands.ts'
Expand All @@ -7,7 +8,9 @@ const config: CliCommandConfig = {
commandName: 'logout',
description: 'Socket API logout',
hidden: false,
flags: {},
flags: {
...commonFlags
},
help: (command, _config) => `
Usage
$ ${command}
Expand All @@ -27,12 +30,14 @@ async function run(
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
meowOrExit({
const cli = meowOrExit({
argv,
config,
importMeta,
parentName
})

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

attemptLogout()
}
19 changes: 15 additions & 4 deletions src/commands/manifest/cmd-manifest-auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import meow from 'meow'
import { cmdManifestGradle } from './cmd-manifest-gradle.ts'
import { cmdManifestScala } from './cmd-manifest-scala.ts'
import { commonFlags } from '../../flags.ts'
import { meowOrExit } from '../../utils/meow-with-subcommands'
import { getFlagListOutput } from '../../utils/output-formatting.ts'

import type { CliCommandConfig } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -51,13 +52,13 @@ async function run(
importMeta: ImportMeta,
{ parentName }: { parentName: string }
): Promise<void> {
const cli = meow(config.help(parentName, config), {
const cli = meowOrExit({
argv,
description: config.description,
config,
importMeta,
flags: config.flags,
allowUnknownFlags: false
parentName
})

const verbose = !!cli.flags['verbose']
const cwd = <string>cli.flags['cwd'] ?? process.cwd()
if (verbose) {
Expand All @@ -72,7 +73,9 @@ async function run(
if (verbose) {
subArgs.push('--verbose')
}

const dir = cwd

if (existsSync(path.join(dir, 'build.sbt'))) {
console.log(
'Detected a Scala sbt build, running default Scala generator...'
Expand All @@ -81,18 +84,26 @@ async function run(
subArgs.push('--cwd', cwd)
}
subArgs.push(dir)

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await cmdManifestScala.run(subArgs, importMeta, { parentName })
return
}

if (existsSync(path.join(dir, 'gradlew'))) {
console.log('Detected a gradle build, running default gradle generator...')
if (cwd) {
// This command takes the cwd as first arg.
subArgs.push(cwd)
}

if (cli.flags['dryRun']) return console.log('[DryRun] Bailing now')

await cmdManifestGradle.run(subArgs, importMeta, { parentName })
return
}

// Show new help screen and exit.
meow(
`
Expand Down
Loading
Loading