Skip to content

Commit

Permalink
Merge pull request #19 from marcofranssen/improvements
Browse files Browse the repository at this point in the history
Small improvements and cleanups
  • Loading branch information
marcofranssen authored Oct 7, 2022
2 parents 2792333 + c3d05be commit cfacca9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/krew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { platform, arch, homedir } from "os";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { mapOS, mapArch, download } from "./utils";
import { exec, spawn } from "child_process";
import { exec } from "child_process";

export async function setupKrew() {
const osPlatform = platform();
Expand Down Expand Up @@ -39,7 +39,7 @@ function installKrew(cachedPath: string, binary: string): Promise<string> {
return reject(e);
}

exec("kubectl krew version", (e, stdout) => {
exec(`${cachedPath}/${binary} krew version`, (e, stdout) => {
if (e) {
return resolve("latest");
}
Expand Down
68 changes: 39 additions & 29 deletions lib/setup-kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,47 @@ import { mapOS, mapArch, download } from "./utils";
import { setupKrew } from "./krew";

export async function setupKubectl() {
const kubectlVersion = await getVersion(core.getInput("kubectlVersion"));

core.debug(`Installing kubectl ${kubectlVersion}…`);

const osPlatform = platform();
const osArch = arch();
const p = mapOS(osPlatform);
const a = mapArch(osArch);

const downloadURL = `https://storage.googleapis.com/kubernetes-release/release/${kubectlVersion}/bin/${p}/${a}/kubectl`;
const checksumURL = `https://storage.googleapis.com/kubernetes-release/release/${kubectlVersion}/bin/${p}/${a}/kubectl.sha256`;

let cachedPath = tc.find("kubectl", kubectlVersion, osArch)

if (cachedPath) {
core.info(`Found kubectl ${kubectlVersion} in toolcache @ ${cachedPath}`);
} else {
core.info(`Attempting to download kubectl ${kubectlVersion}…`);
const pathToCLI = await download(downloadURL, checksumURL);
const dir = `${dirname(pathToCLI)}/kubectl-${kubectlVersion}`
await mkdir(dir, { recursive: true })
await rename(pathToCLI, `${dir}/kubectl`)
await chmod(`${dir}/kubectl`, 0o755)
cachedPath = await tc.cacheDir(`${dir}`, "kubectl", kubectlVersion, osArch)
}
try {
const kubectlVersion = await getVersion(core.getInput("kubectlVersion"));

core.debug(`Installing kubectl ${kubectlVersion}…`);

const osPlatform = platform();
const osArch = arch();
const p = mapOS(osPlatform);
const a = mapArch(osArch);

const downloadURL = `https://storage.googleapis.com/kubernetes-release/release/${kubectlVersion}/bin/${p}/${a}/kubectl`;
const checksumURL = `https://storage.googleapis.com/kubernetes-release/release/${kubectlVersion}/bin/${p}/${a}/kubectl.sha256`;

let cachedPath = tc.find("kubectl", kubectlVersion, osArch);

if (cachedPath) {
core.info(`Found kubectl ${kubectlVersion} in toolcache @ ${cachedPath}`);
} else {
core.info(`Attempting to download kubectl ${kubectlVersion}…`);
const pathToCLI = await download(downloadURL, checksumURL);
const dir = `${dirname(pathToCLI)}/kubectl-${kubectlVersion}`;
await mkdir(dir, { recursive: true });
await rename(pathToCLI, `${dir}/kubectl`);
await chmod(`${dir}/kubectl`, 0o755);
cachedPath = await tc.cacheDir(
`${dir}`,
"kubectl",
kubectlVersion,
osArch
);
}

core.addPath(cachedPath);
core.setOutput("kubectl-version", kubectlVersion);
core.addPath(cachedPath);
core.setOutput("kubectl-version", kubectlVersion);

if (core.getInput("enablePlugins")) {
await setupKrew();
if (core.getInput("enablePlugins")) {
await setupKrew();
}
} catch (e) {
core.error(e as Error);
throw e;
}
}

Expand Down

0 comments on commit cfacca9

Please sign in to comment.