Skip to content

Commit

Permalink
refactor: Coerce --detection-depth into number
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Nov 18, 2020
1 parent a4e29be commit 63896d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export interface ArgsOptions {
// (see the snyk-mvn-plugin or snyk-gradle-plugin)
_doubleDashArgs: string[];
_: MethodArgs;
[key: string]: boolean | string | MethodArgs | string[]; // The two last types are for compatibility only
[key: string]: boolean | string | number | MethodArgs | string[]; // The two last types are for compatibility only
}

export function args(rawArgv: string[]): Args {
Expand Down Expand Up @@ -227,6 +227,10 @@ export function args(rawArgv: string[]): Args {
}
}

if (argv.detectionDepth !== undefined) {
argv.detectionDepth = Number(argv.detectionDepth);
}

if (argv.skipUnresolved !== undefined) {
if (argv.skipUnresolved === 'false') {
argv.allowMissing = false;
Expand Down
10 changes: 10 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import 'source-map-support/register';
import * as Debug from 'debug';
import * as pathLib from 'path';
Expand Down Expand Up @@ -39,6 +40,7 @@ import {
SupportedUserReachableFacingCliArgs,
} from '../lib/types';
import { SarifFileOutputEmptyError } from '../lib/errors/empty-sarif-output-error';
import { InvalidDetectionDepthValue } from '../lib/errors/invalid-detection-depth-value';

const debug = Debug('snyk');
const EXIT_CODES = {
Expand Down Expand Up @@ -261,6 +263,14 @@ async function main() {
throw new FileFlagBadInputError();
}

if (
typeof args.options.detectionDepth !== 'undefined' &&
(args.options.detectionDepth <= 0 ||
Number.isNaN(args.options.detectionDepth))
) {
throw new InvalidDetectionDepthValue();
}

validateUnsupportedSarifCombinations(args);

validateOutputFile(args.options, 'json', new JsonFileOutputBadInputError());
Expand Down
10 changes: 10 additions & 0 deletions src/lib/errors/invalid-detection-depth-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { CustomError } from './custom-error';

export class InvalidDetectionDepthValue extends CustomError {
constructor() {
const msg = `Unsupported value for --detection-depth flag. Expected a positive integer.`;
super(msg);
this.code = 422;
this.userMessage = msg;
}
}

0 comments on commit 63896d1

Please sign in to comment.