Skip to content

Commit b68c688

Browse files
author
Mila Votradovec
committedMay 4, 2021
fix: propagate argument to all modules
Parse args as a first things, before other modules are required. That will allow others to pick arguments.
1 parent 12eadfb commit b68c688

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed
 

‎src/cli/index.ts

+42-30
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import * as pathLib from 'path';
55

66
const camelCase = require('lodash.camelcase');
77

8+
// import args as a first internal module
9+
import { args as argsLib, Args, ArgsOptions } from './args';
10+
// parse args as a first thing; argsLib modifies global namespace
11+
// therefore it is better to do it as a first thing to prevent bugs
12+
// when modules use this global setting during their require phase
13+
// TODO(code): remove once https://app.stepsize.com/issue/c2f6253e-7240-436f-943c-23a897558156/2-http-libraries-in-cli is solved
14+
const globalArgs = argsLib(process.argv);
815
// assert supported node runtime version
916
import * as runtime from './runtime';
1017
// require analytics as soon as possible to start measuring execution time
1118
import * as analytics from '../lib/analytics';
1219
import * as alerts from '../lib/alerts';
1320
import * as sln from '../lib/sln';
14-
import { args as argsLib, Args, ArgsOptions } from './args';
1521
import { TestCommandResult } from './commands/types';
1622
import { copy } from './copy';
1723
import spinner = require('../lib/spinner');
@@ -232,97 +238,103 @@ async function main() {
232238
updateCheck();
233239
checkRuntime();
234240

235-
const args = argsLib(process.argv);
236-
237241
let res;
238242
let failed = false;
239243
let exitCode = EXIT_CODES.ERROR;
240244
try {
241-
modeValidation(args);
245+
modeValidation(globalArgs);
242246
// TODO: fix this, we do transformation to options and teh type doesn't reflect it
243247
validateUnsupportedOptionCombinations(
244-
(args.options as unknown) as AllSupportedCliOptions,
248+
(globalArgs.options as unknown) as AllSupportedCliOptions,
245249
);
246250

247251
// IaC only: used for rolling out the experimental flow
248252
// modify args if experimental flag not provided, based on feature flag
249253
// this can be removed once experimental becomes the default
250254
if (
251-
args.options['iac'] &&
252-
args.command === 'test' &&
253-
!args.options['experimental']
255+
globalArgs.options['iac'] &&
256+
globalArgs.command === 'test' &&
257+
!globalArgs.options['experimental']
254258
) {
255259
const iacOrgSettings = await getIacOrgSettings();
256260
const experimentalFlowEnabled = await isFeatureFlagSupportedForOrg(
257261
camelCase('experimental-local-exec-iac'),
258262
iacOrgSettings.meta.org,
259263
);
260-
args.options['experimental'] = !!experimentalFlowEnabled.ok;
264+
globalArgs.options['experimental'] = !!experimentalFlowEnabled.ok;
261265
}
262266

263-
if (args.options['app-vulns'] && args.options['json']) {
267+
if (globalArgs.options['app-vulns'] && globalArgs.options['json']) {
264268
throw new UnsupportedOptionCombinationError([
265269
'Application vulnerabilities is currently not supported with JSON output. ' +
266270
'Please try using —app-vulns only to get application vulnerabilities, or ' +
267271
'—json only to get your image vulnerabilties, excluding the application ones.',
268272
]);
269273
}
270-
if (args.options['group-issues'] && args.options['iac']) {
274+
if (globalArgs.options['group-issues'] && globalArgs.options['iac']) {
271275
throw new UnsupportedOptionCombinationError([
272276
'--group-issues is currently not supported for Snyk IaC.',
273277
]);
274278
}
275279
if (
276-
args.options['group-issues'] &&
277-
!args.options['json'] &&
278-
!args.options['json-file-output']
280+
globalArgs.options['group-issues'] &&
281+
!globalArgs.options['json'] &&
282+
!globalArgs.options['json-file-output']
279283
) {
280284
throw new UnsupportedOptionCombinationError([
281285
'JSON output is required to use --group-issues, try adding --json.',
282286
]);
283287
}
284288

285289
if (
286-
args.options.file &&
287-
typeof args.options.file === 'string' &&
288-
(args.options.file as string).match(/\.sln$/)
290+
globalArgs.options.file &&
291+
typeof globalArgs.options.file === 'string' &&
292+
(globalArgs.options.file as string).match(/\.sln$/)
289293
) {
290-
if (args.options['project-name']) {
294+
if (globalArgs.options['project-name']) {
291295
throw new UnsupportedOptionCombinationError([
292296
'file=*.sln',
293297
'project-name',
294298
]);
295299
}
296-
sln.updateArgs(args);
297-
} else if (typeof args.options.file === 'boolean') {
300+
sln.updateArgs(globalArgs);
301+
} else if (typeof globalArgs.options.file === 'boolean') {
298302
throw new FileFlagBadInputError();
299303
}
300304

301305
if (
302-
typeof args.options.detectionDepth !== 'undefined' &&
303-
(args.options.detectionDepth <= 0 ||
304-
Number.isNaN(args.options.detectionDepth))
306+
typeof globalArgs.options.detectionDepth !== 'undefined' &&
307+
(globalArgs.options.detectionDepth <= 0 ||
308+
Number.isNaN(globalArgs.options.detectionDepth))
305309
) {
306310
throw new InvalidDetectionDepthValue();
307311
}
308312

309-
validateUnsupportedSarifCombinations(args);
313+
validateUnsupportedSarifCombinations(globalArgs);
310314

311-
validateOutputFile(args.options, 'json', new JsonFileOutputBadInputError());
312-
validateOutputFile(args.options, 'sarif', new SarifFileOutputEmptyError());
315+
validateOutputFile(
316+
globalArgs.options,
317+
'json',
318+
new JsonFileOutputBadInputError(),
319+
);
320+
validateOutputFile(
321+
globalArgs.options,
322+
'sarif',
323+
new SarifFileOutputEmptyError(),
324+
);
313325

314-
checkPaths(args);
326+
checkPaths(globalArgs);
315327

316-
res = await runCommand(args);
328+
res = await runCommand(globalArgs);
317329
} catch (error) {
318330
failed = true;
319331

320-
const response = await handleError(args, error);
332+
const response = await handleError(globalArgs, error);
321333
res = response.res;
322334
exitCode = response.exitCode;
323335
}
324336

325-
if (!args.options.json) {
337+
if (!globalArgs.options.json) {
326338
console.log(alerts.displayAlerts());
327339
}
328340

0 commit comments

Comments
 (0)
Please sign in to comment.