refactor: enhance error handling

This commit is contained in:
peaceiris 2019-09-21 02:47:29 +09:00
parent 67d5fb9f50
commit 8df3468b4d
4 changed files with 54 additions and 33 deletions

View file

@ -1,17 +1,25 @@
function getURL(os, extended, version) {
let extendedStr = "";
let ext = "tar.gz";
const extendedStr = extended => {
if (extended === true) {
return "extended_";
} else {
return "";
// } else {
// throw new Error(`Invalid input (extended): ${extended}`);
}
};
if (extended === true) {
extendedStr = "extended_";
}
const ext = os => {
if (os === "Windows") {
return "zip";
} else {
return "tar.gz";
}
};
if (os === "Windows") {
ext = "zip";
}
const hugoName = `hugo_${extendedStr}${version}_${os}-64bit`;
const url = `https://github.com/gohugoio/hugo/releases/download/v${version}/${hugoName}.${ext}`;
const hugoName = `hugo_${extendedStr(extended)}${version}_${os}-64bit`;
const baseURL = "https://github.com/gohugoio/hugo/releases/download";
const url = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
return url;
}

View file

@ -5,30 +5,34 @@ const getOS = require("./get-os");
const getURL = require("./get-url");
async function installer(version) {
const extended = core.getInput("extended");
console.log(`Hugo extended: ${extended}`);
try {
const extended = core.getInput("extended");
console.log(`Hugo extended: ${extended}`);
const osName = getOS(process.platform);
console.log(`Operating System: ${osName}`);
const osName = getOS(process.platform);
console.log(`Operating System: ${osName}`);
const hugoURL = getURL(osName, extended, version);
core.debug(`hugoURL: ${hugoURL}`);
const hugoURL = getURL(osName, extended, version);
core.debug(`hugoURL: ${hugoURL}`);
const hugoPath = `${process.env.HOME}/bin`;
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
const hugoPath = `${process.env.HOME}/bin`;
await io.mkdirP(hugoPath);
core.addPath(hugoPath);
// Download and extract Hugo binary
const hugoAssets = await tc.downloadTool(hugoURL);
let hugoBin = "";
if (osName === "Windows") {
const hugoExtractedFolder = await tc.extractZip(hugoAssets, "/tmp");
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
} else {
const hugoExtractedFolder = await tc.extractTar(hugoAssets, "/tmp");
hugoBin = `${hugoExtractedFolder}/hugo`;
// Download and extract Hugo binary
const hugoAssets = await tc.downloadTool(hugoURL);
let hugoBin = "";
if (osName === "Windows") {
const hugoExtractedFolder = await tc.extractZip(hugoAssets, "/tmp");
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
} else {
const hugoExtractedFolder = await tc.extractTar(hugoAssets, "/tmp");
hugoBin = `${hugoExtractedFolder}/hugo`;
}
await io.mv(hugoBin, hugoPath);
} catch (error) {
core.setFailed(error.message);
}
await io.mv(hugoBin, hugoPath);
}
module.exports = installer;