From 6196ddd9f56fb8984bc92b9ba2efcee0f27119f4 Mon Sep 17 00:00:00 2001 From: Dmitry Kisler Date: Thu, 12 Oct 2023 17:46:15 +0200 Subject: [PATCH] chore: refactors downloadCLI see: -https://github.com/opentofu/setup-opentofu/pull/2#discussion_r1356503597 - https://github.com/opentofu/setup-opentofu/pull/2#discussion_r1356504741 Signed-off-by: Dmitry Kisler --- dist/index.js | 14 +++++++++----- lib/setup-tofu.js | 14 +++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index 807054b..74b04ae 100644 --- a/dist/index.js +++ b/dist/index.js @@ -145,11 +145,15 @@ function mapOS (os) { return os; } -async function downloadCLI (url) { +async function downloadAndExtractCLI (url) { core.debug(`Downloading OpenTofu CLI from ${url}`); const pathToCLIZip = await tc.downloadTool(url); - let pathToCLI = ''; + if (!pathToCLIZip) { + throw new Error(`Unable to download OpenTofu from ${url}`); + } + + let pathToCLI; core.debug('Extracting OpenTofu CLI zip file'); if (os.platform().startsWith('win')) { @@ -164,8 +168,8 @@ async function downloadCLI (url) { core.debug(`OpenTofu CLI path is ${pathToCLI}.`); - if (!pathToCLIZip || !pathToCLI) { - throw new Error(`Unable to download OpenTofu from ${url}`); + if (!pathToCLI) { + throw new Error('Unable to unzip OpenTofu'); } return pathToCLI; @@ -254,7 +258,7 @@ async function run () { } // Download requested version - const pathToCLI = await downloadCLI(build.url); + const pathToCLI = await downloadAndExtractCLI(build.url); // Install our wrapper if (wrapper) { diff --git a/lib/setup-tofu.js b/lib/setup-tofu.js index 0b1d4c8..f21fb6c 100644 --- a/lib/setup-tofu.js +++ b/lib/setup-tofu.js @@ -34,11 +34,15 @@ function mapOS (os) { return os; } -async function downloadCLI (url) { +async function downloadAndExtractCLI (url) { core.debug(`Downloading OpenTofu CLI from ${url}`); const pathToCLIZip = await tc.downloadTool(url); - let pathToCLI = ''; + if (!pathToCLIZip) { + throw new Error(`Unable to download OpenTofu from ${url}`); + } + + let pathToCLI; core.debug('Extracting OpenTofu CLI zip file'); if (os.platform().startsWith('win')) { @@ -53,8 +57,8 @@ async function downloadCLI (url) { core.debug(`OpenTofu CLI path is ${pathToCLI}.`); - if (!pathToCLIZip || !pathToCLI) { - throw new Error(`Unable to download OpenTofu from ${url}`); + if (!pathToCLI) { + throw new Error('Unable to unzip OpenTofu'); } return pathToCLI; @@ -143,7 +147,7 @@ async function run () { } // Download requested version - const pathToCLI = await downloadCLI(build.url); + const pathToCLI = await downloadAndExtractCLI(build.url); // Install our wrapper if (wrapper) {