Skip to content

Commit

Permalink
fix: allow the internal cmd call helm template receive options
Browse files Browse the repository at this point in the history
 - generate `helm template` with options if it was pass in helm snyk pluign call
 - refctoring to improve code
  • Loading branch information
Arthur Granado committed Nov 13, 2019
1 parent 2eaee7e commit cc29185
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/test-cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let consoleMock;
beforeEach(() => {
//@ts-ignore
mockProcessExit = jest.spyOn(process, "exit").mockImplementation(code => {});
consoleMock = jest.spyOn(console, 'error').mockImplementation(jest.fn());
consoleMock = jest.spyOn(console, "error").mockImplementation(jest.fn());
});

afterEach(() => {
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("test command", () => {
describe("check required input directory", () => {
test("process exit if there is no <chart-directory> required arg", () => {
const inputArgs = ["test"];

parseInputParameters(inputArgs);

expect(mockProcessExit).toHaveBeenCalledWith(1);
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/test-main-runCommand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IExecCommandResult } from "../main";
import { ExecException } from "child_process";
import fs from "fs";
import { IArgs } from "../cli-args";

beforeEach(() => {
jest.resetModules();
Expand Down Expand Up @@ -59,6 +60,37 @@ test("validate stderr captured", async () => {
expect(res.stderr.trim()).toBe("error");
});

describe("Assemble helm template command", () => {
// IArgs -> String
// produce the `helm template` command with or without options
test("command without options for helm template", () => {
const options = { inputDirectory: "." } as IArgs;
const expectedCommand = "helm template .";

const command = mainModule.assembleHelmTemplateCommand(options);

expect(command).toEqual(expectedCommand);
});

test("command with empty options for helm template", () => {
const options = { inputDirectory: "/tmp", helmTemplateOptions: "" } as IArgs;
const expectedCommand = "helm template /tmp";

const command = mainModule.assembleHelmTemplateCommand(options);

expect(command).toEqual(expectedCommand);
});

test("command with no-empty options for helm template", () => {
const options = { inputDirectory: "/tmp", helmTemplateOptions: "--set stringArray" } as IArgs;
const expectedCommand = "helm template /tmp --set stringArray";

const command = mainModule.assembleHelmTemplateCommand(options);

expect(command).toEqual(expectedCommand);
});
});

describe("Assemble Snyk command", () => {
test("Command without options", () => {
const imageName = "someImage";
Expand Down
33 changes: 18 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function runSnykTestWithDocker(snykToken: string, snykCLIImageName: string
};

const createOptions = {
env: [`SNYK_TOKEN=${snykToken}`, "ENV_FLAGS=disableSuggestions=true"],
env: [`SNYK_TOKEN=${snykToken}`],
Binds: ["/var/run/docker.sock:/var/run/docker.sock"],
Tty: false
};
Expand All @@ -118,19 +118,14 @@ async function runSnykTestWithDocker(snykToken: string, snykCLIImageName: string
});
}

export function assembleSnykCommand(imageName: string, options: any) {
export const assembleSnykCommand = (imageName: string, options: any) => {
let command = `snyk test --docker ${imageName}`;
if (options.json) command = command + " --json";
if (options.debug) console.debug(command);
if (options.debug) console.debug(options);

return command;
}

function loadMultiDocYamlFromString(strMultiDocYaml: string) {
const docs = yaml.safeLoadAll(strMultiDocYaml);
return docs;
}
};

export function isValidImageName(imageName: string): boolean {
// This regex is from https://stackoverflow.com/questions/39671641/regex-to-parse-docker-tag posted 2016-Sept-24, falling under MIT license
Expand Down Expand Up @@ -186,7 +181,8 @@ function getHelmChartLabelForOutput(helmChartDirectory: string): string {
}

export async function mainWithParams(args: IArgs, snykToken: string) {
const helmCommand = `helm template ${args.inputDirectory}`;
const helmCommand = assembleHelmTemplateCommand(args);
if (args.debug) console.debug("helm template command: ", helmCommand);
const helmCommandResObj: IExecCommandResult = await runCommand(helmCommand);
const renderedTemplates = helmCommandResObj.stdout;

Expand Down Expand Up @@ -222,7 +218,14 @@ export async function mainWithParams(args: IArgs, snykToken: string) {
return allOutputData;
}

export function handleResult(helmChartLabel, results: SnykResult[], options) {
export const assembleHelmTemplateCommand = (options: IArgs) => {
let cmd = `helm template ${options.inputDirectory}`;
if (options.helmTemplateOptions) cmd = `${cmd} ${options.helmTemplateOptions}`;

return cmd;
};

export const handleResult = (helmChartLabel, results: SnykResult[], options) => {
let output;
if (options.json) {
output = {
Expand All @@ -238,25 +241,25 @@ export function handleResult(helmChartLabel, results: SnykResult[], options) {
}

return output;
}
};

function chopTheAdditionalSuggestions(result: SnykResult) {
const chopTheAdditionalSuggestions = (result: SnykResult) => {
const preOutput = result.result.split("\n");
const NUMBER_OF_LINES_TO_BE_CUT = 7;
const NUMBER_OF_LINES_TO_BE_KEEP = preOutput.length - NUMBER_OF_LINES_TO_BE_CUT;
const trimmedOutput = preOutput.slice(0, NUMBER_OF_LINES_TO_BE_KEEP);

return trimmedOutput.join("\n");
}
};

export function handleOutput(output, options) {
export const handleOutput = (output, options) => {
if (options.json) output = JSON.stringify(output, null, 2);
if (options.output) {
fs.writeFileSync(options.output, output);
} else {
console.log(output);
}
}
};

async function main() {
const snykToken: string = process.env.SNYK_TOKEN ? process.env.SNYK_TOKEN : "";
Expand Down

0 comments on commit cc29185

Please sign in to comment.