2020-08-17 20:18:15 +00:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-09-02 08:07:11 +00:00
|
|
|
import tmp from 'tmp';
|
2020-08-23 01:31:38 +00:00
|
|
|
import * as semver from 'semver';
|
2020-08-17 20:18:15 +00:00
|
|
|
import * as context from './context';
|
2020-08-15 22:36:41 +00:00
|
|
|
import * as exec from './exec';
|
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
export async function getImageIDFile(): Promise<string> {
|
|
|
|
return path.join(context.tmpDir, 'iidfile');
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getImageID(): Promise<string | undefined> {
|
|
|
|
const iidFile = await getImageIDFile();
|
|
|
|
if (!fs.existsSync(iidFile)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:07:11 +00:00
|
|
|
export async function getSecret(kvp: string): Promise<string> {
|
|
|
|
const [key, value] = kvp.split('=');
|
|
|
|
const secretFile = tmp.tmpNameSync({
|
|
|
|
tmpdir: context.tmpDir
|
|
|
|
});
|
|
|
|
await fs.writeFileSync(secretFile, value);
|
|
|
|
return `id=${key},src=${secretFile}`;
|
|
|
|
}
|
|
|
|
|
2020-08-15 22:36:41 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-23 01:31:38 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function parseVersion(stdout: string): Promise<string> {
|
2020-08-23 02:01:20 +00:00
|
|
|
const matches = /\sv?([0-9.]+)/.exec(stdout);
|
2020-08-23 01:31:38 +00:00
|
|
|
if (!matches) {
|
|
|
|
throw new Error(`Cannot parse Buildx version`);
|
|
|
|
}
|
|
|
|
return semver.clean(matches[1]);
|
|
|
|
}
|