-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
97 lines (86 loc) · 2.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require("fs");
const os = require("os");
const path = require("path");
const core = require("@actions/core");
const gh = require("@actions/github");
const tc = require("@actions/tool-cache");
// Leverage the GitHub Action environment variables to authenticate with GitHub
const octokit = new gh.getOctokit(process.env.GITHUB_TOKEN);
// getRelease returns the octokit release object for the given version
async function getRelease(version) {
var release;
try {
if (version === "latest") {
release = await octokit.rest.repos.getLatestRelease({
owner: "hairyhenderson",
repo: "gomplate",
});
} else {
release = await octokit.rest.repos.getReleaseByTag({
owner: "hairyhenderson",
repo: "gomplate",
tag: version,
});
}
} catch (e) {
core.setFailed(e);
}
return release
}
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
function mapArch(arch) {
const mappings = {
x32: "386",
x64: "amd64",
};
return mappings[arch] || arch;
}
// os in [darwin, linux, win32...] (https://nodejs.org/api/os.html#os_os_platform)
// return value in [darwin, linux, windows]
function mapOS(os) {
const mappings = {
win32: "windows",
};
return mappings[os] || os;
}
// getDownloadObject returns an object with the following properties:
// url: the url to download the tool from
// binPath: the local path to the downloaded tool
async function getDownloadObject(version) {
const release = await getRelease(version);
core.debug("release: " + release.data.name);
const asset = release.data.assets.find((asset) =>
asset.name.endsWith(
`gomplate_${mapOS(os.platform())}-${mapArch(os.arch())}`
)
);
core.debug("asset: " + asset)
const url = asset.browser_download_url;
const binPath = path.join("gomplate_linux-amd64");
core.info("url: " + url);
core.info("binPath: " + binPath);
return { url, binPath };
}
// setup downloads the tool and installs it to the given path
async function setup() {
try {
// Get version of tool to be installed
const version = core.getInput("gomplate-version");
core.debug("version: " + version);
// Download the specific version of the tool.
const download = await getDownloadObject(version);
const pathToCLI = await tc.downloadTool(
download.url,
process.env.RUNNER_TEMP + "/gomplate"
);
fs.chmodSync(pathToCLI, 0o755); // make the binary executable
console.log("pathToCLI: " + pathToCLI);
// Expose the tool by adding it to the PATH
core.addPath(process.env.RUNNER_TEMP);
} catch (e) {
core.setFailed(e);
}
}
export default setup;
setup();