docker: fix instance
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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<string> {
|
||||
|
||||
@@ -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<void> {
|
||||
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<void> {
|
||||
const noDocker = standalone ?? !Docker.isAvailable;
|
||||
const noDocker = standalone ?? !Docker.getInstance().available;
|
||||
if (noDocker) {
|
||||
core.debug('Docker.printInfo: Docker is not available, skipping.');
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user