Merge pull request #64 from crazy-max/builder-exists
builder: exists method
This commit is contained in:
@@ -19,6 +19,7 @@ import * as fs from 'fs';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
|
||||||
import {Builder} from '../../src/buildx/builder';
|
import {Builder} from '../../src/buildx/builder';
|
||||||
|
import {Exec} from '../../src/exec';
|
||||||
|
|
||||||
import {BuilderInfo} from '../../src/types/builder';
|
import {BuilderInfo} from '../../src/types/builder';
|
||||||
|
|
||||||
@@ -47,6 +48,19 @@ jest.spyOn(Builder.prototype, 'inspect').mockImplementation(async (): Promise<Bu
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('exists', () => {
|
||||||
|
it('valid', async () => {
|
||||||
|
const execSpy = jest.spyOn(Exec, 'getExecOutput');
|
||||||
|
const builder = new Builder();
|
||||||
|
await builder.exists('foo');
|
||||||
|
// eslint-disable-next-line jest/no-standalone-expect
|
||||||
|
expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx', 'inspect', 'foo'], {
|
||||||
|
silent: true,
|
||||||
|
ignoreReturnCode: true
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('inspect', () => {
|
describe('inspect', () => {
|
||||||
it('valid', async () => {
|
it('valid', async () => {
|
||||||
const builder = new Builder();
|
const builder = new Builder();
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import * as core from '@actions/core';
|
||||||
|
|
||||||
import {Buildx} from './buildx';
|
import {Buildx} from './buildx';
|
||||||
import {Exec} from '../exec';
|
import {Exec} from '../exec';
|
||||||
|
|
||||||
@@ -30,6 +32,29 @@ export class Builder {
|
|||||||
this.buildx = opts?.buildx || new Buildx();
|
this.buildx = opts?.buildx || new Buildx();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async exists(name: string): Promise<boolean> {
|
||||||
|
const cmd = await this.buildx.getCommand(['inspect', name]);
|
||||||
|
|
||||||
|
const ok: boolean = await Exec.getExecOutput(cmd.command, cmd.args, {
|
||||||
|
ignoreReturnCode: true,
|
||||||
|
silent: true
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
|
core.debug(`Builder.exists cmd err: ${res.stderr.trim()}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return res.exitCode == 0;
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
core.debug(`Builder.exists error: ${error}`);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
core.debug(`Builder.exists: ${ok}`);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
|
||||||
public async inspect(name: string): Promise<BuilderInfo> {
|
public async inspect(name: string): Promise<BuilderInfo> {
|
||||||
const cmd = await this.buildx.getCommand(['inspect', name]);
|
const cmd = await this.buildx.getCommand(['inspect', name]);
|
||||||
return await Exec.getExecOutput(cmd.command, cmd.args, {
|
return await Exec.getExecOutput(cmd.command, cmd.args, {
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ export class Buildx {
|
|||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||||
core.debug(`Buildx.isAvailable cmd err: ${res.stderr}`);
|
core.debug(`Buildx.isAvailable cmd err: ${res.stderr.trim()}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return res.exitCode == 0;
|
return res.exitCode == 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user