docker: remove singleton

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-19 02:37:43 +01:00
parent 28c11a1819
commit cd825ae548
4 changed files with 25 additions and 37 deletions

View File

@@ -50,7 +50,7 @@ describe('configDir', () => {
describe('isAvailable', () => { describe('isAvailable', () => {
it('cli', async () => { it('cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput'); const execSpy = jest.spyOn(exec, 'getExecOutput');
await Docker.getInstance().isAvailable(); await Docker.isAvailable();
// eslint-disable-next-line jest/no-standalone-expect // eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, { expect(execSpy).toHaveBeenCalledWith(`docker`, undefined, {
silent: true, silent: true,

View File

@@ -55,7 +55,7 @@ export class Buildx {
} }
public async isStandalone(): Promise<boolean> { public async isStandalone(): Promise<boolean> {
const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable()); const standalone = this._standalone ?? !(await Docker.isAvailable());
core.debug(`Buildx.isStandalone: ${standalone}`); core.debug(`Buildx.isStandalone: ${standalone}`);
return standalone; return standalone;
} }

View File

@@ -143,7 +143,7 @@ export class Install {
} }
private async isStandalone(): Promise<boolean> { private async isStandalone(): Promise<boolean> {
const standalone = this._standalone ?? !(await Docker.getInstance().isAvailable()); const standalone = this._standalone ?? !(await Docker.isAvailable());
core.debug(`Install.isStandalone: ${standalone}`); core.debug(`Install.isStandalone: ${standalone}`);
return standalone; return standalone;
} }

View File

@@ -20,46 +20,34 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
export class Docker { 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 { static get configDir(): string {
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
} }
public async isAvailable(): Promise<boolean> { public static async isAvailable(): Promise<boolean> {
if (this._available === undefined) { const ok: boolean = await exec
await exec .getExecOutput('docker', undefined, {
.getExecOutput('docker', undefined, { ignoreReturnCode: true,
ignoreReturnCode: true, silent: true
silent: true })
}) .then(res => {
.then(res => { if (res.stderr.length > 0 && res.exitCode != 0) {
if (res.stderr.length > 0 && res.exitCode != 0) { core.debug(`Docker.isAvailable cmd err: ${res.stderr}`);
core.debug(`Docker.available error: ${res.stderr}`); return false;
this._available = false; }
} else { return res.exitCode == 0;
core.debug(`Docker.available ok`); })
this._available = res.exitCode == 0; .catch(error => {
} core.debug(`Docker.isAvailable error: ${error}`);
}) return false;
// eslint-disable-next-line @typescript-eslint/no-unused-vars });
.catch(error => {
core.debug(`Docker.available failed: ${error}`); core.debug(`Docker.isAvailable: ${ok}`);
this._available = false; return ok;
});
}
core.debug(`Docker.available: ${this._available}`);
return this._available ?? false;
} }
public static async printVersion(standalone?: boolean): Promise<void> { public static async printVersion(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !(await Docker.getInstance().isAvailable()); const noDocker = standalone ?? !(await Docker.isAvailable());
if (noDocker) { if (noDocker) {
core.debug('Docker.printVersion: Docker is not available, skipping.'); core.debug('Docker.printVersion: Docker is not available, skipping.');
return; return;
@@ -70,7 +58,7 @@ export class Docker {
} }
public static async printInfo(standalone?: boolean): Promise<void> { public static async printInfo(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !(await Docker.getInstance().isAvailable()); const noDocker = standalone ?? !(await Docker.isAvailable());
if (noDocker) { if (noDocker) {
core.debug('Docker.printInfo: Docker is not available, skipping.'); core.debug('Docker.printInfo: Docker is not available, skipping.');
return; return;