-
Notifications
You must be signed in to change notification settings - Fork 533
/
args.ts
289 lines (251 loc) · 8.11 KB
/
args.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import * as abbrev from 'abbrev';
import { MethodResult } from './commands/types';
import debugModule = require('debug');
import { parseMode, displayModeHelp } from './modes';
import {
SupportedCliCommands,
SupportedUserReachableFacingCliArgs,
} from '../lib/types';
import { getContainerImageSavePath } from '../lib/container';
import { obfuscateArgs } from '../lib/utils';
export declare interface Global extends NodeJS.Global {
ignoreUnknownCA: boolean;
}
declare const global: Global;
const alias = abbrev(
'copy',
'version',
'debug',
'help',
'quiet',
'interactive',
'dev',
);
alias.d = 'debug'; // always make `-d` debug
alias.t = 'test';
alias.p = 'prune-repeated-subdependencies';
// The -d flag enables printing the messages for predefined namespaces.
// Additional ones can be specified (comma-separated) in the DEBUG environment variable.
const DEBUG_DEFAULT_NAMESPACES = [
'snyk-test',
'snyk',
'snyk:find-files',
'snyk:run-test',
'snyk:prune',
'snyk-nodejs-plugin',
'snyk-gradle-plugin',
'snyk-sbt-plugin',
'snyk-mvn-plugin',
'snyk-yarn-workspaces',
'snyk-java-call-graph-builder',
];
// NOTE[muscar] This is accepted in seconds for UX reasons, the maven plugin
// turns it into milliseconds before calling the call graph generator
const REACHABLE_VULNS_TIMEOUT = 5 * 60; // 5 min (in seconds)
function dashToCamelCase(dash) {
return dash.indexOf('-') < 0
? dash
: dash.replace(/-[a-z]/g, (m) => m[1].toUpperCase());
}
// Last item is ArgsOptions, the rest are strings (positional arguments, e.g. paths)
export type MethodArgs = Array<string | ArgsOptions>;
export interface Args {
command: string;
method: (...args: MethodArgs) => Promise<MethodResult>; // command resolved to a function
options: ArgsOptions;
}
export interface ArgsOptions {
// all arguments after a '--' are taken as is and passed to the next process
// (see the snyk-mvn-plugin or snyk-gradle-plugin)
_doubleDashArgs: string[];
_: MethodArgs;
[key: string]: boolean | string | number | MethodArgs | string[]; // The two last types are for compatibility only
}
export function args(rawArgv: string[]): Args {
const argv = {
_: [] as string[],
} as ArgsOptions;
for (let arg of rawArgv.slice(2)) {
if (argv._doubleDashArgs) {
argv._doubleDashArgs.push(arg);
} else if (arg === '--') {
argv._doubleDashArgs = [];
} else if (arg[0] === '-') {
arg = arg.slice(1);
if (alias[arg] !== undefined) {
argv[alias[arg]] = true;
} else if (arg[0] === '-') {
arg = arg.slice(1);
if (arg.indexOf('=') === -1) {
argv[arg] = true;
} else {
const parts = arg.split('=');
argv[parts.shift()!] = parts.join('=');
}
} else {
argv[arg] = true;
}
} else {
argv._.push(arg);
}
}
// By passing `-d` to the CLI, we enable the debugging output.
// It needs to happen BEFORE any of the `debug(namespace)` calls needed to create loggers.
// Therefore, the code used by the CLI should create the loggers in a lazy fashion
// or be `require`d after this code.
// TODO(BST-648): sort this out reliably
if (argv.debug) {
let enable = DEBUG_DEFAULT_NAMESPACES.join(',');
if (process.env.DEBUG) {
enable += ',' + process.env.DEBUG;
}
// Storing in the global state, because just "debugModule.enable" call won't affect different instances of `debug`
// module imported by plugins, libraries etc.
process.env.DEBUG = enable;
debugModule.enable(enable);
}
const debug = debugModule('snyk');
// Late require, see the note re "debug" option above.
const cli = require('./commands');
// the first argument is the command we'll execute, everything else will be
// an argument to our command, like `snyk help protect`
let command = argv._.shift() as string; // can actually be undefined
// snyk [mode?] [command] [paths?] [options-double-dash]
command = displayModeHelp(command, argv);
command = parseMode(command, argv);
// alias switcheroo - allows us to have
if (cli.aliases[command]) {
command = cli.aliases[command];
}
// alias `-v` to `snyk version`
if (argv.version) {
command = 'version';
}
if (!command || argv.help || command === 'help') {
// bit of a song and dance to support `snyk -h` and `snyk help`
if (argv.help === true || command === 'help') {
argv.help = 'help';
}
// If command has a value prior to running it over with “help” and argv.help contains "help", save the command in argv._
// so that no argument gets deleted or ignored. This ensures `snyk --help [command]` and `snyk [command] --help` return the
// specific help page instead of the generic one.
// This change also covers the scenario of 'snyk [mode] [command] --help' and 'snyk --help [mode] [command]`.
if (!argv._.length) {
command && argv.help === 'help'
? argv._.unshift(command)
: argv._.unshift((argv.help as string) || 'help');
}
command = 'help';
}
if (command && command.indexOf('config:') === 0) {
// config looks like `config:set x=y` or `config:get x`
// so we need to mangle the commands into this format:
// snyk.config('set', 'api=x')
// snyk.config('get', 'api') // etc
const tmp = command.split(':');
command = tmp.shift()!;
argv._.unshift(tmp.shift()!);
}
let method: () => Promise<MethodResult> = cli[command];
if (!method) {
// if we failed to find a command, then default to an error
method = require('../lib/errors/legacy-errors');
argv._.push(command);
}
// TODO: Once experimental flag became default this block should be
// moved to inside the parseModes function for container mode
const imageSavePath = getContainerImageSavePath();
if (imageSavePath) {
argv['imageSavePath'] = imageSavePath;
}
if (command in SupportedCliCommands) {
// copy all the options across to argv._ as an object
argv._.push(argv);
}
// TODO: eventually all arguments should be transformed like this.
const argumentsToTransform: Array<Partial<
SupportedUserReachableFacingCliArgs
>> = [
'package-manager',
'packages-folder',
'severity-threshold',
'strict-out-of-sync',
'all-sub-projects',
'sub-project',
'gradle-sub-project',
'skip-unresolved',
'scan-all-unmanaged',
'fail-on',
'all-projects',
'yarn-workspaces',
'detection-depth',
'reachable',
'reachable-vulns',
'reachable-timeout',
'reachable-vulns-timeout',
'init-script',
'integration-name',
'integration-version',
'prune-repeated-subdependencies',
'dry-run',
'update-sequentially',
];
for (const dashedArg of argumentsToTransform) {
if (argv[dashedArg]) {
const camelCased = dashToCamelCase(dashedArg);
if (camelCased === dashedArg) {
continue;
}
argv[camelCased] = argv[dashedArg];
delete argv[dashedArg];
}
}
if (argv.detectionDepth !== undefined) {
argv.detectionDepth = Number(argv.detectionDepth);
}
if (argv.skipUnresolved !== undefined) {
if (argv.skipUnresolved === 'false') {
argv.allowMissing = false;
} else {
argv.allowMissing = true;
}
}
if (argv.strictOutOfSync !== undefined) {
if (argv.strictOutOfSync === 'false') {
argv.strictOutOfSync = false;
} else {
argv.strictOutOfSync = true;
}
}
if (
(argv.reachableVulns || argv.reachable) &&
argv.reachableTimeout === undefined &&
argv.reachableVulnsTimeout === undefined
) {
argv.reachableVulnsTimeout = REACHABLE_VULNS_TIMEOUT.toString();
}
// Alias
const aliases = {
gradleSubProject: 'subProject',
container: 'docker',
reachable: 'reachableVulns',
reachableTimeout: 'callGraphBuilderTimeout',
reachableVulnsTimeout: 'callGraphBuilderTimeout',
};
for (const argAlias in aliases) {
if (argv[argAlias]) {
const target = aliases[argAlias];
argv[target] = argv[argAlias];
delete argv[argAlias];
}
}
if (argv.insecure) {
global.ignoreUnknownCA = true;
}
debug(command, obfuscateArgs(argv));
return {
command,
method,
options: argv,
};
}