Skip to content

Commit c92e38e

Browse files
Refactor to incapsulate download urls in a map
1 parent 79dce53 commit c92e38e

File tree

6 files changed

+82
-82
lines changed

6 files changed

+82
-82
lines changed

src/cli-installer/cli-installer.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,70 @@ import os from "os";
33
import * as core from "@actions/core";
44
import * as tc from "@actions/tool-cache";
55

6-
export const archMap: Record<string, string> = {
6+
import { type Installer } from "./index";
7+
import { LinuxInstaller } from "./linux";
8+
import { MacOsInstaller } from "./macos";
9+
import { WindowsInstaller } from "./windows";
10+
11+
export type SupportedPlatform = Extract<
12+
NodeJS.Platform,
13+
"linux" | "darwin" | "win32"
14+
>;
15+
16+
// maps OS architecture names to 1Password CLI installer architecture names
17+
const archMap: Record<string, string> = {
718
ia32: "386",
8-
x32: "386",
9-
x86: "386",
1019
x64: "amd64",
1120
arm: "arm",
1221
arm64: "arm64",
1322
};
1423

24+
export const cliUrlBuilder: Record<
25+
SupportedPlatform,
26+
(version: string, arch?: string) => string
27+
> = {
28+
linux: (version, arch) =>
29+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_linux_${arch}_${version}.zip`,
30+
darwin: (version) =>
31+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_apple_universal_${version}.pkg`,
32+
win32: (version, arch) =>
33+
`https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_windows_${arch}_${version}_.zip`,
34+
};
35+
1536
export class CliInstaller {
16-
public async install(downloadUrl: string): Promise<void> {
17-
console.info(`Downloading 1Password CLI from: ${downloadUrl}`);
18-
const downloadPath = await tc.downloadTool(downloadUrl);
37+
protected readonly version: string;
38+
protected readonly arch: string;
39+
40+
public constructor(version: string) {
41+
this.version = version;
42+
this.arch = this.getArch();
43+
}
44+
45+
public static create(version: string): Installer {
46+
const platform = os.platform();
47+
switch (platform) {
48+
case "linux":
49+
return new LinuxInstaller(version);
50+
case "darwin":
51+
return new MacOsInstaller(version);
52+
case "win32":
53+
return new WindowsInstaller(version);
54+
default:
55+
throw new Error(`Unsupported platform: ${platform}`);
56+
}
57+
}
58+
59+
public async install(url: string): Promise<void> {
60+
console.info(`Downloading 1Password CLI from: ${url}`);
61+
const downloadPath = await tc.downloadTool(url);
1962
console.info("Installing 1Password CLI");
2063
const extractedPath = await tc.extractZip(downloadPath);
2164
core.addPath(extractedPath);
2265
core.info("1Password CLI installed");
2366
}
2467

25-
// possible values for GitHub hosted runners (process.env.RUNNER_ARCH) can be found here: https://docs.github.com/en/actions/reference/variables-reference#default-environment-variables
26-
public getArch(): string {
27-
const arch = archMap[process.env.RUNNER_ARCH?.toLowerCase() || os.arch()];
68+
private getArch(): string {
69+
const arch = archMap[os.arch()];
2870
if (!arch) {
2971
throw new Error("Unsupported architecture");
3072
}

src/cli-installer/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
export { LinuxInstaller } from "./linux";
2-
export { MacOsInstaller } from "./macos";
3-
export { WindowsInstaller } from "./windows";
1+
export { CliInstaller } from "./cli-installer";
42

53
export interface Installer {
64
installCli(): Promise<void>;
75
}
8-
9-
/* eslint-disable @typescript-eslint/naming-convention */
10-
// RunnerOS defines the operating system of the runner executing the job.
11-
// Look `RUNNER_OS` for possible values (https://docs.github.com/en/actions/reference/variables-reference).
12-
export enum RunnerOS {
13-
Linux = "Linux",
14-
MacOS = "macOS",
15-
Windows = "Windows",
16-
}
17-
/* eslint-enable @typescript-eslint/naming-convention */

src/cli-installer/linux.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import { CliInstaller } from "./cli-installer";
2-
import { type Installer } from "./index";
1+
import {
2+
CliInstaller,
3+
cliUrlBuilder,
4+
type SupportedPlatform,
5+
} from "./cli-installer";
6+
import type { Installer } from "./index";
37

48
export class LinuxInstaller extends CliInstaller implements Installer {
5-
private readonly arch: string;
6-
private readonly version: string;
9+
private readonly platform: SupportedPlatform = "linux"; // Node.js platform identifier for Linux
710

811
public constructor(version: string) {
9-
super();
10-
this.version = version;
11-
this.arch = super.getArch();
12+
super(version);
1213
}
1314

1415
public async installCli(): Promise<void> {
15-
const downloadUrl = this.downloadUrl();
16-
await super.install(downloadUrl);
17-
}
18-
19-
private downloadUrl(): string {
20-
return `https://cache.agilebits.com/dist/1P/op2/pkg/${this.version}/op_linux_${this.arch}_${this.version}.zip`;
16+
const urlBuilder = cliUrlBuilder[this.platform];
17+
await super.install(urlBuilder(this.version, this.arch));
2118
}
2219
}

src/cli-installer/macos.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@ import { promisify } from "util";
66
import * as core from "@actions/core";
77
import * as tc from "@actions/tool-cache";
88

9-
import { CliInstaller } from "./cli-installer";
9+
import {
10+
CliInstaller,
11+
cliUrlBuilder,
12+
type SupportedPlatform,
13+
} from "./cli-installer";
1014
import { type Installer } from "./index";
1115

1216
const execAsync = promisify(exec);
1317

1418
export class MacOsInstaller extends CliInstaller implements Installer {
15-
private readonly version: string;
19+
private readonly platform: SupportedPlatform = "darwin"; // Node.js platform identifier for macOS
1620

1721
public constructor(version: string) {
18-
super();
19-
this.version = version;
22+
super(version);
2023
}
2124

2225
public async installCli(): Promise<void> {
23-
const downloadUrl = this.downloadUrl();
24-
core.info(`Downloading 1Password CLI ${this.version} from ${downloadUrl}`);
25-
await this.install(downloadUrl);
26-
}
27-
28-
private downloadUrl(): string {
29-
return `https://cache.agilebits.com/dist/1P/op2/pkg/${this.version}/op_apple_universal_${this.version}.pkg`;
26+
const urlBuilder = cliUrlBuilder[this.platform];
27+
await this.install(urlBuilder(this.version));
3028
}
3129

3230
// @actions/tool-cache package does not support .pkg files, so we need to handle the installation manually

src/cli-installer/windows.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import { CliInstaller } from "./cli-installer";
1+
import {
2+
CliInstaller,
3+
cliUrlBuilder,
4+
type SupportedPlatform,
5+
} from "./cli-installer";
26
import type { Installer } from "./index";
37

48
export class WindowsInstaller extends CliInstaller implements Installer {
5-
private readonly arch: string;
6-
private readonly version: string;
9+
private readonly platform: SupportedPlatform = "win32"; // Node.js platform identifier for Windows
710

811
public constructor(version: string) {
9-
super();
10-
this.version = version;
11-
this.arch = super.getArch();
12+
super(version);
1213
}
1314

1415
public async installCli(): Promise<void> {
15-
const downloadUrl = this.downloadUrl();
16-
await super.install(downloadUrl);
17-
}
18-
19-
private downloadUrl(): string {
20-
return `https://cache.agilebits.com/dist/1P/op2/pkg/${this.version}/op_windows_${this.arch}_${this.version}.zip`;
16+
const urlBuilder = cliUrlBuilder[this.platform];
17+
await super.install(urlBuilder(this.version, this.arch));
2118
}
2219
}

src/index.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import * as core from "@actions/core";
22

3-
import {
4-
type Installer,
5-
LinuxInstaller,
6-
MacOsInstaller,
7-
RunnerOS,
8-
WindowsInstaller,
9-
} from "./cli-installer";
3+
import { CliInstaller } from "./cli-installer";
104
import { VersionResolver } from "./version";
115

126
/**
@@ -16,23 +10,7 @@ const run = async (): Promise<void> => {
1610
try {
1711
const versionResolver = new VersionResolver(core.getInput("version"));
1812
await versionResolver.resolve();
19-
20-
let installer: Installer;
21-
switch (process.env.RUNNER_OS) {
22-
case RunnerOS.Linux:
23-
installer = new LinuxInstaller(versionResolver.get());
24-
break;
25-
case RunnerOS.MacOS:
26-
installer = new MacOsInstaller(versionResolver.get());
27-
break;
28-
case RunnerOS.Windows:
29-
installer = new WindowsInstaller(versionResolver.get());
30-
break;
31-
default:
32-
core.setFailed(`Unsupported platform: ${process.env.RUNNER_OS}`);
33-
return;
34-
}
35-
13+
const installer = CliInstaller.create(versionResolver.get());
3614
await installer.installCli();
3715
} catch (error: unknown) {
3816
if (error instanceof Error) {

0 commit comments

Comments
 (0)