Skip to content

Commit

Permalink
CM-27187 - Migrate to the new architecture that auto-manages the CLI (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Mar 22, 2024
1 parent 06437e0 commit c3b9fda
Show file tree
Hide file tree
Showing 26 changed files with 989 additions and 412 deletions.
6 changes: 5 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
.vscode/**
.vscode-test/**
.idea/**
.github/**
out/**
node_modules/**
src/**
.gitignore
.eslintignore
.yarnrc
webpack.config.js
vsc-extension-quickstart.md
*.md
**/tsconfig.json
**/.eslintrc.json
**/*.map
**/*.ts
CODEOWNERS
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v1.5.0]

- Migrate to the new architecture that auto-manages the CLI

## [v1.4.0]

- Add the on-demand scan of an entire project for Secrets
Expand Down Expand Up @@ -38,6 +42,8 @@

The first stable release with the support of Secrets, SCA, TreeView, Violation Card, and more.

[v1.5.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.5.0

[v1.4.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.4.0

[v1.3.0]: https://github.com/cycodehq/vscode-extension/releases/tag/v1.3.0
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/cycodehq/vscode-extension"
},
"homepage": "https://cycode.com/",
"version": "1.4.0",
"version": "1.5.0",
"publisher": "cycode",
"engines": {
"vscode": "^1.63.0"
Expand Down Expand Up @@ -84,11 +84,17 @@
"title": "Cycode",
"id": "cycode",
"properties": {
"cycode.cliAutoManaged": {
"type": "boolean",
"default": "true",
"description": "Enable executable auto-management.",
"markdownDescription": "Enable executable auto-management."
},
"cycode.cliPath": {
"type": "string",
"default": "",
"description": "Path to the Cycode CLI executable. Run `which cycode` in your terminal to find the path.",
"markdownDescription": "Path to the Cycode CLI executable. Run `which cycode` in your terminal to find the path."
"description": "Path to the Cycode CLI executable.",
"markdownDescription": "Path to the Cycode CLI executable."
},
"cycode.scanOnSave": {
"type": "boolean",
Expand Down Expand Up @@ -218,6 +224,7 @@
"test": "node ./out/test/runTest.js"
},
"devDependencies": {
"@types/decompress": "^4.2.7",
"@types/glob": "8.1.0",
"@types/mocha": "10.0.1",
"@types/node": "16.x",
Expand All @@ -238,6 +245,7 @@
"webpack-cli": "5.0.1"
},
"dependencies": {
"decompress": "^4.2.1",
"semver": "7.5.4",
"shelljs": "0.8.5",
"showdown": "^2.1.0"
Expand Down
65 changes: 8 additions & 57 deletions src/cli-wrapper/cli-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as vscode from 'vscode';
import {IgnoreCommandConfig} from '../types/commands';
import {CliCommands, CommandParameters, getScanTypeCliValue} from './constants';
import {IConfig, RunCliResult, UserAgent} from './types';
import {getRunnableCliCommand} from './runner';
import {experimentalScaSyncFlowProperty, extensionId} from '../utils/texts';

export const generateUserAgentCommandParam = (config: IConfig) => {
const generateUserAgentCommandParam = (config: IConfig) => {
const userAgent: UserAgent = {
app_name: config.agentName,
app_version: config.agentVersion,
Expand All @@ -19,33 +17,22 @@ export const generateUserAgentCommandParam = (config: IConfig) => {
};

export const cliWrapper = {
getRunnableGetVersionLegacyCommand: (params: {
config: IConfig;
workspaceFolderPath: string;
}): RunCliResult => {
// support for legacy versions of the CLI (< 1.0.0)
const {config, workspaceFolderPath} = params;
const {cliPath, cliEnv} = config;

return getRunnableCliCommand({
cliPath,
workspaceFolderPath,
commandParams: [CommandParameters.Version],
cliEnv,
printToOutput: true,
});
},
getRunnableGetVersionCommand: (params: {
config: IConfig;
workspaceFolderPath?: string;
}): RunCliResult => {
const {config, workspaceFolderPath} = params;
const {cliPath, cliEnv} = config;

const commandParams: string[] = [];
commandParams.push(generateUserAgentCommandParam(config));
commandParams.push(CommandParameters.OutputFormatJson);
commandParams.push(CliCommands.Version);

return getRunnableCliCommand({
cliPath,
workspaceFolderPath,
commandParams: [CliCommands.Version],
commandParams,
cliEnv,
printToOutput: true,
});
Expand Down Expand Up @@ -96,9 +83,7 @@ export const cliWrapper = {
commandParams.push(CommandParameters.scanType);
commandParams.push(CommandParameters.SCAScanType);

const experimentalScaSyncFlowPropertyEnabled =
vscode.workspace.getConfiguration(extensionId).get(experimentalScaSyncFlowProperty);
if (experimentalScaSyncFlowPropertyEnabled) {
if (config.experimentalScaSyncFlow) {
// TODO(MarshalX): remove experimental setting if stable
commandParams.push(CommandParameters.Sync);
commandParams.push(CommandParameters.NoRestore);
Expand Down Expand Up @@ -145,40 +130,6 @@ export const cliWrapper = {

return getRunnableCliCommand({cliPath, cliEnv, commandParams});
},
getRunnablePipInstallCommand: (params: {
config: IConfig;
workspaceFolderPath?: string;
}): RunCliResult => {
const commandParams: string[] = [];
const {config, workspaceFolderPath} = params;
const {cliEnv} = config;
commandParams.push('install');
commandParams.push('--upgrade');
commandParams.push('cycode');

return getRunnableCliCommand({
cliPath: 'pip',
workspaceFolderPath,
commandParams,
cliEnv,
printToOutput: true,
});
},
getRunnablePipUninstallCommand: (params: {
config: IConfig;
workspaceFolderPath?: string;
}): RunCliResult => {
const {config, workspaceFolderPath} = params;
const {cliEnv} = config;

return getRunnableCliCommand({
cliPath: 'pip3',
workspaceFolderPath,
commandParams: ['uninstall', '-y', 'cycode'],
cliEnv,
printToOutput: true,
});
},
getRunnableIgnoreCommand: (params: {
config: IConfig;
workspaceFolderPath?: string;
Expand Down
2 changes: 0 additions & 2 deletions src/cli-wrapper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export enum CommandParameters {
NoRestore = '--no-restore',
}

export const MinCLIVersion = '1.9.0';

const SCAN_TYPE_TO_SCAN_TYPE_CLI_FLAG_VALUE = {
[ScanType.Secrets]: 'secret',
[ScanType.Sca]: 'sca',
Expand Down
3 changes: 3 additions & 0 deletions src/cli-wrapper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ export type CommandResult = {

export interface IConfig {
cliPath: string;
cliAutoManaged: boolean;
additionalParams: string[];
cliEnv: { [key: string]: string };
agentName: string;
agentVersion: string;
envName: string;
envVersion: string;
scanOnSaveEnabled: boolean;
experimentalScaSyncFlow: boolean;
}

export type CliConfig = {
Expand Down
32 changes: 31 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as path from 'path';
import {getContext} from './utils/context';

// keep in lowercase.
// eslint-disable-next-line max-len
// source: https://github.com/cycodehq/cycode-cli/blob/ec8333707ab2590518fd0f36454c8636ccbf1061/cycode/cli/consts.py#L50-L82
Expand Down Expand Up @@ -56,7 +59,7 @@ const _SCA_CONFIGURATION_SCAN_LOCK_FILE_TO_PACKAGE_FILE: { [key: string]: string
};

const _SCA_CONFIGURATION_SCAN_SUPPORTED_LOCK_FILES: ReadonlyArray<string> =
Object.keys(_SCA_CONFIGURATION_SCAN_LOCK_FILE_TO_PACKAGE_FILE);
Object.keys(_SCA_CONFIGURATION_SCAN_LOCK_FILE_TO_PACKAGE_FILE);

export const isSupportedPackageFile = (fileName: string): boolean => {
const lowerCaseFileName = fileName.toLowerCase();
Expand Down Expand Up @@ -110,3 +113,30 @@ export const getScanTypeDisplayName = (scanType: string): string => {
};

export const DIAGNOSTIC_CODE_SEPARATOR = '::';

export const REQUIRED_CLI_VERSION = '1.9.1';

export const CLI_GITHUB = {
OWNER: 'cycodehq',
REPO: 'cycode-cli',
TAG: `v${REQUIRED_CLI_VERSION}`,
};

export const CLI_CHECK_NEW_VERSION_EVERY_SEC = 24 * 60 * 60; // 24 hours

export const getPluginPath = (): string => {
return path.join(getContext().extensionPath, 'cycode-vscode-extension');
};

export const getDefaultCliPath = (): string => {
if (process.platform === 'win32') {
return path.join(getPluginPath(), 'cycode.exe');
}

if (process.platform === 'darwin') {
// on macOS, we are always using onedir mode because of gatekeeper
return path.join(getPluginPath(), 'cycode-cli', 'cycode-cli');
}

return path.join(getPluginPath(), 'cycode');
};
Loading

0 comments on commit c3b9fda

Please sign in to comment.