-
Notifications
You must be signed in to change notification settings - Fork 39
Use op-cli-installer as local package #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/op-cli-installer/github-action/cli-installer/cli-installer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import os from "os"; | ||
|
|
||
| import * as core from "@actions/core"; | ||
| import * as tc from "@actions/tool-cache"; | ||
|
|
||
| export type SupportedPlatform = Extract< | ||
| NodeJS.Platform, | ||
| "linux" | "darwin" | "win32" | ||
| >; | ||
|
|
||
| // maps OS architecture names to 1Password CLI installer architecture names | ||
| export const archMap: Record<string, string> = { | ||
| ia32: "386", | ||
| x64: "amd64", | ||
| arm: "arm", | ||
| arm64: "arm64", | ||
| }; | ||
|
|
||
| // Builds the download URL for the 1Password CLI based on the platform and version. | ||
| export const cliUrlBuilder: Record< | ||
| SupportedPlatform, | ||
| (version: string, arch?: string) => string | ||
| > = { | ||
| linux: (version, arch) => | ||
| `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_linux_${arch}_${version}.zip`, | ||
| darwin: (version) => | ||
| `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_apple_universal_${version}.pkg`, | ||
| win32: (version, arch) => | ||
| `https://cache.agilebits.com/dist/1P/op2/pkg/${version}/op_windows_${arch}_${version}.zip`, | ||
| }; | ||
|
|
||
| export class CliInstaller { | ||
| public readonly version: string; | ||
| public readonly arch: string; | ||
|
|
||
| public constructor(version: string) { | ||
| this.version = version; | ||
| this.arch = this.getArch(); | ||
| } | ||
|
|
||
| public async install(url: string): Promise<void> { | ||
| console.info(`Downloading 1Password CLI from: ${url}`); | ||
| const downloadPath = await tc.downloadTool(url); | ||
| console.info("Installing 1Password CLI"); | ||
| const extractedPath = await tc.extractZip(downloadPath); | ||
| core.addPath(extractedPath); | ||
| core.info("1Password CLI installed"); | ||
| } | ||
|
|
||
| private getArch(): string { | ||
| const arch = archMap[os.arch()]; | ||
| if (!arch) { | ||
| throw new Error("Unsupported architecture"); | ||
| } | ||
|
|
||
| return arch; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { type Installer, newCliInstaller } from "./installer"; |
43 changes: 43 additions & 0 deletions
43
src/op-cli-installer/github-action/cli-installer/installer.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import os from "os"; | ||
|
|
||
| import { newCliInstaller } from "./installer"; | ||
| import { LinuxInstaller } from "./linux"; | ||
| import { MacOsInstaller } from "./macos"; | ||
| import { WindowsInstaller } from "./windows"; | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("newCliInstaller", () => { | ||
| const version = "1.0.0"; | ||
|
|
||
| afterEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("should return LinuxInstaller for linux platform", () => { | ||
| jest.spyOn(os, "platform").mockReturnValue("linux"); | ||
| const installer = newCliInstaller(version); | ||
| expect(installer).toBeInstanceOf(LinuxInstaller); | ||
| }); | ||
|
|
||
| it("should return MacOsInstaller for darwin platform", () => { | ||
| jest.spyOn(os, "platform").mockReturnValue("darwin"); | ||
| const installer = newCliInstaller(version); | ||
| expect(installer).toBeInstanceOf(MacOsInstaller); | ||
| }); | ||
|
|
||
| it("should return WindowsInstaller for win32 platform", () => { | ||
| jest.spyOn(os, "platform").mockReturnValue("win32"); | ||
| const installer = newCliInstaller(version); | ||
| expect(installer).toBeInstanceOf(WindowsInstaller); | ||
| }); | ||
|
|
||
| it("should throw error for unsupported platform", () => { | ||
| jest.spyOn(os, "platform").mockReturnValue("sunos"); | ||
| expect(() => newCliInstaller(version)).toThrow( | ||
| "Unsupported platform: sunos", | ||
| ); | ||
| }); | ||
| }); |
23 changes: 23 additions & 0 deletions
23
src/op-cli-installer/github-action/cli-installer/installer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import os from "os"; | ||
|
|
||
| import { LinuxInstaller } from "./linux"; | ||
| import { MacOsInstaller } from "./macos"; | ||
| import { WindowsInstaller } from "./windows"; | ||
|
|
||
| export interface Installer { | ||
| installCli(): Promise<void>; | ||
| } | ||
|
|
||
| export const newCliInstaller = (version: string): Installer => { | ||
| const platform = os.platform(); | ||
| switch (platform) { | ||
| case "linux": | ||
| return new LinuxInstaller(version); | ||
| case "darwin": | ||
| return new MacOsInstaller(version); | ||
| case "win32": | ||
| return new WindowsInstaller(version); | ||
| default: | ||
| throw new Error(`Unsupported platform: ${platform}`); | ||
| } | ||
| }; |
38 changes: 38 additions & 0 deletions
38
src/op-cli-installer/github-action/cli-installer/linux.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import os from "os"; | ||
|
|
||
| import { | ||
| archMap, | ||
| CliInstaller, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import { LinuxInstaller } from "./linux"; | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("LinuxInstaller", () => { | ||
| const version = "1.2.3"; | ||
| const arch: NodeJS.Architecture = "arm64"; | ||
|
|
||
| it("should construct with given version and architecture", () => { | ||
| jest.spyOn(os, "arch").mockReturnValue(arch); | ||
| const installer = new LinuxInstaller(version); | ||
| expect(installer.version).toEqual(version); | ||
| expect(installer.arch).toEqual(archMap[arch]); | ||
| }); | ||
|
|
||
| it("should call install with correct URL", async () => { | ||
| const installer = new LinuxInstaller(version); | ||
| const installMock = jest | ||
| .spyOn(CliInstaller.prototype, "install") | ||
| .mockResolvedValue(); | ||
|
|
||
| await installer.installCli(); | ||
|
|
||
| const builder = cliUrlBuilder["linux" as SupportedPlatform]; | ||
| const url = builder(version, installer.arch); | ||
| expect(installMock).toHaveBeenCalledWith(url); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { | ||
| CliInstaller, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import type { Installer } from "./installer"; | ||
|
|
||
| export class LinuxInstaller extends CliInstaller implements Installer { | ||
| private readonly platform: SupportedPlatform = "linux"; // Node.js platform identifier for Linux | ||
|
|
||
| public constructor(version: string) { | ||
| super(version); | ||
| } | ||
|
|
||
| public async installCli(): Promise<void> { | ||
| const urlBuilder = cliUrlBuilder[this.platform]; | ||
| await super.install(urlBuilder(this.version, this.arch)); | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/op-cli-installer/github-action/cli-installer/macos.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import os from "os"; | ||
|
|
||
| import { | ||
| archMap, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import { MacOsInstaller } from "./macos"; | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("MacOsInstaller", () => { | ||
| const version = "1.2.3"; | ||
| const arch: NodeJS.Architecture = "x64"; | ||
|
|
||
| it("should construct with given version and architecture", () => { | ||
| jest.spyOn(os, "arch").mockReturnValue(arch); | ||
| const installer = new MacOsInstaller(version); | ||
| expect(installer.version).toEqual(version); | ||
| expect(installer.arch).toEqual(archMap[arch]); | ||
| }); | ||
|
|
||
| it("should call install with correct URL", async () => { | ||
| const installer = new MacOsInstaller(version); | ||
| const installMock = jest.spyOn(installer, "install").mockResolvedValue(); | ||
|
|
||
| await installer.installCli(); | ||
|
|
||
| const builder = cliUrlBuilder["darwin" as SupportedPlatform]; | ||
| const url = builder(version, installer.arch); | ||
| expect(installMock).toHaveBeenCalledWith(url); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { execFile } from "child_process"; | ||
| import * as fs from "fs"; | ||
| import * as path from "path"; | ||
| import { promisify } from "util"; | ||
|
|
||
| import * as core from "@actions/core"; | ||
| import * as tc from "@actions/tool-cache"; | ||
|
|
||
| import { | ||
| CliInstaller, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import { type Installer } from "./installer"; | ||
|
|
||
| const execFileAsync = promisify(execFile); | ||
|
|
||
| export class MacOsInstaller extends CliInstaller implements Installer { | ||
| private readonly platform: SupportedPlatform = "darwin"; // Node.js platform identifier for macOS | ||
|
|
||
| public constructor(version: string) { | ||
| super(version); | ||
| } | ||
|
|
||
| public async installCli(): Promise<void> { | ||
| const urlBuilder = cliUrlBuilder[this.platform]; | ||
| await this.install(urlBuilder(this.version)); | ||
| } | ||
|
|
||
| // @actions/tool-cache package does not support .pkg files, so we need to handle the installation manually | ||
| public override async install(downloadUrl: string): Promise<void> { | ||
| console.info(`Downloading 1Password CLI from: ${downloadUrl}`); | ||
| const pkgPath = await tc.downloadTool(downloadUrl); | ||
| const pkgWithExtension = `${pkgPath}.pkg`; | ||
| fs.renameSync(pkgPath, pkgWithExtension); | ||
|
|
||
| const expandDir = "temp-pkg"; | ||
| await execFileAsync("pkgutil", ["--expand", pkgWithExtension, expandDir]); | ||
| const payloadPath = path.join(expandDir, "op.pkg", "Payload"); | ||
| console.info("Installing 1Password CLI"); | ||
| const cliPath = await tc.extractTar(payloadPath); | ||
| core.addPath(cliPath); | ||
|
|
||
| fs.rmSync(expandDir, { recursive: true, force: true }); | ||
| fs.rmSync(pkgPath, { force: true }); | ||
|
|
||
| core.info("1Password CLI installed"); | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
src/op-cli-installer/github-action/cli-installer/windows.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import os from "os"; | ||
|
|
||
| import { | ||
| archMap, | ||
| CliInstaller, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import { WindowsInstaller } from "./windows"; | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe("WindowsInstaller", () => { | ||
| const version = "1.2.3"; | ||
| const arch: NodeJS.Architecture = "x64"; | ||
|
|
||
| it("should construct with given version and architecture", () => { | ||
| jest.spyOn(os, "arch").mockReturnValue(arch); | ||
| const installer = new WindowsInstaller(version); | ||
| expect(installer.version).toEqual(version); | ||
| expect(installer.arch).toEqual(archMap[arch]); | ||
| }); | ||
|
|
||
| it("should call install with correct URL", async () => { | ||
| const installer = new WindowsInstaller(version); | ||
| const installMock = jest | ||
| .spyOn(CliInstaller.prototype, "install") | ||
| .mockResolvedValue(); | ||
|
|
||
| await installer.installCli(); | ||
|
|
||
| const builder = cliUrlBuilder["win32" as SupportedPlatform]; | ||
| const url = builder(version, installer.arch); | ||
| expect(installMock).toHaveBeenCalledWith(url); | ||
| }); | ||
| }); |
19 changes: 19 additions & 0 deletions
19
src/op-cli-installer/github-action/cli-installer/windows.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { | ||
| CliInstaller, | ||
| cliUrlBuilder, | ||
| type SupportedPlatform, | ||
| } from "./cli-installer"; | ||
| import type { Installer } from "./installer"; | ||
|
|
||
| export class WindowsInstaller extends CliInstaller implements Installer { | ||
| private readonly platform: SupportedPlatform = "win32"; // Node.js platform identifier for Windows | ||
|
|
||
| public constructor(version: string) { | ||
| super(version); | ||
| } | ||
|
|
||
| public async installCli(): Promise<void> { | ||
| const urlBuilder = cliUrlBuilder[this.platform]; | ||
| await super.install(urlBuilder(this.version, this.arch)); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.