forked from pcjun97/action-setup-tfswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (105 loc) · 3.13 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const os = require("os");
const fs = require("fs");
const path = require("path");
const core = require("@actions/core");
const github = require("@actions/github");
const tc = require("@actions/tool-cache");
const owner = "warrensbox";
const repo = "terraform-switcher";
function getPlatform() {
const platform = os.platform();
const mappings = {
win32: "windows",
};
return mappings[platform] || platform;
}
function getArch() {
const arch = os.arch();
if (arch === "arm") {
return `armv${process.config.variables.arm_version}`;
}
const mappings = {
x32: "386",
x64: "amd64",
};
return mappings[arch] || arch;
}
async function getRelease(token, tag) {
try {
const octokit = new github.getOctokit(token);
let release;
if (tag && tag !== "latest") {
core.info(`checking GitHub for tag '${tag}'`);
release = await octokit.rest.repos.getReleaseByTag({
owner,
repo,
tag,
});
} else {
core.info(`checking GitHub for latest tag`);
release = await octokit.rest.repos.getLatestRelease({
owner,
repo,
});
}
return release;
} catch (err) {
if (err.status === 404) {
throw new Error(
`unable to find '${tag}' - use 'latest' or see https://github.com/${owner}/${repo}/releases for details`
);
} else {
throw err;
}
}
}
function getAsset(release, platform, arch, tag) {
const suffix = platform === "windows" ? "zip" : "tar.gz";
const version = release.data.tag_name;
const name = `terraform-switcher_${version}_${platform}_${arch}.${suffix}`;
const asset = release.data.assets.find((asset) => asset.name === name);
if (!asset) {
throw new Error(
`platform ${platform}/${arch} is not supported for version ${version}. Please file a request at https://github.com/${owner}/${repo}/issues/new`
);
}
return asset;
}
async function download(url) {
const pathDownload = await tc.downloadTool(url);
core.info(pathDownload);
let pathExtract;
if (os.platform().startsWith("win")) {
pathExtract = await tc.extractZip(pathDownload);
} else {
pathExtract = await tc.extractTar(pathDownload);
}
core.info(pathExtract);
if (!pathDownload || !pathExtract) {
throw new Error(`unable to download tfswitch from ${url}`);
}
return pathExtract;
}
async function createToml(pathToCLI) {
const homedir = os.homedir();
const toml = `bin = "${pathToCLI}/terraform"\n`;
fs.writeFileSync(path.join(homedir, ".tfswitch.toml"), toml);
}
async function run() {
try {
const token = core.getInput("github-token", { required: true });
const tag = core.getInput("tag") || "latest";
const platform = await getPlatform();
const arch = await getArch();
const release = await getRelease(token, tag);
const version = release.data.tag_name;
const asset = getAsset(release, platform, arch, tag);
core.info(`found version: ${version} for ${tag}/${platform}/${arch}`);
const pathToCLI = await download(asset.browser_download_url);
core.addPath(pathToCLI);
createToml(pathToCLI);
} catch (error) {
core.setFailed(error);
}
}
run();