Skip to content

Commit

Permalink
feat(port): terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohe-Am committed Dec 29, 2023
1 parent 42b9179 commit 2836d18
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 18 deletions.
1 change: 1 addition & 0 deletions ports/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export { default as pnpm } from "../ports/pnpm.ts";
export { default as protoc } from "../ports/protoc.ts";
export { default as ruff } from "../ports/ruff.ts";
export { default as tar } from "../ports/tar.ts";
export { default as terraform } from "../ports/terraform.ts";
export { default as unzip } from "../ports/unzip.ts";
export { default as wasm_opt } from "../ports/wasm-opt.ts";
export { default as wasm_tools } from "../ports/wasm-tools.ts";
Expand Down
94 changes: 94 additions & 0 deletions ports/terraform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import {
$,
ALL_OS,
downloadFile,
dwnUrlOut,
osXarch,
PortBase,
std_fs,
std_path,
unarchive,
} from "../port.ts";
import type {
DownloadArgs,
InstallArgs,
InstallConfigSimple,
} from "../port.ts";

// TODO: sanity check exports of all ports
export const manifest = {
ty: "denoWorker@v1" as const,
name: "terraform_hashicorp",
version: "0.1.0",
moduleSpecifier: import.meta.url,
deps: [],
platforms: osXarch([...ALL_OS], ["aarch64", "x86_64"]),
};

export default function conf(config: InstallConfigSimple = {}) {
return {
...config,
port: manifest,
};
}

export class Port extends PortBase {
async listAll() {
const rawHtml = await $.request(`https://releases.hashicorp.com/terraform/`)
.text();
const versions = [...rawHtml.matchAll(/terraform_([^<\/]*)</g)].map((
[_, capture],
) => capture);

return versions.reverse();
}
async latestStable() {
const all = await this.listAll();
// stable versions don't have any additional info in theform of 1.2.3-alpha
return all.findLast((str) => !str.match(/-/))!;
}

// node distribute archives that contain the binary, ecma source for npm/npmx/corepack, include source files and more
downloadUrls(args: DownloadArgs) {
const { installVersion, platform } = args;
let arch;
switch (platform.arch) {
case "x86_64":
arch = "amd64";
break;
case "aarch64":
arch = "arm";
break;
default:
throw new Error(`unsupported: ${platform}`);
}
const os = platform.os;

return [
`https://releases.hashicorp.com/terraform/${installVersion}/terraform_${installVersion}_${os}_${arch}.zip`,
].map(dwnUrlOut);
}

async download(args: DownloadArgs) {
const urls = this.downloadUrls(args);
await Promise.all(
urls.map((obj) => downloadFile({ ...args, ...obj })),
);
}

async install(args: InstallArgs) {
const [{ name: fileName }] = this.downloadUrls(args);
const fileDwnPath = std_path.resolve(args.downloadPath, fileName);

await unarchive(fileDwnPath, args.tmpDirPath);

const installPath = $.path(args.installPath);
if (await installPath.exists()) {
await installPath.remove({ recursive: true });
}
await std_fs.move(
args.tmpDirPath,
(await installPath.ensureDir()).join("bin").toString(),
);
}
}
45 changes: 27 additions & 18 deletions tests/ports.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import "../setup_logger.ts";
import { secureConfig, stdDeps } from "../mod.ts";
import { dockerE2eTest, E2eTestCase, localE2eTest } from "./utils.ts";
import node from "../ports/node.ts";
import pnpm from "../ports/pnpm.ts";
import cargo_binstall from "../ports/cargo-binstall.ts";
import wasmedge from "../ports/wasmedge.ts";
import wasm_tools from "../ports/wasm-tools.ts";
import wasm_opt from "../ports/wasm-opt.ts";
import cargo_insta from "../ports/cargo-insta.ts";
import jco from "../ports/jco.ts";
import mold from "../ports/mold.ts";
import act from "../ports/act.ts";
import asdf from "../ports/asdf.ts";
import protoc from "../ports/protoc.ts";
import earthly from "../ports/earthly.ts";
import ruff from "../ports/ruff.ts";
import whiz from "../ports/whiz.ts";
import cpython from "../ports/cpy_bs.ts";
import pipi from "../ports/pipi.ts";
import {
act,
asdf,
cargo_binstall,
cargo_insta,
cpy_bs,
earthly,
jco,
mold,
node,
pipi,
pnpm,
protoc,
ruff,
terraform,
wasm_opt,
wasm_tools,
wasmedge,
whiz,
} from "../ports/mod.ts";

type CustomE2eTestCase = Omit<E2eTestCase, "ePoints"> & {
ePoint: string;
Expand Down Expand Up @@ -80,6 +83,12 @@ const cases: CustomE2eTestCase[] = [
installConf: wasm_tools(),
ePoint: `wasm-tools -V`,
},
// 24 megs
{
name: "terraform",
installConf: terraform(),
ePoint: `terraform --version`,
},
// 25 megs
{
name: "node",
Expand Down Expand Up @@ -125,7 +134,7 @@ const cases: CustomE2eTestCase[] = [
// 80 meg
{
name: "cpy_bs",
installConf: cpython(),
installConf: cpy_bs(),
ePoint: `python3 --version`,
},
// 80 meg +
Expand Down
1 change: 1 addition & 0 deletions utils/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export async function downloadFile(
await $.path(downloadPath).ensureDir();

await tmpFilePath.copyFile(fileDwnPath);
return downloadPath.toString();
}

/* *
Expand Down

0 comments on commit 2836d18

Please sign in to comment.