diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index a0381a4..6550d53 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -48,9 +48,9 @@ describe('configDir', () => { }); describe('isAvailable', () => { - it('cli', () => { + it('cli', async () => { const execSpy = jest.spyOn(exec, 'getExecOutput'); - Docker.isAvailable; + Docker.getInstance().available; // eslint-disable-next-line jest/no-standalone-expect expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, { silent: true, diff --git a/src/buildx/buildx.ts b/src/buildx/buildx.ts index ec8930a..cc71e0b 100644 --- a/src/buildx/buildx.ts +++ b/src/buildx/buildx.ts @@ -41,8 +41,8 @@ export class Buildx { constructor(opts: BuildxOpts) { this.context = opts.context; + this.standalone = opts?.standalone ?? !Docker.getInstance().available; this.inputs = new Inputs(this.context); - this.standalone = opts?.standalone ?? !Docker.isAvailable; } static get configDir(): string { diff --git a/src/buildx/install.ts b/src/buildx/install.ts index 04f0bbf..f206dc9 100644 --- a/src/buildx/install.ts +++ b/src/buildx/install.ts @@ -42,7 +42,7 @@ export class Install { constructor(opts?: InstallOpts) { this.context = opts?.context || new Context(); - this.standalone = opts?.standalone ?? !Docker.isAvailable; + this.standalone = opts?.standalone ?? !Docker.getInstance().available; } public async download(version: string, dest?: string): Promise { diff --git a/src/docker.ts b/src/docker.ts index 57acf2b..bf75a8a 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -20,36 +20,47 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; export class Docker { + private static instance?: Docker; + static getInstance = (): Docker => (Docker.instance = Docker.instance ?? new Docker()); + + private _available: boolean | undefined; + + // eslint-disable-next-line @typescript-eslint/no-empty-function + private constructor() {} + static get configDir(): string { return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); } - static get isAvailable(): boolean { - let dockerAvailable = false; - exec - .getExecOutput('docker', undefined, { - ignoreReturnCode: true, - silent: true - }) - .then(res => { - if (res.stderr.length > 0 && res.exitCode != 0) { - core.debug(`Docker.isAvailable error: ${res.stderr}`); - dockerAvailable = false; - } else { - core.debug(`Docker.isAvailable ok`); - dockerAvailable = res.exitCode == 0; - } - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .catch(error => { - core.debug(`Docker.isAvailable failed: ${error}`); - dockerAvailable = false; - }); - return dockerAvailable; + get available() { + return (async () => { + if (!this._available) { + this._available = await exec + .getExecOutput('docker', undefined, { + ignoreReturnCode: true, + silent: true + }) + .then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + core.debug(`Docker.isAvailable error: ${res.stderr}`); + return false; + } else { + core.debug(`Docker.isAvailable ok`); + return res.exitCode == 0; + } + }) + // eslint-disable-next-line @typescript-eslint/no-unused-vars + .catch(error => { + core.debug(`Docker.isAvailable failed: ${error}`); + return false; + }); + } + return this._available; + })(); } public static async printVersion(standalone?: boolean): Promise { - const noDocker = standalone ?? !Docker.isAvailable; + const noDocker = standalone ?? !Docker.getInstance().available; if (noDocker) { core.debug('Docker.printVersion: Docker is not available, skipping.'); return; @@ -60,7 +71,7 @@ export class Docker { } public static async printInfo(standalone?: boolean): Promise { - const noDocker = standalone ?? !Docker.isAvailable; + const noDocker = standalone ?? !Docker.getInstance().available; if (noDocker) { core.debug('Docker.printInfo: Docker is not available, skipping.'); return;