Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions src/cli-wrapper/cli-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandParams, IgnoreCommandConfig } from "../types/commands";
import { IgnoreCommandConfig } from "../types/commands";
import { CliCommands, CommandParameters } from "./constants";
import { CommandResult, IConfig, UserAgent } from "./types";
import { runCli } from "./runner";
Expand Down Expand Up @@ -70,6 +70,7 @@ export const cliWrapper = {
workspaceFolderPath,
commandParams,
cliEnv,
printToOutput: true,
});
},
runScaScan: async (params: {
Expand Down Expand Up @@ -97,7 +98,8 @@ export const cliWrapper = {
cliPath,
workspaceFolderPath,
commandParams,
cliEnv
cliEnv,
printToOutput: true,
});
},
runAuth: async (params: {
Expand Down Expand Up @@ -164,21 +166,6 @@ export const cliWrapper = {
printToOutput: true,
});
},
runUsage: async (params: {
config: IConfig;
workspaceFolderPath?: string;
}): Promise<CommandResult> => {
const { config, workspaceFolderPath } = params;
const { cliEnv, cliPath } = config;

return await runCli({
cliPath,
workspaceFolderPath,
commandParams: [CommandParameters.Usage],
cliEnv,
printToOutput: true,
});
},
runIgnore: async (params: {
config: IConfig;
workspaceFolderPath?: string;
Expand All @@ -201,6 +188,7 @@ export const cliWrapper = {
workspaceFolderPath,
commandParams,
cliEnv,
printToOutput: true,
});
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/cli-wrapper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ export enum CommandParameters {
SCAScanType = "sca",
}

export const MinCLIVersion = "1.0.0";
export const MinCLIVersion = "1.1.1";
24 changes: 13 additions & 11 deletions src/cli-wrapper/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ interface RunCliArgs {

let childProcess: ChildProcess | undefined = undefined;

const parseResult = (stdout: string): string | object => {
const parseResult = (out: string): object => {
let result = {};
try {
if (stdout) {
result = JSON.parse(stdout);
if (out) {
result = JSON.parse(out);
}
} catch (error) {
result = { data: stdout };
result = { data: out };
}

return result;
Expand All @@ -34,7 +34,7 @@ export const runCli = (args: RunCliArgs): Promise<CommandResult> => {
`Running command: "${cliPath} ${commandParams.join(" ")}"`
);

return new Promise((resolve, reject) => {
return new Promise((resolve, _) => {
let stderr = "";
let stdout = "";

Expand All @@ -58,31 +58,33 @@ export const runCli = (args: RunCliArgs): Promise<CommandResult> => {
});

childProcess.on("exit", (code: number) => {
extensionOutput.info(`Command exited with code: ${code}`);
resolve({
exitCode: code,
error: stderr,
stderr: stderr,
result: parseResult(stdout),
});
});

childProcess.on("error", (error: { errno: number }) => {
handleErrorOutput(error);

resolve({
exitCode: error.errno,
error: stderr,
stderr: stderr,
result: parseResult(stderr),
});
});

childProcess.stdout?.on("data", (data) => {
if (printToOutput) {
extensionOutput.info(`Command stdout: ${data.toString()}`);
}

if (!data) {
return;
}

stdout += data.toString();
if (printToOutput) {
extensionOutput.info(data.toString());
}
});

childProcess.stderr?.on("data", handleErrorOutput);
Expand Down
2 changes: 1 addition & 1 deletion src/cli-wrapper/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type CommandResult = {
exitCode: number;
result: any;
error: string;
stderr: string;
};

export interface IConfig {
Expand Down
7 changes: 4 additions & 3 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export async function auth(params: CommandParams) {
message: `Authenticating with Cycode...`,
});

const { result, error, exitCode } = await cliWrapper.runAuth(params);
const { result, stderr, exitCode } = await cliWrapper.runAuth(params);

endAuthenticationProcess();

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

handleAuthStatus(exitCode, result, error);
handleAuthStatus(exitCode, result, stderr);
} catch (error) {
extensionOutput.error("Error while creating scan: " + error);
onAuthFailure();
Expand All @@ -45,6 +45,7 @@ export async function auth(params: CommandParams) {
}

function handleAuthStatus(exitCode: number, result: any, error: string) {
// TODO(MarshalX): support JSON output
if (exitCode !== 0 || (result.data && result.data.includes("failed"))) {
onAuthFailure();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/services/auth_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export async function authCheck(config: IConfig): Promise<boolean> {

const authCheckResult = await cliWrapper.runAuthCheck(config);
const {
error,
stderr,
exitCode,
result: { result: isAuthenticated },
} = authCheckResult;

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
throw new Error("Failed to check auth status");
}

Expand Down
15 changes: 5 additions & 10 deletions src/services/ignore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { IConfig } from "../cli-wrapper/types";
import { IgnoreCommandConfig } from "../types/commands";
import { secretScan } from "./scanner";
import TrayNotifications from "../utils/TrayNotifications";
import { TreeView } from '../providers/tree-view/types';
import { CommandParameters } from '../cli-wrapper/constants';
import { TreeView } from "../providers/tree-view/types";
import { CommandParameters } from "../cli-wrapper/constants";

export async function ignore(
context: vscode.ExtensionContext,
Expand All @@ -23,22 +23,18 @@ export async function ignore(
}
) {
try {
const { result, error, exitCode } = await cliWrapper.runIgnore(params);
const { stderr, exitCode } = await cliWrapper.runIgnore(params);

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

// throw error
if (exitCode !== 0) {
throw new Error(error);
throw new Error(stderr);
}

onIgnoreComplete();
extensionOutput.info(
"Ignore completed: " + JSON.stringify({ result, error }, null, 3)
);

if (params.ignoreConfig.ignoreBy === CommandParameters.ByPath) {
params.diagnosticCollection.delete(params.documentInitiatedIgnore.uri);
params.treeView.provider.excludeViolationsByPath(params.ignoreConfig.param);
Expand All @@ -56,7 +52,6 @@ export async function ignore(
},
params.treeView
);

} catch (error) {
extensionOutput.error("Error while ignoring: " + error);
onIgnoreFailed();
Expand Down
9 changes: 3 additions & 6 deletions src/services/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ export async function install(
message: `Install with pip3...`,
});

const { result, error, exitCode } = await cliWrapper.runInstall(params);
const { stderr, exitCode } = await cliWrapper.runInstall(params);

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

// throw error
if (exitCode !== 0) {
throw new Error(error);
throw new Error(stderr);
}

onInstallComplete();
extensionOutput.info(
"Install completed: " + JSON.stringify({ result, error }, null, 3)
);
} catch (error) {
extensionOutput.error("Error while installing: " + error);
onInstallFailed();
Expand Down
24 changes: 10 additions & 14 deletions src/services/scaScanner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as path from 'path';
import * as path from "path";
import * as vscode from "vscode";
import { extensionOutput } from "../logging/extension-output";
import { cliWrapper } from "../cli-wrapper/cli-wrapper";
Expand All @@ -9,14 +9,14 @@ import {
extensionId,
} from "../utils/texts";
import { validateCliCommonErrors } from "./common";
import { getWorkspaceState, setContext, updateWorkspaceState } from '../utils/context';
import { getWorkspaceState, setContext, updateWorkspaceState } from "../utils/context";
import { ScaDetection } from "../types/detection";
import { IConfig } from "../cli-wrapper/types";
import TrayNotifications from "../utils/TrayNotifications";
import { TreeView } from '../providers/tree-view/types';
import { refreshTreeViewData } from '../providers/tree-view/utils';
import { getPackageFileForLockFile, isSupportedLockFile, ScanType } from '../constants';
import { VscodeStates } from '../utils/states';
import { TreeView } from "../providers/tree-view/types";
import { refreshTreeViewData } from "../providers/tree-view/utils";
import { getPackageFileForLockFile, isSupportedLockFile, ScanType } from "../constants";
import { VscodeStates } from "../utils/states";


interface ScaScanParams {
Expand Down Expand Up @@ -84,23 +84,19 @@ const _runCliScaScan = async (params: ScaScanParams): Promise<any> => {
config: params.config,
};

const { result, error, exitCode } = await cliWrapper.runScaScan(
const { result, stderr, exitCode } = await cliWrapper.runScaScan(
cliParams
);

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

// Check if an error occurred
if (error && !result.detections?.length) {
throw new Error(error);
if (result.errors?.length || !result.detections?.length) {
throw new Error(result.errors || stderr);
}

extensionOutput.info(
"Scan complete: " + JSON.stringify({ result, error }, null, 3)
);

return result;
};

Expand Down
12 changes: 4 additions & 8 deletions src/services/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,23 @@ export async function secretScan(
workspaceFolderPath: params.workspaceFolderPath,
config: params.config,
};
const { result, error, exitCode } = await cliWrapper.runScan(cliParams);
const { result, stderr, exitCode } = await cliWrapper.runScan(cliParams);

updateWorkspaceState(VscodeStates.SecretsScanInProgress, false);

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

// Check if an error occurred
if (error && !result.detections?.length) {
throw new Error(error);
if (result.errors?.length || !result.detections?.length) {
throw new Error(result.errors || stderr);
}

if (result.error) {
throw new Error(result.message);
}

extensionOutput.info(
"Scan complete: " + JSON.stringify({ result, error }, null, 3)
);

// Show in "problems" tab
handleScanDetections(
result,
Expand Down
9 changes: 3 additions & 6 deletions src/services/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ export async function uninstall(
message: `Uninstall cycode CLI...`,
});

const { result, error, exitCode } = await cliWrapper.runUninstall(
const { stderr, exitCode } = await cliWrapper.runUninstall(
params
);

if (validateCliCommonErrors(error, exitCode)) {
if (validateCliCommonErrors(stderr, exitCode)) {
return;
}

// throw error
if (exitCode !== 0) {
throw new Error(error);
throw new Error(stderr);
}

onUninstallComplete();
extensionOutput.info(
"Uninstall completed: " + JSON.stringify({ result, error }, null, 3)
);
} catch (error) {
extensionOutput.error("Error while uninstalling: " + error);
onUninstallFailed();
Expand Down