-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcmd-pycli.mts
More file actions
139 lines (117 loc) · 4.16 KB
/
Copy pathcmd-pycli.mts
File metadata and controls
139 lines (117 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Socket Python CLI (pycli) command.
*
* Explicit passthrough to the Socket Python CLI (socketsecurity) for features
* not yet available in the Node.js CLI. This replaces implicit fallback
* behavior with an explicit command that makes it clear when Python CLI is
* being used.
*
* Features available via Python CLI: - --generate-license: Generate license
* metadata for packages - --enable-sarif: Output in SARIF format -
* --strict-blocking: Fail on any policy violations, not just new ones -
* --disable-blocking: Always exit 0 - --enable-gitlab-security: GitLab
* Dependency Scanning format - --slack-webhook: Send notifications to Slack -
* --save-manifest-tar: Archive manifests for audit trail.
*/
import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default'
import { defineFlags } from '../../meow.mts'
import { commonFlags } from '../../flags.mts'
import { meowOrExit } from '../../util/cli/with-subcommands.mts'
import { outputDryRunExecute } from '../../util/dry-run/output.mts'
import { getFlagListOutput } from '../../util/output/formatting.mts'
import { filterFlags, isHelpFlag } from '../../util/process/cmd.mts'
import { spawnSocketPyCli } from '../../util/python/standalone.mts'
import type { CliCommandContext } from '../../util/cli/with-subcommands.mts'
const logger = getDefaultLogger()
// Flags interface for type safety.
export interface PycliFlags {
dryRun: boolean
}
const config = {
commandName: 'pycli',
description: 'Run Socket Python CLI (socketsecurity) directly',
flags: defineFlags({
...commonFlags,
}),
help: (command: string) => `
Usage
$ ${command} [python-cli-options] [TARGET...]
Options
${getFlagListOutput(commonFlags)}
This command passes all arguments directly to the Socket Python CLI
(socketsecurity). Use this for features not yet available in the
Node.js CLI.
Python CLI Features:
--generate-license Generate license metadata for all packages
--license-file-name Output file for license data
--enable-sarif Output in SARIF format
--enable-gitlab-security GitLab Dependency Scanning report format
--strict-blocking Fail on ANY policy violations (not just new)
--disable-blocking Always exit 0 regardless of findings
--save-manifest-tar Archive manifests for audit trail
--slack-webhook Send notifications to Slack webhook
Common Options (passed to Python CLI):
--repo <owner/repo> Repository name
--branch <name> Branch name
--commit-sha <sha> Commit SHA
--target-path <path> Path to scan
--pr-number <n> Pull request number
Examples
$ ${command} --help
$ ${command} --generate-license --repo owner/repo .
$ ${command} --enable-sarif --strict-blocking .
$ ${command} --slack-webhook https://hooks.slack.com/... .
`,
hidden: false,
}
export const cmdPyCli = {
description: config.description,
hidden: config.hidden,
run,
}
export async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
context: CliCommandContext,
): Promise<void> {
const { parentName } = {
__proto__: null,
...context,
} as CliCommandContext
// Check for help flag - if present, show our help first then Python CLI help.
const hasHelpFlag = argv.some(a => isHelpFlag(a))
if (hasHelpFlag) {
// Show Socket CLI wrapper help.
meowOrExit({
argv: ['--help'],
config,
importMeta,
parentName,
})
// meowOrExit will exit here.
return
}
const cli = meowOrExit({
argv: argv.filter(a => !isHelpFlag(a)),
config,
importMeta,
parentName,
})
const { dryRun } = cli.flags
// Filter Socket-specific flags from argv, pass rest to Python CLI.
const pyCliArgs = filterFlags(argv, commonFlags, [])
if (dryRun) {
outputDryRunExecute('socketsecurity', pyCliArgs, 'Python CLI')
return
}
logger.info('Invoking Socket Python CLI…')
const result = await spawnSocketPyCli(pyCliArgs, {
stdio: 'inherit',
})
if (!result.ok) {
process.exitCode = 1
if (result.message) {
logger.fail(result.message)
}
}
}