Merge pull request #41 from crazy-max/fix-docker

docker: fix instance
This commit is contained in:
CrazyMax 2023-02-18 07:32:33 +01:00 committed by GitHub
commit b3a30a9e32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 29 deletions

View File

@ -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,

View File

@ -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 {

View File

@ -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> {

View File

@ -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;

View File

@ -20,6 +20,7 @@ import {Install} from './buildx/install';
import {Builder} from './buildx/builder';
import {BuildKit} from './buildkit/buildkit';
import {GitHub} from './github';
import {Docker} from './docker';
export interface ToolkitOpts {
/**
@ -32,6 +33,7 @@ export interface ToolkitOpts {
export class Toolkit {
public context: Context;
public github: GitHub;
public docker: Docker;
public buildx: Buildx;
public buildxInstall: Install;
public builder: Builder;
@ -40,7 +42,8 @@ export class Toolkit {
constructor(opts: ToolkitOpts = {}) {
this.context = new Context();
this.github = new GitHub({token: opts.githubToken});
this.buildx = new Buildx({context: this.context});
this.docker = Docker.getInstance();
this.buildx = new Buildx({context: this.context, standalone: !this.docker.available});
this.buildxInstall = new Install({context: this.context, standalone: this.buildx.standalone});
this.builder = new Builder({context: this.context, buildx: this.buildx});
this.buildkit = new BuildKit({context: this.context, buildx: this.buildx});