Skip to content

Commit 38bde4f

Browse files
Make dist-lib to contain code to share in load-secrets-cli repo
1 parent e60d1d6 commit 38bde4f

28 files changed

+310
-10
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export type SupportedPlatform = Extract<
2+
NodeJS.Platform,
3+
"linux" | "darwin" | "win32"
4+
>;
5+
export declare const archMap: Record<string, string>;
6+
export declare const cliUrlBuilder: Record<
7+
SupportedPlatform,
8+
(version: string, arch?: string) => string
9+
>;
10+
export declare class CliInstaller {
11+
readonly version: string;
12+
readonly arch: string;
13+
constructor(version: string);
14+
install(url: string): Promise<void>;
15+
private getArch;
16+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os from "os";
2+
import * as core from "@actions/core";
3+
import * as tc from "@actions/tool-cache";
4+
// maps OS architecture names to 1Password CLI installer architecture names
5+
export const archMap = {
6+
ia32: "386",
7+
x64: "amd64",
8+
arm: "arm",
9+
arm64: "arm64",
10+
};
11+
// Builds the download URL for the 1Password CLI based on the platform and version.
12+
export const cliUrlBuilder = {
13+
linux: (version, arch) =>
14+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_linux_${arch}_${version}.zip`,
15+
darwin: (version) =>
16+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_apple_universal_${version}.pkg`,
17+
win32: (version, arch) =>
18+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_windows_${arch}_${version}.zip`,
19+
};
20+
export class CliInstaller {
21+
version;
22+
arch;
23+
constructor(version) {
24+
this.version = version;
25+
this.arch = this.getArch();
26+
}
27+
async install(url) {
28+
console.info(`Downloading 1Password CLI from: ${url}`);
29+
const downloadPath = await tc.downloadTool(url);
30+
console.info("Installing 1Password CLI");
31+
const extractedPath = await tc.extractZip(downloadPath);
32+
core.addPath(extractedPath);
33+
core.info("1Password CLI installed");
34+
}
35+
getArch() {
36+
const arch = archMap[os.arch()];
37+
if (!arch) {
38+
throw new Error("Unsupported architecture");
39+
}
40+
return arch;
41+
}
42+
}

dist-lib/cli-installer/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { type Installer, newCliInstaller } from "./installer";

dist-lib/cli-installer/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { newCliInstaller } from "./installer";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface Installer {
2+
installCli(): Promise<void>;
3+
}
4+
export declare const newCliInstaller: (version: string) => Installer;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os from "os";
2+
import { LinuxInstaller } from "./linux";
3+
import { MacOsInstaller } from "./macos";
4+
import { WindowsInstaller } from "./windows";
5+
export const newCliInstaller = (version) => {
6+
const platform = os.platform();
7+
switch (platform) {
8+
case "linux":
9+
return new LinuxInstaller(version);
10+
case "darwin":
11+
return new MacOsInstaller(version);
12+
case "win32":
13+
return new WindowsInstaller(version);
14+
default:
15+
throw new Error(`Unsupported platform: ${platform}`);
16+
}
17+
};

dist-lib/cli-installer/linux.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { CliInstaller } from "./cli-installer";
2+
import type { Installer } from "./installer";
3+
export declare class LinuxInstaller extends CliInstaller implements Installer {
4+
private readonly platform;
5+
constructor(version: string);
6+
installCli(): Promise<void>;
7+
}

dist-lib/cli-installer/linux.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { CliInstaller, cliUrlBuilder } from "./cli-installer";
2+
export class LinuxInstaller extends CliInstaller {
3+
platform = "linux"; // Node.js platform identifier for Linux
4+
constructor(version) {
5+
super(version);
6+
}
7+
async installCli() {
8+
const urlBuilder = cliUrlBuilder[this.platform];
9+
await super.install(urlBuilder(this.version, this.arch));
10+
}
11+
}

dist-lib/cli-installer/macos.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { CliInstaller } from "./cli-installer";
2+
import { type Installer } from "./installer";
3+
export declare class MacOsInstaller extends CliInstaller implements Installer {
4+
private readonly platform;
5+
constructor(version: string);
6+
installCli(): Promise<void>;
7+
install(downloadUrl: string): Promise<void>;
8+
}

dist-lib/cli-installer/macos.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { exec } from "child_process";
2+
import * as fs from "fs";
3+
import * as path from "path";
4+
import { promisify } from "util";
5+
import * as core from "@actions/core";
6+
import * as tc from "@actions/tool-cache";
7+
import { CliInstaller, cliUrlBuilder } from "./cli-installer";
8+
const execAsync = promisify(exec);
9+
export class MacOsInstaller extends CliInstaller {
10+
platform = "darwin"; // Node.js platform identifier for macOS
11+
constructor(version) {
12+
super(version);
13+
}
14+
async installCli() {
15+
const urlBuilder = cliUrlBuilder[this.platform];
16+
await this.install(urlBuilder(this.version));
17+
}
18+
// @actions/tool-cache package does not support .pkg files, so we need to handle the installation manually
19+
async install(downloadUrl) {
20+
console.info(`Downloading 1Password CLI from: ${downloadUrl}`);
21+
const pkgPath = await tc.downloadTool(downloadUrl);
22+
const pkgWithExtension = `${pkgPath}.pkg`;
23+
fs.renameSync(pkgPath, pkgWithExtension);
24+
const expandDir = "temp-pkg";
25+
await execAsync(`pkgutil --expand "${pkgWithExtension}" "${expandDir}"`);
26+
const payloadPath = path.join(expandDir, "op.pkg", "Payload");
27+
console.info("Installing 1Password CLI");
28+
const cliPath = await tc.extractTar(payloadPath);
29+
core.addPath(cliPath);
30+
fs.rmSync(expandDir, { recursive: true, force: true });
31+
fs.rmSync(pkgPath, { force: true });
32+
core.info("1Password CLI installed");
33+
}
34+
}

0 commit comments

Comments
 (0)