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
5 changed files with 43 additions and 29 deletions

View File

@@ -48,9 +48,9 @@ describe('configDir', () => {
}); });
describe('isAvailable', () => { describe('isAvailable', () => {
it('cli', () => { it('cli', async () => {
const execSpy = jest.spyOn(exec, 'getExecOutput'); const execSpy = jest.spyOn(exec, 'getExecOutput');
Docker.isAvailable; Docker.getInstance().available;
// 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

@@ -41,8 +41,8 @@ export class Buildx {
constructor(opts: BuildxOpts) { constructor(opts: BuildxOpts) {
this.context = opts.context; this.context = opts.context;
this.standalone = opts?.standalone ?? !Docker.getInstance().available;
this.inputs = new Inputs(this.context); this.inputs = new Inputs(this.context);
this.standalone = opts?.standalone ?? !Docker.isAvailable;
} }
static get configDir(): string { static get configDir(): string {

View File

@@ -42,7 +42,7 @@ export class Install {
constructor(opts?: InstallOpts) { constructor(opts?: InstallOpts) {
this.context = opts?.context || new Context(); 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> { 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'; 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');
} }
static get isAvailable(): boolean { get available() {
let dockerAvailable = false; return (async () => {
exec if (!this._available) {
.getExecOutput('docker', undefined, { this._available = await exec
ignoreReturnCode: true, .getExecOutput('docker', undefined, {
silent: true ignoreReturnCode: true,
}) silent: true
.then(res => { })
if (res.stderr.length > 0 && res.exitCode != 0) { .then(res => {
core.debug(`Docker.isAvailable error: ${res.stderr}`); if (res.stderr.length > 0 && res.exitCode != 0) {
dockerAvailable = false; core.debug(`Docker.isAvailable error: ${res.stderr}`);
} else { return false;
core.debug(`Docker.isAvailable ok`); } else {
dockerAvailable = res.exitCode == 0; core.debug(`Docker.isAvailable ok`);
} return res.exitCode == 0;
}) }
// eslint-disable-next-line @typescript-eslint/no-unused-vars })
.catch(error => { // eslint-disable-next-line @typescript-eslint/no-unused-vars
core.debug(`Docker.isAvailable failed: ${error}`); .catch(error => {
dockerAvailable = false; core.debug(`Docker.isAvailable failed: ${error}`);
}); return false;
return dockerAvailable; });
}
return this._available;
})();
} }
public static async printVersion(standalone?: boolean): Promise<void> { public static async printVersion(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable; const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) { if (noDocker) {
core.debug('Docker.printVersion: Docker is not available, skipping.'); core.debug('Docker.printVersion: Docker is not available, skipping.');
return; return;
@@ -60,7 +71,7 @@ export class Docker {
} }
public static async printInfo(standalone?: boolean): Promise<void> { public static async printInfo(standalone?: boolean): Promise<void> {
const noDocker = standalone ?? !Docker.isAvailable; const noDocker = standalone ?? !Docker.getInstance().available;
if (noDocker) { if (noDocker) {
core.debug('Docker.printInfo: Docker is not available, skipping.'); core.debug('Docker.printInfo: Docker is not available, skipping.');
return; return;

View File

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