Skip to content

Commit

Permalink
wip: terraform/releases.js
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Dec 15, 2024
1 parent f7027ae commit 2346dad
Showing 1 changed file with 31 additions and 48 deletions.
79 changes: 31 additions & 48 deletions terraform/releases.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
'use strict';

async function getDistributables() {
try {
// Fetch the Terraform releases JSON
const response = await fetch(
'https://releases.hashicorp.com/terraform/index.json',
{
method: 'GET',
headers: { Accept: 'application/json' },
},
let response = await fetch(
'https://releases.hashicorp.com/terraform/index.json',
{ headers: { Accept: 'application/json' } },
);
if (!response.ok) {
throw new Error(
`Failed to fetch releases: HTTP ${response.status} - ${response.statusText}`,
);
}

// Validate the HTTP response
if (!response.ok) {
throw new Error(
`Failed to fetch releases: HTTP ${response.status} - ${response.statusText}`,
);
}

// Parse the JSON response
const releases = await response.json();

let all = {
releases: [],
download: '', // Full URI provided in response body
};

function getBuildsForVersion(version) {
releases.versions[version].builds.forEach(function (build) {
let r = {
version: build.version,
download: build.url,
// These are generic enough for the autodetect,
// and the per-file logic has proven to get outdated sooner
//os: convert[build.os],
//arch: convert[build.arch],
//channel: 'stable|-rc|-beta|-alpha',
};
all.releases.push(r);
});
let releases = await response.json();
let all = {
releases: [],
download: '',
};

let allVersions = Object.keys(releases.versions);
allVersions.reverse(); // Releases are listed chronologically, we want the latest first.

for (let version of allVersions) {
for (let build of releases.versions[version].builds) {
let r = {
version: build.version,
download: build.url,
// These are generic enough for the autodetect,
// and the per-file logic has proven to get outdated sooner
//os: convert[build.os],
//arch: convert[build.arch],
//channel: 'stable|-rc|-beta|-alpha',
};
all.releases.push(r);
}

// Releases are listed chronologically, we want the latest first.
const allVersions = Object.keys(releases.versions).reverse();

allVersions.forEach(function (version) {
getBuildsForVersion(version);
});

return all;
} catch (err) {
console.error('Error fetching Terraform releases:', err.message);
return { releases: [], download: '' };
}

return all;
}

module.exports = getDistributables;
Expand Down

0 comments on commit 2346dad

Please sign in to comment.