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
|
needs: test
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
os: ['ubuntu-18.04']
|
# os: ['ubuntu-18.04', 'macOS-10.14']
|
||||||
# os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019']
|
os: ['ubuntu-18.04', 'macOS-10.14', 'windows-2019']
|
||||||
# hugo-version: ['latest']
|
# hugo-version: ['latest']
|
||||||
# extended: [true]
|
# extended: [true]
|
||||||
hugo-version: ['latest', '0.58.2']
|
hugo-version: ['latest', '0.58.2']
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,23 @@ const getURL = require("../lib/get-url");
|
||||||
|
|
||||||
describe("getURL()", () => {
|
describe("getURL()", () => {
|
||||||
test("test", () => {
|
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 urlLinux = `${baseURL}/hugo_0.58.2_Linux-64bit.tar.gz`;
|
||||||
const urlLinuxExtended = `${baseURL}/hugo_extended_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 urlMacOS = `${baseURL}/hugo_0.58.2_macOS-64bit.tar.gz`;
|
||||||
const urlWindows = `${baseURL}/hugo_0.58.2_Windows-64bit.zip`;
|
const urlWindows = `${baseURL}/hugo_0.58.2_Windows-64bit.zip`;
|
||||||
expect(getURL("Linux", false, "0.58.2")).toBe(urlLinux);
|
expect(getURL("Linux", false, "0.58.2")).toBe(urlLinux);
|
||||||
expect(getURL("Linux", true, "0.58.2")).toBe(urlLinuxExtended);
|
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("macOS", false, "0.58.2")).toBe(urlMacOS);
|
||||||
expect(getURL("Windows", false, "0.58.2")).toBe(urlWindows);
|
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) {
|
function getURL(os, extended, version) {
|
||||||
let extendedStr = "";
|
const extendedStr = extended => {
|
||||||
let ext = "tar.gz";
|
if (extended === true) {
|
||||||
|
return "extended_";
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
// } else {
|
||||||
|
// throw new Error(`Invalid input (extended): ${extended}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (extended === true) {
|
const ext = os => {
|
||||||
extendedStr = "extended_";
|
if (os === "Windows") {
|
||||||
}
|
return "zip";
|
||||||
|
} else {
|
||||||
|
return "tar.gz";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (os === "Windows") {
|
const hugoName = `hugo_${extendedStr(extended)}${version}_${os}-64bit`;
|
||||||
ext = "zip";
|
const baseURL = "https://github.com/gohugoio/hugo/releases/download";
|
||||||
}
|
const url = `${baseURL}/v${version}/${hugoName}.${ext(os)}`;
|
||||||
|
|
||||||
const hugoName = `hugo_${extendedStr}${version}_${os}-64bit`;
|
|
||||||
const url = `https://github.com/gohugoio/hugo/releases/download/v${version}/${hugoName}.${ext}`;
|
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,30 +5,34 @@ const getOS = require("./get-os");
|
||||||
const getURL = require("./get-url");
|
const getURL = require("./get-url");
|
||||||
|
|
||||||
async function installer(version) {
|
async function installer(version) {
|
||||||
const extended = core.getInput("extended");
|
try {
|
||||||
console.log(`Hugo extended: ${extended}`);
|
const extended = core.getInput("extended");
|
||||||
|
console.log(`Hugo extended: ${extended}`);
|
||||||
|
|
||||||
const osName = getOS(process.platform);
|
const osName = getOS(process.platform);
|
||||||
console.log(`Operating System: ${osName}`);
|
console.log(`Operating System: ${osName}`);
|
||||||
|
|
||||||
const hugoURL = getURL(osName, extended, version);
|
const hugoURL = getURL(osName, extended, version);
|
||||||
core.debug(`hugoURL: ${hugoURL}`);
|
core.debug(`hugoURL: ${hugoURL}`);
|
||||||
|
|
||||||
const hugoPath = `${process.env.HOME}/bin`;
|
const hugoPath = `${process.env.HOME}/bin`;
|
||||||
await io.mkdirP(hugoPath);
|
await io.mkdirP(hugoPath);
|
||||||
core.addPath(hugoPath);
|
core.addPath(hugoPath);
|
||||||
|
|
||||||
// Download and extract Hugo binary
|
// Download and extract Hugo binary
|
||||||
const hugoAssets = await tc.downloadTool(hugoURL);
|
const hugoAssets = await tc.downloadTool(hugoURL);
|
||||||
let hugoBin = "";
|
let hugoBin = "";
|
||||||
if (osName === "Windows") {
|
if (osName === "Windows") {
|
||||||
const hugoExtractedFolder = await tc.extractZip(hugoAssets, "/tmp");
|
const hugoExtractedFolder = await tc.extractZip(hugoAssets, "/tmp");
|
||||||
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
|
hugoBin = `${hugoExtractedFolder}/hugo.exe`;
|
||||||
} else {
|
} else {
|
||||||
const hugoExtractedFolder = await tc.extractTar(hugoAssets, "/tmp");
|
const hugoExtractedFolder = await tc.extractTar(hugoAssets, "/tmp");
|
||||||
hugoBin = `${hugoExtractedFolder}/hugo`;
|
hugoBin = `${hugoExtractedFolder}/hugo`;
|
||||||
|
}
|
||||||
|
await io.mv(hugoBin, hugoPath);
|
||||||
|
} catch (error) {
|
||||||
|
core.setFailed(error.message);
|
||||||
}
|
}
|
||||||
await io.mv(hugoBin, hugoPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = installer;
|
module.exports = installer;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue