Allow to specify range specification instead of fixed version (#38)

That allows to install for example the latest bug-fix version of
terraform 1.12.* even if 1.13 is already installed.

Co-authored-by: Matthew Sanabria <24284972+sudomateo@users.noreply.github.com>
This commit is contained in:
Jarek Potiuk 2020-09-08 16:18:09 +02:00 committed by GitHub
parent e255dfd077
commit af8505ef0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 348 additions and 51 deletions

56
dist/index.js vendored
View file

@ -2068,14 +2068,23 @@ function findLatest (allVersions) {
// Find specific version given list of all available
function findSpecific (allVersions, version) {
core.debug(`Parsing version list for version ${version}`);
return allVersions.versions[version];
}
const versionObj = allVersions.versions[version];
if (!versionObj) {
throw new Error(`Could not find Terraform version ${version} in version list`);
// 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 versionObj;
return allVersions.versions[bestMatchVersion];
}
async function downloadMetadata () {
@ -2215,30 +2224,37 @@ async function run () {
// 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) : findSpecific(versionMetadata, version);
const versionObj = version.toLowerCase() === 'latest'
? findLatest(versionMetadata) : specificMatch || findLatestMatchingSpecification(versionMetadata, version);
// Get the build available for this runner's OS and a 64 bit architecture
const buildObj = getBuild(versionObj, osPlat, osArch);
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);
// Download requested version
const pathToCLI = await downloadCLI(buildObj.url);
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Install our wrapper
if (wrapper) {
await installWrapper(pathToCLI);
}
// Add to path
core.addPath(pathToCLI);
// Add to path
core.addPath(pathToCLI);
// Add credentials to file if they are provided
if (credentialsHostname && credentialsToken) {
await addCredentials(credentialsHostname, credentialsToken, osPlat);
// 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`);
}
} catch (error) {
core.error(error);
throw new Error(error);
throw error;
}
}