refactor: enhance error handling
This commit is contained in:
parent
67d5fb9f50
commit
8df3468b4d
4 changed files with 54 additions and 33 deletions
4
.github/workflows/test.yml
vendored
4
.github/workflows/test.yml
vendored
|
|
@ -34,8 +34,8 @@ jobs:
|
|||
needs: test
|
||||
strategy:
|
||||
matrix:
|
||||
os: ['ubuntu-18.04']
|
||||
# os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019']
|
||||
# os: ['ubuntu-18.04', 'macOS-10.14']
|
||||
os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019']
|
||||
# hugo-version: ['latest']
|
||||
# extended: [true]
|
||||
hugo-version: ['latest', '0.58.2']
|
||||
|
|
|
|||
|
|
@ -2,14 +2,23 @@ const getURL = require("../lib/get-url");
|
|||
|
||||
describe("getURL()", () => {
|
||||
test("test", () => {
|
||||
const baseURL = "https://github.com/gohugoio/hugo/releases/download/v0.58.2";
|
||||
const baseURL =
|
||||
"https://github.com/gohugoio/hugo/releases/download/v0.58.2";
|
||||
const urlLinux = `${baseURL}/hugo_0.58.2_Linux-64bit.tar.gz`;
|
||||
const urlLinuxExtended = `${baseURL}/hugo_extended_0.58.2_Linux-64bit.tar.gz`;
|
||||
const urlMacOS = `${baseURL}/hugo_0.58.2_macOS-64bit.tar.gz`;
|
||||
const urlWindows = `${baseURL}/hugo_0.58.2_Windows-64bit.zip`;
|
||||
expect(getURL("Linux", false, "0.58.2")).toBe(urlLinux);
|
||||
expect(getURL("Linux", true, "0.58.2")).toBe(urlLinuxExtended);
|
||||
expect(getURL("Linux", "false", "0.58.2")).toBe(urlLinux);
|
||||
expect(getURL("Linux", "true", "0.58.2")).toBe(urlLinux);
|
||||
expect(getURL("macOS", false, "0.58.2")).toBe(urlMacOS);
|
||||
expect(getURL("Windows", false, "0.58.2")).toBe(urlWindows);
|
||||
});
|
||||
|
||||
// test("test exception", () => {
|
||||
// expect(() => {
|
||||
// getURL("Linux", "hoge", "0.58.2");
|
||||
// }).toThrowError("Invalid input (extended): hoge");
|
||||
// });
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue