Skip to content

Commit

Permalink
feat: use processCommandArgs in monitor too
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Mar 9, 2021
1 parent b022dff commit 7acd2aa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/cli/commands/monitor/index.ts
Expand Up @@ -12,15 +12,14 @@ import {
MonitorMeta,
MonitorResult,
Options,
PolicyOptions,
Contributor,
} from '../../../lib/types';
import * as config from '../../../lib/config';
import * as detect from '../../../lib/detect';
import { GoodResult, BadResult } from './types';
import * as spinner from '../../../lib/spinner';
import * as analytics from '../../../lib/analytics';
import { MethodArgs, ArgsOptions } from '../../args';
import { MethodArgs } from '../../args';
import { apiTokenExists } from '../../../lib/api-token';
import { maybePrintDepTree, maybePrintDepGraph } from '../../../lib/print-deps';
import { monitor as snykMonitor } from '../../../lib/monitor';
Expand All @@ -39,6 +38,7 @@ import { FailedToRunTestError, MonitorError } from '../../../lib/errors';
import { isMultiProjectScan } from '../../../lib/is-multi-project-scan';
import { getEcosystem, monitorEcosystem } from '../../../lib/ecosystems';
import { getFormattedMonitorOutput } from '../../../lib/ecosystems/monitor';
import { processCommandArgs } from '../process-command-args';

const SEPARATOR = '\n-------------------------------------------------------\n';
const debug = Debug('snyk');
Expand All @@ -58,19 +58,8 @@ async function promiseOrCleanup<T>(
// Returns an array of Registry responses (one per every sub-project scanned), a single response,
// or an error message.
async function monitor(...args0: MethodArgs): Promise<any> {
let args = [...args0];
let monitorOptions = {};
const { options, paths } = processCommandArgs(...args0);
const results: Array<GoodResult | BadResult> = [];
if (typeof args[args.length - 1] === 'object') {
monitorOptions = args.pop() as ArgsOptions;
}
args = args.filter(Boolean);

// populate with default path (cwd) if no path given
if (args.length === 0) {
args.unshift(process.cwd());
}
const options = monitorOptions as Options & PolicyOptions & MonitorOptions;

if (options.id) {
snyk.id = options.id;
Expand Down Expand Up @@ -99,11 +88,7 @@ async function monitor(...args0: MethodArgs): Promise<any> {

const ecosystem = getEcosystem(options);
if (ecosystem) {
const commandResult = await monitorEcosystem(
ecosystem,
args as string[],
options,
);
const commandResult = await monitorEcosystem(ecosystem, paths, options);

const [monitorResults, monitorErrors] = commandResult;

Expand All @@ -116,7 +101,7 @@ async function monitor(...args0: MethodArgs): Promise<any> {
}

// Part 1: every argument is a scan target; process them sequentially
for (const path of args as string[]) {
for (const path of paths) {
debug(`Processing ${path}...`);
try {
validateMonitorPath(path, options.docker);
Expand Down
2 changes: 2 additions & 0 deletions src/cli/commands/process-command-args.ts
Expand Up @@ -8,10 +8,12 @@ export function processCommandArgs<CommandOptions>(
if (typeof args[args.length - 1] === 'object') {
options = (args.pop() as any) as Options & CommandOptions;
}
args = args.filter(Boolean);

// populate with default path (cwd) if no path given
if (args.length === 0) {
args.unshift(process.cwd());
}

return { options, paths: args };
}

0 comments on commit 7acd2aa

Please sign in to comment.