|
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { execa } from 'execa'; |
| 4 | +import { injectFromHierarchy, injectable } from 'inversify'; |
| 5 | +import { BaseInstallService } from '../install-tool/base-install.service'; |
| 6 | + |
| 7 | +@injectable() |
| 8 | +@injectFromHierarchy() |
| 9 | +export class ApkoInstallService extends BaseInstallService { |
| 10 | + readonly name = 'apko'; |
| 11 | + |
| 12 | + private get ghArch(): string { |
| 13 | + switch (this.envSvc.arch) { |
| 14 | + case 'arm64': |
| 15 | + return 'arm64'; |
| 16 | + case 'amd64': |
| 17 | + return 'amd64'; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + override async install(version: string): Promise<void> { |
| 22 | + /** |
| 23 | + * @example |
| 24 | + * @see {@href https://github.com/chainguard-dev/apko/releases/tag/v0.30.11} |
| 25 | + */ |
| 26 | + const baseUrl = `https://github.com/chainguard-dev/apko/releases/download/v${version}/`; |
| 27 | + |
| 28 | + const filename = `apko_${version}_linux_${this.ghArch}.tar.gz`; |
| 29 | + |
| 30 | + const checksumFile = await this.http.download({ |
| 31 | + url: `${baseUrl}checksums.txt`, |
| 32 | + }); |
| 33 | + const expectedChecksum = (await fs.readFile(checksumFile, 'utf-8')) |
| 34 | + .split('\n') |
| 35 | + .find((l) => l.includes(filename)) |
| 36 | + ?.split(' ')[0]; |
| 37 | + |
| 38 | + const file = await this.http.download({ |
| 39 | + url: `${baseUrl}${filename}`, |
| 40 | + checksumType: 'sha256', |
| 41 | + expectedChecksum, |
| 42 | + }); |
| 43 | + |
| 44 | + await this.pathSvc.ensureToolPath(this.name); |
| 45 | + |
| 46 | + const path = join( |
| 47 | + await this.pathSvc.createVersionedToolPath(this.name, version), |
| 48 | + 'bin', |
| 49 | + ); |
| 50 | + await fs.mkdir(path); |
| 51 | + await this.compress.extract({ |
| 52 | + file, |
| 53 | + cwd: path, |
| 54 | + strip: 1, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + override async link(version: string): Promise<void> { |
| 59 | + const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin'); |
| 60 | + await this.shellwrapper({ srcDir: src }); |
| 61 | + } |
| 62 | + |
| 63 | + override async test(_version: string): Promise<void> { |
| 64 | + await execa(this.name, ['version'], { stdio: ['inherit', 'inherit', 1] }); |
| 65 | + } |
| 66 | +} |
0 commit comments