2019-06-21 01:28:39 +08:00
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
import * as path from 'path';
|
2019-08-19 20:28:37 +08:00
|
|
|
import * as semver from 'semver';
|
2020-02-09 13:21:39 +08:00
|
|
|
import * as httpm from '@actions/http-client'
|
|
|
|
import * as sys from './system'
|
2019-11-21 04:24:28 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
export async function downloadGo(versionSpec: string, stable: boolean): Promise<string | undefined> {
|
|
|
|
let toolPath: string | undefined;
|
2019-11-21 04:24:28 +08:00
|
|
|
|
2019-06-21 01:28:39 +08:00
|
|
|
try {
|
2020-02-09 13:21:39 +08:00
|
|
|
let match: IGoVersion | undefined = await findMatch(versionSpec, stable);
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
if (match) {
|
|
|
|
// download
|
|
|
|
let downloadUrl: string = `https://storage.googleapis.com/golang/${match.files[0]}`;
|
|
|
|
let downloadPath: string = await tc.downloadTool(downloadUrl);
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
// extract
|
|
|
|
let extPath: string = sys.getPlatform() == 'windows'?
|
|
|
|
await tc.extractZip(downloadPath): await tc.extractTar(downloadPath);
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
// extracts with a root folder that matches the fileName downloaded
|
|
|
|
const toolRoot = path.join(extPath, 'go');
|
|
|
|
toolPath = await tc.cacheDir(toolRoot, 'go', versionSpec);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throw `Failed to download version ${versionSpec}: ${error}`;
|
2019-06-21 01:28:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
return toolPath;
|
2019-06-21 01:28:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
export interface IGoVersionFile {
|
|
|
|
filename: string,
|
|
|
|
// darwin, linux, windows
|
|
|
|
os: string,
|
|
|
|
arch: string
|
2019-06-21 01:28:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
export interface IGoVersion {
|
|
|
|
version: string;
|
|
|
|
stable: boolean;
|
|
|
|
files: IGoVersionFile[];
|
2019-06-21 01:28:39 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
export async function findMatch(versionSpec: string, stable: boolean): Promise<IGoVersion | undefined> {
|
|
|
|
let archFilter = sys.getArch();
|
|
|
|
let platFilter = sys.getPlatform();
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
let match: IGoVersion| undefined;
|
|
|
|
const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
// this returns versions descending so latest is first
|
|
|
|
let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
|
|
|
|
let candidates: IGoVersion[] | null = (await http.getJson<IGoVersion[]>(dlUrl)).result;
|
2019-06-21 01:28:39 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
if (!candidates) {
|
|
|
|
throw new Error(`golang download url did not return results: ${dlUrl}`);
|
2019-08-19 20:28:37 +08:00
|
|
|
}
|
2020-02-09 13:21:39 +08:00
|
|
|
|
|
|
|
let goFile: IGoVersionFile | undefined;
|
|
|
|
for (let i=0; i < candidates.length; i++) {
|
|
|
|
let candidate: IGoVersion = candidates[i];
|
|
|
|
let version = candidate.version.replace('go', '');
|
|
|
|
|
|
|
|
// 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0
|
|
|
|
// since a semver of 1.13 would match latest 1.13
|
|
|
|
let parts: string[] = version.split('.');
|
|
|
|
if (parts.length == 2) {
|
|
|
|
version = version + '.0';
|
2019-08-19 20:28:37 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
//console.log(version, versionSpec);
|
|
|
|
if (semver.satisfies(version, versionSpec) && candidate.stable == stable) {
|
|
|
|
goFile = candidate.files.find(file => {
|
|
|
|
return file.arch === archFilter && file.os === platFilter;
|
|
|
|
});
|
2019-08-19 20:28:37 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
if (goFile) {
|
|
|
|
match = candidate;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-19 20:28:37 +08:00
|
|
|
}
|
2020-02-09 13:21:39 +08:00
|
|
|
};
|
2019-08-19 20:28:37 +08:00
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
if (match && goFile) {
|
|
|
|
match.files = [ goFile ];
|
2019-08-19 20:28:37 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 13:21:39 +08:00
|
|
|
return match;
|
2019-08-19 20:28:37 +08:00
|
|
|
}
|