From fdc8b5d37be92d4a558fa3c6cafb7ec16ba058cb Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 23 Jan 2023 14:48:00 +0100 Subject: [PATCH] docker: set default standalone Signed-off-by: CrazyMax --- __tests__/buildx.test.ts | 4 +++- __tests__/docker.test.ts | 2 +- src/buildx.ts | 14 +------------- src/docker.ts | 16 ++++++++++------ 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/__tests__/buildx.test.ts b/__tests__/buildx.test.ts index 70922f7..24f19f0 100644 --- a/__tests__/buildx.test.ts +++ b/__tests__/buildx.test.ts @@ -78,7 +78,9 @@ describe('hasLocalOrTarExporter', () => { describe('isAvailable', () => { it('docker cli', async () => { const execSpy = jest.spyOn(exec, 'getExecOutput'); - const buildx = new Buildx(); + const buildx = new Buildx({ + standalone: false + }); await buildx.isAvailable(); // eslint-disable-next-line jest/no-standalone-expect expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], { diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index b15cd6e..ec14466 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -19,7 +19,7 @@ describe('isAvailable', () => { describe('info', () => { it('standard', () => { const execSpy = jest.spyOn(exec, 'exec'); - Docker.info(); + Docker.info(false); expect(execSpy).toHaveBeenCalledWith(`docker`, ['version'], { failOnStdErr: false }); diff --git a/src/buildx.ts b/src/buildx.ts index be5d622..f86c0a6 100644 --- a/src/buildx.ts +++ b/src/buildx.ts @@ -18,22 +18,10 @@ export class Buildx { private tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep); constructor(opts?: BuildxOpts) { - this.standalone = opts?.standalone ?? this.isStandalone(); + this.standalone = opts?.standalone ?? !Docker.isAvailable(); this.version = this.getVersion(); } - private isStandalone(): boolean { - let dockerAvailable = false; - Docker.isAvailable() - .then((res: boolean) => { - dockerAvailable = res; - }) - .catch(e => { - dockerAvailable = false; - }); - return dockerAvailable; - } - public getCommand(args: Array) { return { command: this.standalone ? 'buildx' : 'docker', diff --git a/src/docker.ts b/src/docker.ts index 1e887fd..1da940f 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -2,26 +2,30 @@ import * as core from '@actions/core'; import * as exec from '@actions/exec'; export class Docker { - public static async isAvailable(): Promise { - return await exec + public static isAvailable(): boolean { + let dockerAvailable = false; + exec .getExecOutput('docker', undefined, { ignoreReturnCode: true, silent: true }) .then(res => { if (res.stderr.length > 0 && res.exitCode != 0) { - return false; + dockerAvailable = false; + } else { + dockerAvailable = res.exitCode == 0; } - return res.exitCode == 0; }) // eslint-disable-next-line @typescript-eslint/no-unused-vars .catch(error => { - return false; + dockerAvailable = false; }); + return dockerAvailable; } public static async info(standalone?: boolean) { - if (standalone) { + const dockerAvailable = standalone ?? !Docker.isAvailable(); + if (dockerAvailable) { core.info(`Docker info skipped in standalone mode`); } else { await exec.exec('docker', ['version'], {