From e7eef55398f2de0fdb3f42a8f6aaf2fcac35d860 Mon Sep 17 00:00:00 2001 From: Shivam Gupta Date: Wed, 17 Mar 2021 12:19:23 +0530 Subject: [PATCH] Changing order --- lib/run.js | 7 +- src/run.ts | 199 +++++++++++++++++++++++++++-------------------------- 2 files changed, 104 insertions(+), 102 deletions(-) diff --git a/lib/run.js b/lib/run.js index fa01700..3f082b4 100644 --- a/lib/run.js +++ b/lib/run.js @@ -144,6 +144,7 @@ function getLatestHelmVersionFor(type) { } `); console.log(versions); + return stableHelmVersion; }); } function findHelm(rootFolder) { @@ -163,15 +164,15 @@ function run() { if (version.toLocaleLowerCase() === 'latest') { version = yield getStableHelmVersion(); } - else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } else if (version === LATEST_HELM2_VERSION) { version = 'v' + getLatestHelmVersionFor(2); } else if (version === LATEST_HELM3_VERSION) { version = 'v' + getLatestHelmVersionFor(3); } + else if (!version.toLocaleLowerCase().startsWith('v')) { + version = 'v' + version; + } let cachedPath = yield downloadHelm(version); try { if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { diff --git a/src/run.ts b/src/run.ts index 4ac2274..56eed94 100644 --- a/src/run.ts +++ b/src/run.ts @@ -18,92 +18,92 @@ const LATEST_HELM2_VERSION = '2.*'; const LATEST_HELM3_VERSION = '3.*'; function getExecutableExtension(): string { - if (os.type().match(/^Win/)) { - return '.exe'; - } - return ''; + if (os.type().match(/^Win/)) { + return '.exe'; + } + return ''; } function getHelmDownloadURL(version: string): string { - switch (os.type()) { - case 'Linux': - return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version); + switch (os.type()) { + case 'Linux': + return util.format('https://get.helm.sh/helm-%s-linux-amd64.zip', version); - case 'Darwin': - return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version); + case 'Darwin': + return util.format('https://get.helm.sh/helm-%s-darwin-amd64.zip', version); - case 'Windows_NT': - default: - return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version); - } + case 'Windows_NT': + default: + return util.format('https://get.helm.sh/helm-%s-windows-amd64.zip', version); + } } async function getStableHelmVersion(): Promise { - try { - const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl); - const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); - let latestHelmVersion = semver.clean(stableHelmVersion); - responseArray.forEach(response => { - if (response && response.tag_name) { - let currentHelmVerison = semver.clean(response.tag_name.toString()); - if (currentHelmVerison) { - if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) { - //If current helm version is not a pre release and is greater than latest helm version - latestHelmVersion = currentHelmVerison; - } - } - } - }); - latestHelmVersion = "v" + latestHelmVersion; - return latestHelmVersion; - } catch (error) { - core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion)); - } + try { + const downloadPath = await toolCache.downloadTool(helmAllReleasesUrl); + const responseArray = JSON.parse(fs.readFileSync(downloadPath, 'utf8').toString().trim()); + let latestHelmVersion = semver.clean(stableHelmVersion); + responseArray.forEach(response => { + if (response && response.tag_name) { + let currentHelmVerison = semver.clean(response.tag_name.toString()); + if (currentHelmVerison) { + if (currentHelmVerison.toString().indexOf('rc') == -1 && semver.gt(currentHelmVerison, latestHelmVersion)) { + //If current helm version is not a pre release and is greater than latest helm version + latestHelmVersion = currentHelmVerison; + } + } + } + }); + latestHelmVersion = "v" + latestHelmVersion; + return latestHelmVersion; + } catch (error) { + core.warning(util.format("Cannot get the latest Helm info from %s. Error %s. Using default Helm version %s.", helmAllReleasesUrl, error, stableHelmVersion)); + } - return stableHelmVersion; + return stableHelmVersion; } var walkSync = function (dir, filelist, fileToFind) { - var files = fs.readdirSync(dir); - filelist = filelist || []; - files.forEach(function (file) { - if (fs.statSync(path.join(dir, file)).isDirectory()) { - filelist = walkSync(path.join(dir, file), filelist, fileToFind); - } - else { - core.debug(file); - if (file == fileToFind) { - filelist.push(path.join(dir, file)); - } - } - }); - return filelist; + var files = fs.readdirSync(dir); + filelist = filelist || []; + files.forEach(function (file) { + if (fs.statSync(path.join(dir, file)).isDirectory()) { + filelist = walkSync(path.join(dir, file), filelist, fileToFind); + } + else { + core.debug(file); + if (file == fileToFind) { + filelist.push(path.join(dir, file)); + } + } + }); + return filelist; }; async function downloadHelm(version: string): Promise { - if (!version) { version = await getStableHelmVersion(); } - let cachedToolpath = toolCache.find(helmToolName, version); - if (!cachedToolpath) { - let helmDownloadPath; - try { - helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version)); - } catch (exception) { - throw new Error(util.format("Failed to download Helm from location ", getHelmDownloadURL(version))); - } - - fs.chmodSync(helmDownloadPath, '777'); - const unzipedHelmPath = await toolCache.extractZip(helmDownloadPath); - cachedToolpath = await toolCache.cacheDir(unzipedHelmPath, helmToolName, version); + if (!version) { version = await getStableHelmVersion(); } + let cachedToolpath = toolCache.find(helmToolName, version); + if (!cachedToolpath) { + let helmDownloadPath; + try { + helmDownloadPath = await toolCache.downloadTool(getHelmDownloadURL(version)); + } catch (exception) { + throw new Error(util.format("Failed to download Helm from location ", getHelmDownloadURL(version))); } - const helmpath = findHelm(cachedToolpath); - if (!helmpath) { - throw new Error(util.format("Helm executable not found in path ", cachedToolpath)); - } + fs.chmodSync(helmDownloadPath, '777'); + const unzipedHelmPath = await toolCache.extractZip(helmDownloadPath); + cachedToolpath = await toolCache.cacheDir(unzipedHelmPath, helmToolName, version); + } - fs.chmodSync(helmpath, '777'); - return helmpath; + const helmpath = findHelm(cachedToolpath); + if (!helmpath) { + throw new Error(util.format("Helm executable not found in path ", cachedToolpath)); + } + + fs.chmodSync(helmpath, '777'); + return helmpath; } async function getLatestHelmVersionFor(type) { @@ -123,46 +123,47 @@ async function getLatestHelmVersionFor(type) { ` ); console.log(versions); + return stableHelmVersion; } function findHelm(rootFolder: string): string { - fs.chmodSync(rootFolder, '777'); - var filelist: string[] = []; - walkSync(rootFolder, filelist, helmToolName + getExecutableExtension()); - if (!filelist) { - throw new Error(util.format("Helm executable not found in path ", rootFolder)); - } - else { - return filelist[0]; - } + fs.chmodSync(rootFolder, '777'); + var filelist: string[] = []; + walkSync(rootFolder, filelist, helmToolName + getExecutableExtension()); + if (!filelist) { + throw new Error(util.format("Helm executable not found in path ", rootFolder)); + } + else { + return filelist[0]; + } } async function run() { - let version = core.getInput('version', { 'required': true }); - if (version.toLocaleLowerCase() === 'latest') { - version = await getStableHelmVersion(); - } else if (!version.toLocaleLowerCase().startsWith('v')) { - version = 'v' + version; - } else if (version === LATEST_HELM2_VERSION) { - version = 'v' + getLatestHelmVersionFor(2); - } else if (version === LATEST_HELM3_VERSION) { - version = 'v' + getLatestHelmVersionFor(3); + let version = core.getInput('version', { 'required': true }); + if (version.toLocaleLowerCase() === 'latest') { + version = await getStableHelmVersion(); + } else if (version === LATEST_HELM2_VERSION) { + version = 'v' + getLatestHelmVersionFor(2); + } else if (version === LATEST_HELM3_VERSION) { + version = 'v' + getLatestHelmVersionFor(3); + } else if (!version.toLocaleLowerCase().startsWith('v')) { + version = 'v' + version; + } + + let cachedPath = await downloadHelm(version); + + try { + + if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { + core.addPath(path.dirname(cachedPath)); } + } + catch { + //do nothing, set as output variable + } - let cachedPath = await downloadHelm(version); - - try { - - if (!process.env['PATH'].startsWith(path.dirname(cachedPath))) { - core.addPath(path.dirname(cachedPath)); - } - } - catch { - //do nothing, set as output variable - } - - console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`); - core.setOutput('helm-path', cachedPath); + console.log(`Helm tool version: '${version}' has been cached at ${cachedPath}`); + core.setOutput('helm-path', cachedPath); } run().catch(core.setFailed); \ No newline at end of file