Skip to content

Commit 63896d1

Browse files
committedNov 18, 2020
refactor: Coerce --detection-depth into number
1 parent a4e29be commit 63896d1

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
 

‎src/cli/args.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export interface ArgsOptions {
6868
// (see the snyk-mvn-plugin or snyk-gradle-plugin)
6969
_doubleDashArgs: string[];
7070
_: MethodArgs;
71-
[key: string]: boolean | string | MethodArgs | string[]; // The two last types are for compatibility only
71+
[key: string]: boolean | string | number | MethodArgs | string[]; // The two last types are for compatibility only
7272
}
7373

7474
export function args(rawArgv: string[]): Args {
@@ -227,6 +227,10 @@ export function args(rawArgv: string[]): Args {
227227
}
228228
}
229229

230+
if (argv.detectionDepth !== undefined) {
231+
argv.detectionDepth = Number(argv.detectionDepth);
232+
}
233+
230234
if (argv.skipUnresolved !== undefined) {
231235
if (argv.skipUnresolved === 'false') {
232236
argv.allowMissing = false;

‎src/cli/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env node
12
import 'source-map-support/register';
23
import * as Debug from 'debug';
34
import * as pathLib from 'path';
@@ -39,6 +40,7 @@ import {
3940
SupportedUserReachableFacingCliArgs,
4041
} from '../lib/types';
4142
import { SarifFileOutputEmptyError } from '../lib/errors/empty-sarif-output-error';
43+
import { InvalidDetectionDepthValue } from '../lib/errors/invalid-detection-depth-value';
4244

4345
const debug = Debug('snyk');
4446
const EXIT_CODES = {
@@ -261,6 +263,14 @@ async function main() {
261263
throw new FileFlagBadInputError();
262264
}
263265

266+
if (
267+
typeof args.options.detectionDepth !== 'undefined' &&
268+
(args.options.detectionDepth <= 0 ||
269+
Number.isNaN(args.options.detectionDepth))
270+
) {
271+
throw new InvalidDetectionDepthValue();
272+
}
273+
264274
validateUnsupportedSarifCombinations(args);
265275

266276
validateOutputFile(args.options, 'json', new JsonFileOutputBadInputError());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { CustomError } from './custom-error';
2+
3+
export class InvalidDetectionDepthValue extends CustomError {
4+
constructor() {
5+
const msg = `Unsupported value for --detection-depth flag. Expected a positive integer.`;
6+
super(msg);
7+
this.code = 422;
8+
this.userMessage = msg;
9+
}
10+
}

0 commit comments

Comments
 (0)
Please sign in to comment.