Switch to @hashicorp/js-releases for finding correct package to install (#70)

This commit is contained in:
Audrey Eschright 2020-12-01 10:49:15 -08:00 committed by GitHub
parent 4c5048fbaf
commit 32c4f59108
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 48794 additions and 4682 deletions

View file

@ -7,60 +7,7 @@ const path = require('path');
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const io = require('@actions/io');
const fetch = require('node-fetch');
const semver = require('semver');
// Find latest version given list of all available
function findLatest (allVersions) {
core.debug('Parsing version list for latest version');
let latest = '0.0.0';
for (const version in allVersions.versions) {
// Ignore pre-release
if (semver.prerelease(version) !== null) {
continue;
}
// is "version" greater than "latest"
latest = semver.gt(version, latest) ? version : latest;
}
core.info(`Latest version is ${latest}`);
return allVersions.versions[latest];
}
// Find specific version given list of all available
function findSpecific (allVersions, version) {
core.debug(`Parsing version list for version ${version}`);
return allVersions.versions[version];
}
// Find specific version given list of all available
function findLatestMatchingSpecification (allVersions, version) {
core.debug(`Parsing version list for latest matching specification ${version}`);
const versionList = [];
for (const _version in allVersions.versions) {
versionList.push(_version);
}
const bestMatchVersion = semver.maxSatisfying(versionList, version);
if (!bestMatchVersion) {
throw new Error(`Could not find Terraform version matching ${version} in version list`);
}
core.info(`Latest version satisfying ${version} is ${bestMatchVersion}`);
return allVersions.versions[bestMatchVersion];
}
async function downloadMetadata () {
core.debug('Downloading version metadata');
return fetch('https://releases.hashicorp.com/terraform/index.json')
.then(res => res.json())
.catch(err => {
core.setFailed(`Failed to fetch version metadata. ${err}`);
});
}
const releases = require('@hashicorp/js-releases');
// arch in [arm, x32, x64...] (https://nodejs.org/api/os.html#os_os_arch)
// return value in [amd64, 386, arm]
@ -81,23 +28,6 @@ function mapOS (os) {
return mappings[os] || os;
}
// Get build for an operating system and architecture
function getBuild (versionObj, os, arch) {
core.debug(`Getting build for Terraform version ${versionObj.version}, os ${os}, and arch ${arch}`);
const buildObj = versionObj.builds.length &&
versionObj.builds.find(build =>
build.arch === mapArch(arch) &&
build.os === mapOS(os)
);
if (!buildObj) {
throw new Error(`Terraform version ${versionObj.version} not available for ${os} and ${arch}`);
}
return buildObj;
}
async function downloadCLI (url) {
core.debug(`Downloading Terraform CLI from ${url}`);
const pathToCLIZip = await tc.downloadTool(url);
@ -183,40 +113,35 @@ async function run () {
const wrapper = core.getInput('terraform_wrapper') === 'true';
// Gather OS details
const osPlat = os.platform();
const osPlatform = os.platform();
const osArch = os.arch();
// Download metadata about all versions of Terraform CLI
const versionMetadata = await downloadMetadata();
const specificMatch = findSpecific(versionMetadata, version);
// Find latest or a specific version like 0.1.0
const versionObj = version.toLowerCase() === 'latest'
? findLatest(versionMetadata) : specificMatch || findLatestMatchingSpecification(versionMetadata, version);
if (versionObj) {
// Get the build available for this runner's OS and a 64 bit architecture
const buildObj = getBuild(versionObj, osPlat, osArch);
// Download requested version
const pathToCLI = await downloadCLI(buildObj.url);
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Add to path
core.addPath(pathToCLI);
// Add credentials to file if they are provided
if (credentialsHostname && credentialsToken) {
await addCredentials(credentialsHostname, credentialsToken, osPlat);
}
return versionObj;
} else {
core.setFailed(`Could not find Terraform version ${version} in version list`);
core.debug(`Finding releases for Terraform version ${version}`);
const release = await releases.getRelease('terraform', version);
const platform = mapOS(osPlatform);
const arch = mapArch(osArch);
core.debug(`Getting build for Terraform version ${release.version}: ${platform} ${arch}`);
const build = release.getBuild(platform, arch);
if (!build) {
throw new Error(`Terraform version ${version} not available for ${platform} and ${arch}`);
}
// Download requested version
const pathToCLI = await downloadCLI(build.url);
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Add to path
core.addPath(pathToCLI);
// Add credentials to file if they are provided
if (credentialsHostname && credentialsToken) {
await addCredentials(credentialsHostname, credentialsToken, osPlatform);
}
return release;
} catch (error) {
core.error(error);
throw error;