Skip to content

Commit b3a30a9

Browse files
authored
Merge pull request #41 from crazy-max/fix-docker
docker: fix instance
2 parents 2c60cad + 05bbe49 commit b3a30a9

File tree

5 files changed

+43
-29
lines changed

5 files changed

+43
-29
lines changed

__tests__/docker.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ describe('configDir', () => {
4848
});
4949

5050
describe('isAvailable', () => {
51-
it('cli', () => {
51+
it('cli', async () => {
5252
const execSpy = jest.spyOn(exec, 'getExecOutput');
53-
Docker.isAvailable;
53+
Docker.getInstance().available;
5454
// eslint-disable-next-line jest/no-standalone-expect
5555
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
5656
silent: true,

src/buildx/buildx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export class Buildx {
4141

4242
constructor(opts: BuildxOpts) {
4343
this.context = opts.context;
44+
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
4445
this.inputs = new Inputs(this.context);
45-
this.standalone = opts?.standalone ?? !Docker.isAvailable;
4646
}
4747

4848
static get configDir(): string {

src/buildx/install.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Install {
4242

4343
constructor(opts?: InstallOpts) {
4444
this.context = opts?.context || new Context();
45-
this.standalone = opts?.standalone ?? !Docker.isAvailable;
45+
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
4646
}
4747

4848
public async download(version: string, dest?: string): Promise<string> {

src/docker.ts

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,47 @@ import * as core from '@actions/core';
2020
import * as exec from '@actions/exec';
2121

2222
export class Docker {
23+
private static instance?: Docker;
24+
static getInstance = (): Docker => (Docker.instance = Docker.instance ?? new Docker());
25+
26+
private _available: boolean | undefined;
27+
28+
// eslint-disable-next-line @typescript-eslint/no-empty-function
29+
private constructor() {}
30+
2331
static get configDir(): string {
2432
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
2533
}
2634

27-
static get isAvailable(): boolean {
28-
let dockerAvailable = false;
29-
exec
30-
.getExecOutput('docker', undefined, {
31-
ignoreReturnCode: true,
32-
silent: true
33-
})
34-
.then(res => {
35-
if (res.stderr.length > 0 && res.exitCode != 0) {
36-
core.debug(`Docker.isAvailable error: ${res.stderr}`);
37-
dockerAvailable = false;
38-
} else {
39-
core.debug(`Docker.isAvailable ok`);
40-
dockerAvailable = res.exitCode == 0;
41-
}
42-
})
43-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
44-
.catch(error => {
45-
core.debug(`Docker.isAvailable failed: ${error}`);
46-
dockerAvailable = false;
47-
});
48-
return dockerAvailable;
35+
get available() {
36+
return (async () => {
37+
if (!this._available) {
38+
this._available = await exec
39+
.getExecOutput('docker', undefined, {
40+
ignoreReturnCode: true,
41+
silent: true
42+
})
43+
.then(res => {
44+
if (res.stderr.length > 0 && res.exitCode != 0) {
45+
core.debug(`Docker.isAvailable error: ${res.stderr}`);
46+
return false;
47+
} else {
48+
core.debug(`Docker.isAvailable ok`);
49+
return res.exitCode == 0;
50+
}
51+
})
52+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
53+
.catch(error => {
54+
core.debug(`Docker.isAvailable failed: ${error}`);
55+
return false;
56+
});
57+
}
58+
return this._available;
59+
})();
4960
}
5061

5162
public static async printVersion(standalone?: boolean): Promise<void> {
52-
const noDocker = standalone ?? !Docker.isAvailable;
63+
const noDocker = standalone ?? !Docker.getInstance().available;
5364
if (noDocker) {
5465
core.debug('Docker.printVersion: Docker is not available, skipping.');
5566
return;
@@ -60,7 +71,7 @@ export class Docker {
6071
}
6172

6273
public static async printInfo(standalone?: boolean): Promise<void> {
63-
const noDocker = standalone ?? !Docker.isAvailable;
74+
const noDocker = standalone ?? !Docker.getInstance().available;
6475
if (noDocker) {
6576
core.debug('Docker.printInfo: Docker is not available, skipping.');
6677
return;

src/toolkit.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {Install} from './buildx/install';
2020
import {Builder} from './buildx/builder';
2121
import {BuildKit} from './buildkit/buildkit';
2222
import {GitHub} from './github';
23+
import {Docker} from './docker';
2324

2425
export interface ToolkitOpts {
2526
/**
@@ -32,6 +33,7 @@ export interface ToolkitOpts {
3233
export class Toolkit {
3334
public context: Context;
3435
public github: GitHub;
36+
public docker: Docker;
3537
public buildx: Buildx;
3638
public buildxInstall: Install;
3739
public builder: Builder;
@@ -40,7 +42,8 @@ export class Toolkit {
4042
constructor(opts: ToolkitOpts = {}) {
4143
this.context = new Context();
4244
this.github = new GitHub({token: opts.githubToken});
43-
this.buildx = new Buildx({context: this.context});
45+
this.docker = Docker.getInstance();
46+
this.buildx = new Buildx({context: this.context, standalone: !this.docker.available});
4447
this.buildxInstall = new Install({context: this.context, standalone: this.buildx.standalone});
4548
this.builder = new Builder({context: this.context, buildx: this.buildx});
4649
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});

0 commit comments

Comments
 (0)