Files
bake-action/src/buildx.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

import fs from 'fs';
import path from 'path';
2020-10-08 00:52:52 +02:00
import * as semver from 'semver';
2021-06-08 20:40:23 +02:00
import * as exec from '@actions/exec';
2020-10-08 00:52:52 +02:00
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'});
}
2020-10-08 00:52:52 +02:00
export async function isAvailable(): Promise<Boolean> {
2021-06-08 20:40:23 +02:00
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;
});
2020-10-08 00:52:52 +02:00
}
export async function getVersion(): Promise<string> {
2021-06-08 20:40:23 +02:00
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());
2021-06-08 20:40:23 +02:00
}
return parseVersion(res.stdout.trim());
2021-06-08 20:40:23 +02:00
});
2020-10-08 00:52:52 +02:00
}
export function parseVersion(stdout: string): string {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
2020-10-08 00:52:52 +02:00
if (!matches) {
2021-06-08 20:40:23 +02:00
throw new Error(`Cannot parse buildx version`);
2020-10-08 00:52:52 +02:00
}
return matches[1];
2020-10-08 00:52:52 +02:00
}
export function satisfies(version: string, range: string): boolean {
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
}