Skip to content

Commit 8fa1030

Browse files
pvdzjdalton
andauthored
Reaching for the stars (#627)
* Reaching for the stars * update copy pasta * dep management * oh sure we probably dont need this for this command --------- Co-authored-by: John-David Dalton <jdalton@users.noreply.github.com>
1 parent e9cf93b commit 8fa1030

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/commands/scan/cmd-scan.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { cmdScanDiff } from './cmd-scan-diff.mts'
44
import { cmdScanGithub } from './cmd-scan-github.mts'
55
import { cmdScanList } from './cmd-scan-list.mts'
66
import { cmdScanMetadata } from './cmd-scan-metadata.mts'
7+
import { cmdScanReach } from './cmd-scan-reach.mts'
78
import { cmdScanReport } from './cmd-scan-report.mts'
89
import { cmdScanSetup } from './cmd-scan-setup.mts'
910
import { cmdScanView } from './cmd-scan-view.mts'
@@ -24,6 +25,7 @@ export const cmdScan: CliSubcommand = {
2425
github: cmdScanGithub,
2526
list: cmdScanList,
2627
metadata: cmdScanMetadata,
28+
reach: cmdScanReach,
2729
report: cmdScanReport,
2830
setup: cmdScanSetup,
2931
view: cmdScanView,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { outputScanReach } from './output-scan-reach.mts'
2+
import { scanReachability } from './scan-reachability.mts'
3+
4+
import type { OutputKind } from '../../types.mts'
5+
6+
export async function handleScanReach(cwd: string, outputKind: OutputKind) {
7+
const result = await scanReachability(cwd)
8+
9+
await outputScanReach(result, cwd, outputKind)
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { logger } from '@socketsecurity/registry/lib/logger'
2+
3+
import { failMsgWithBadge } from '../../utils/fail-msg-with-badge.mts'
4+
import { serializeResultJson } from '../../utils/serialize-result-json.mts'
5+
6+
import type { CResult, OutputKind } from '../../types.mts'
7+
8+
export async function outputScanReach(
9+
result: CResult<unknown>,
10+
cwd: string,
11+
outputKind: OutputKind,
12+
): Promise<void> {
13+
if (!result.ok) {
14+
process.exitCode = result.code ?? 1
15+
}
16+
17+
if (outputKind === 'json') {
18+
logger.log(serializeResultJson(result))
19+
return
20+
}
21+
if (!result.ok) {
22+
logger.fail(failMsgWithBadge(result.message, result.cause))
23+
return
24+
}
25+
26+
logger.success('finished on', cwd)
27+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { CResult } from '../../types.mts'
2+
3+
export async function scanReachability(cwd: string): Promise<CResult<unknown>> {
4+
console.log('Scanning now... as soon as you implement me! From', cwd)
5+
6+
return { ok: true, data: undefined }
7+
}

src/utils/api.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export async function queryApi(path: string, apiToken: string) {
176176
'API endpoint is not set and default was empty. Request is likely to fail.',
177177
)
178178
}
179+
179180
return await fetch(`${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}${path}`, {
180181
method: 'GET',
181182
headers: {

0 commit comments

Comments
 (0)