mirror of
https://github.com/docker/bake-action.git
synced 2025-12-31 02:01:19 +01:00
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import * as semver from 'semver';
|
|
import * as exec from '@actions/exec';
|
|
|
|
import * as context from './context';
|
|
|
|
export async function getMetadataFile(): Promise<string> {
|
|
return path.join(context.tmpDir(), 'metadata-file').split(path.sep).join(path.posix.sep);
|
|
}
|
|
|
|
export async function getMetadata(): Promise<string | undefined> {
|
|
const metadataFile = await getMetadataFile();
|
|
if (!fs.existsSync(metadataFile)) {
|
|
return undefined;
|
|
}
|
|
return fs.readFileSync(metadataFile, {encoding: 'utf-8'});
|
|
}
|
|
|
|
export async function isAvailable(): Promise<Boolean> {
|
|
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
|
|
.getExecOutput('docker', ['buildx', 'version'], {
|
|
ignoreReturnCode: true,
|
|
silent: true
|
|
})
|
|
.then(res => {
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
throw new Error(res.stderr.trim());
|
|
}
|
|
return parseVersion(res.stdout.trim());
|
|
});
|
|
}
|
|
|
|
export function parseVersion(stdout: string): string {
|
|
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
|
|
if (!matches) {
|
|
throw new Error(`Cannot parse buildx version`);
|
|
}
|
|
return matches[1];
|
|
}
|
|
|
|
export function satisfies(version: string, range: string): boolean {
|
|
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
|
|
}
|