|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { logger } from '@socketsecurity/registry/lib/logger' |
| 4 | + |
| 5 | +import { handleScanReach } from './handle-reach-scan.mts' |
| 6 | +import constants from '../../constants.mts' |
| 7 | +import { commonFlags, outputFlags } from '../../flags.mts' |
| 8 | +import { checkCommandInput } from '../../utils/check-input.mts' |
| 9 | +import { getOutputKind } from '../../utils/get-output-kind.mts' |
| 10 | +import { meowOrExit } from '../../utils/meow-with-subcommands.mts' |
| 11 | +import { getFlagListOutput } from '../../utils/output-formatting.mts' |
| 12 | + |
| 13 | +import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts' |
| 14 | + |
| 15 | +const { DRY_RUN_BAILING_NOW } = constants |
| 16 | + |
| 17 | +const config: CliCommandConfig = { |
| 18 | + commandName: 'reach', |
| 19 | + description: 'Compute tier 1 reachability', |
| 20 | + hidden: true, // b-e-tah |
| 21 | + flags: { |
| 22 | + ...commonFlags, |
| 23 | + ...outputFlags, |
| 24 | + interactive: { |
| 25 | + type: 'boolean', |
| 26 | + default: true, |
| 27 | + description: |
| 28 | + 'Allow for interactive elements, asking for input. Use --no-interactive to prevent any input questions, defaulting them to cancel/no.', |
| 29 | + }, |
| 30 | + }, |
| 31 | + help: (command, config) => ` |
| 32 | + Usage |
| 33 | + $ ${command} [CWD=.] |
| 34 | +
|
| 35 | + Options |
| 36 | + ${getFlagListOutput(config.flags, 6)} |
| 37 | +
|
| 38 | + Examples |
| 39 | + $ ${command} |
| 40 | + $ ${command} ./proj |
| 41 | + `, |
| 42 | +} |
| 43 | + |
| 44 | +export const cmdScanReach = { |
| 45 | + description: config.description, |
| 46 | + hidden: config.hidden, |
| 47 | + run, |
| 48 | +} |
| 49 | + |
| 50 | +async function run( |
| 51 | + argv: string[] | readonly string[], |
| 52 | + importMeta: ImportMeta, |
| 53 | + { parentName }: { parentName: string }, |
| 54 | +): Promise<void> { |
| 55 | + const cli = meowOrExit({ |
| 56 | + argv, |
| 57 | + config, |
| 58 | + importMeta, |
| 59 | + parentName, |
| 60 | + }) |
| 61 | + |
| 62 | + const { dryRun, interactive, json, markdown } = cli.flags |
| 63 | + const outputKind = getOutputKind(json, markdown) |
| 64 | + let [cwd = '.'] = cli.input |
| 65 | + // Note: path.resolve vs .join: |
| 66 | + // If given path is absolute then cwd should not affect it. |
| 67 | + cwd = path.resolve(process.cwd(), cwd) |
| 68 | + logger.info( |
| 69 | + 'If you dont have any interactive bits then drop the flag', |
| 70 | + interactive, |
| 71 | + ) |
| 72 | + |
| 73 | + const wasValidInput = checkCommandInput(outputKind) |
| 74 | + if (!wasValidInput) { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if (dryRun) { |
| 79 | + logger.log(DRY_RUN_BAILING_NOW) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + await handleScanReach(cwd, outputKind) |
| 84 | +} |
0 commit comments