2019-06-20 13:28:39 -04:00
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
import * as path from 'path';
|
2019-08-19 08:28:37 -04:00
|
|
|
import * as semver from 'semver';
|
2020-02-09 00:29:21 -05:00
|
|
|
import * as httpm from '@actions/http-client';
|
|
|
|
import * as sys from './system';
|
2020-02-09 18:09:15 -05:00
|
|
|
import {debug} from '@actions/core';
|
2019-11-20 15:24:28 -05:00
|
|
|
|
2020-02-09 00:29:21 -05:00
|
|
|
export async function downloadGo(
|
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean
|
|
|
|
): Promise<string | undefined> {
|
2020-02-09 00:21:39 -05:00
|
|
|
let toolPath: string | undefined;
|
2019-11-20 15:24:28 -05:00
|
|
|
|
2019-06-20 13:28:39 -04:00
|
|
|
try {
|
2020-02-09 00:21:39 -05:00
|
|
|
let match: IGoVersion | undefined = await findMatch(versionSpec, stable);
|
2019-06-20 13:28:39 -04:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
if (match) {
|
|
|
|
// download
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`match ${match.version}`);
|
2020-02-09 08:44:32 -05:00
|
|
|
let downloadUrl: string = `https://storage.googleapis.com/golang/${match.files[0].filename}`;
|
2020-02-09 22:39:44 -05:00
|
|
|
console.log(`Downloading from ${downloadUrl}`);
|
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
let downloadPath: string = await tc.downloadTool(downloadUrl);
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`downloaded to ${downloadPath}`);
|
2019-06-20 13:28:39 -04:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
// extract
|
2020-02-09 22:39:44 -05:00
|
|
|
console.log('Extracting ...');
|
2020-02-09 00:29:21 -05:00
|
|
|
let extPath: string =
|
|
|
|
sys.getPlatform() == 'windows'
|
|
|
|
? await tc.extractZip(downloadPath)
|
|
|
|
: await tc.extractTar(downloadPath);
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`extracted to ${extPath}`);
|
2019-06-20 13:28:39 -04:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
// extracts with a root folder that matches the fileName downloaded
|
|
|
|
const toolRoot = path.join(extPath, 'go');
|
2020-03-26 10:44:11 -04:00
|
|
|
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
|
2020-02-09 00:21:39 -05:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-02-09 18:09:15 -05:00
|
|
|
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
|
2019-06-20 13:28:39 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
return toolPath;
|
2019-06-20 13:28:39 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
export interface IGoVersionFile {
|
2020-02-09 00:29:21 -05:00
|
|
|
filename: string;
|
2020-02-09 00:21:39 -05:00
|
|
|
// darwin, linux, windows
|
2020-02-09 00:29:21 -05:00
|
|
|
os: string;
|
|
|
|
arch: string;
|
2019-06-20 13:28:39 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
export interface IGoVersion {
|
|
|
|
version: string;
|
|
|
|
stable: boolean;
|
|
|
|
files: IGoVersionFile[];
|
2019-06-20 13:28:39 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 00:29:21 -05:00
|
|
|
export async function findMatch(
|
|
|
|
versionSpec: string,
|
|
|
|
stable: boolean
|
|
|
|
): Promise<IGoVersion | undefined> {
|
2020-02-09 00:21:39 -05:00
|
|
|
let archFilter = sys.getArch();
|
|
|
|
let platFilter = sys.getPlatform();
|
2019-06-20 13:28:39 -04:00
|
|
|
|
2020-02-09 18:09:15 -05:00
|
|
|
let result: IGoVersion | undefined;
|
2020-02-09 00:29:21 -05:00
|
|
|
let match: IGoVersion | undefined;
|
2019-06-20 13:28:39 -04:00
|
|
|
|
2020-02-09 09:25:20 -05:00
|
|
|
const dlUrl: string = 'https://golang.org/dl/?mode=json&include=all';
|
|
|
|
let candidates: IGoVersion[] | null = await module.exports.getVersions(dlUrl);
|
2020-02-09 00:21:39 -05:00
|
|
|
if (!candidates) {
|
2020-02-09 18:48:40 -05:00
|
|
|
throw new Error(`golang download url did not return results`);
|
2019-08-19 08:28:37 -04:00
|
|
|
}
|
2020-02-09 00:29:21 -05:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
let goFile: IGoVersionFile | undefined;
|
2020-02-09 00:29:21 -05:00
|
|
|
for (let i = 0; i < candidates.length; i++) {
|
2020-02-09 00:21:39 -05:00
|
|
|
let candidate: IGoVersion = candidates[i];
|
2020-02-10 19:18:01 -05:00
|
|
|
let version = makeSemver(candidate.version);
|
2020-02-09 00:29:21 -05:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
// 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 08:28:37 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`check ${version} satisfies ${versionSpec}`);
|
2020-02-10 15:21:04 -05:00
|
|
|
if (
|
|
|
|
semver.satisfies(version, versionSpec) &&
|
|
|
|
(!stable || candidate.stable === stable)
|
|
|
|
) {
|
2020-02-09 00:21:39 -05:00
|
|
|
goFile = candidate.files.find(file => {
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
|
2020-02-09 00:21:39 -05:00
|
|
|
return file.arch === archFilter && file.os === platFilter;
|
|
|
|
});
|
2019-08-19 08:28:37 -04:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
if (goFile) {
|
2020-02-09 18:09:15 -05:00
|
|
|
debug(`matched ${candidate.version}`);
|
2020-02-09 00:21:39 -05:00
|
|
|
match = candidate;
|
|
|
|
break;
|
|
|
|
}
|
2019-08-19 08:28:37 -04:00
|
|
|
}
|
2020-02-09 00:29:21 -05:00
|
|
|
}
|
2019-08-19 08:28:37 -04:00
|
|
|
|
2020-02-09 00:21:39 -05:00
|
|
|
if (match && goFile) {
|
2020-02-09 18:09:15 -05:00
|
|
|
// clone since we're mutating the file list to be only the file that matches
|
|
|
|
result = <IGoVersion>Object.assign({}, match);
|
|
|
|
result.files = [goFile];
|
2019-08-19 08:28:37 -04:00
|
|
|
}
|
|
|
|
|
2020-02-09 18:09:15 -05:00
|
|
|
return result;
|
2019-08-19 08:28:37 -04:00
|
|
|
}
|
2020-02-09 09:25:20 -05:00
|
|
|
|
|
|
|
export async function getVersions(dlUrl: string): Promise<IGoVersion[] | null> {
|
|
|
|
// this returns versions descending so latest is first
|
2020-02-09 14:39:34 -05:00
|
|
|
let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
|
2020-02-09 18:48:40 -05:00
|
|
|
return (await http.getJson<IGoVersion[]>(dlUrl)).result;
|
2020-02-09 09:25:20 -05:00
|
|
|
}
|
2020-02-10 19:18:01 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// Convert the go version syntax into semver for semver matching
|
|
|
|
// 1.13.1 => 1.13.1
|
|
|
|
// 1.13 => 1.13.0
|
|
|
|
// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1
|
|
|
|
// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1
|
|
|
|
export function makeSemver(version: string): string {
|
|
|
|
version = version.replace('go', '');
|
|
|
|
version = version.replace('beta', '-beta').replace('rc', '-rc');
|
|
|
|
let parts = version.split('-');
|
|
|
|
|
|
|
|
let verPart: string = parts[0];
|
|
|
|
let prereleasePart = parts.length > 1 ? `-${parts[1]}` : '';
|
|
|
|
|
|
|
|
let verParts: string[] = verPart.split('.');
|
|
|
|
if (verParts.length == 2) {
|
|
|
|
verPart += '.0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${verPart}${prereleasePart}`;
|
|
|
|
}
|