Skip to content

Commit

Permalink
Use built-in getExecOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Jun 8, 2021
1 parent 707dc1f commit daeb954
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 128 deletions.
19 changes: 18 additions & 1 deletion __tests__/buildx.test.ts
Expand Up @@ -2,9 +2,26 @@ import * as semver from 'semver';
import * as buildx from '../src/buildx';
import * as exec from '@actions/exec';

describe('isAvailable', () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
execSpy.mockImplementation(() =>
Promise.resolve({
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
})
);

buildx.isAvailable();

expect(execSpy).toHaveBeenCalledWith(`docker`, ['buildx'], {
silent: true,
ignoreReturnCode: true
});
});

describe('getVersion', () => {
it('valid', async () => {
await exec.exec('docker', ['buildx', 'version']);
const version = await buildx.getVersion();
console.log(`version: ${version}`);
expect(semver.valid(version)).not.toBeNull();
Expand Down
97 changes: 24 additions & 73 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jest.config.js
@@ -1,5 +1,6 @@
module.exports = {
clearMocks: true,
restoreMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
Expand Down
38 changes: 24 additions & 14 deletions src/buildx.ts
@@ -1,28 +1,38 @@
import * as semver from 'semver';
import * as exec from './exec';
import * as exec from '@actions/exec';

export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {
return false;
}
return res.success;
});
return await exec
.getExecOutput('docker', ['buildx'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
return false;
}
return res.exitCode == 0;
});
}

export async function getVersion(): Promise<string> {
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return parseVersion(res.stdout);
});
return await exec
.getExecOutput('docker', ['buildx', 'version'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
return parseVersion(res.stdout);
});
}

export async function parseVersion(stdout: string): Promise<string> {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
throw new Error(`Cannot parse buildx version`);
}
return semver.clean(matches[1]);
}
34 changes: 0 additions & 34 deletions src/exec.ts

This file was deleted.

15 changes: 9 additions & 6 deletions src/main.ts
@@ -1,6 +1,5 @@
import * as buildx from './buildx';
import * as context from './context';
import * as mexec from './exec';
import * as core from '@actions/core';
import * as exec from '@actions/exec';

Expand All @@ -24,11 +23,15 @@ async function run(): Promise<void> {
await exec.exec('docker', [...args, '--print']);
core.endGroup();

await mexec.exec('docker', args).then(res => {
if (res.stderr.length > 0 && !res.success) {
throw new Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)![0]}`);
}
});
await exec
.getExecOutput('docker', args, {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)![0].trim()}`);
}
});
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit daeb954

Please sign in to comment.