2020-08-17 16:18:15 -04:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2020-10-20 09:18:02 -04:00
|
|
|
import csvparse from 'csv-parse/lib/sync';
|
2020-08-22 21:31:38 -04:00
|
|
|
import * as semver from 'semver';
|
2020-08-17 16:18:15 -04:00
|
|
|
import * as context from './context';
|
2020-08-15 18:36:41 -04:00
|
|
|
import * as exec from './exec';
|
|
|
|
|
2020-08-17 16:18:15 -04:00
|
|
|
export async function getImageIDFile(): Promise<string> {
|
2020-10-19 15:17:06 -04:00
|
|
|
return path.join(context.tmpDir(), 'iidfile').split(path.sep).join(path.posix.sep);
|
2020-08-17 16:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 04:07:11 -04:00
|
|
|
export async function getSecret(kvp: string): Promise<string> {
|
|
|
|
const [key, value] = kvp.split('=');
|
2020-10-19 15:17:06 -04:00
|
|
|
const secretFile = context.tmpNameSync({
|
|
|
|
tmpdir: context.tmpDir()
|
2020-09-02 04:07:11 -04:00
|
|
|
});
|
|
|
|
await fs.writeFileSync(secretFile, value);
|
|
|
|
return `id=${key},src=${secretFile}`;
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:12:33 -04:00
|
|
|
export function isLocalOrTarExporter(outputs: string[]): Boolean {
|
2020-10-20 09:18:02 -04:00
|
|
|
for (let output of csvparse(outputs.join(`\n`), {
|
|
|
|
delimiter: ',',
|
|
|
|
trim: true,
|
|
|
|
columns: false,
|
|
|
|
relax_column_count: true
|
|
|
|
})) {
|
2020-10-20 11:53:03 -04:00
|
|
|
// Local if no type is defined
|
|
|
|
// https://github.com/docker/buildx/blob/d2bf42f8b4784d83fde17acb3ed84703ddc2156b/build/output.go#L29-L43
|
|
|
|
if (output.length == 1 && !output[0].startsWith('type=')) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-20 09:18:02 -04:00
|
|
|
for (let [key, value] of output.map(chunk => chunk.split('=').map(item => item.trim()))) {
|
2020-10-19 16:12:33 -04:00
|
|
|
if (key == 'type' && (value == 'local' || value == 'tar')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hasGitAuthToken(secrets: string[]): Boolean {
|
|
|
|
for (let secret of secrets) {
|
|
|
|
if (secret.startsWith('GIT_AUTH_TOKEN=')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-15 18:36:41 -04: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-22 21:31:38 -04: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-22 22:01:20 -04:00
|
|
|
const matches = /\sv?([0-9.]+)/.exec(stdout);
|
2020-08-22 21:31:38 -04:00
|
|
|
if (!matches) {
|
|
|
|
throw new Error(`Cannot parse Buildx version`);
|
|
|
|
}
|
|
|
|
return semver.clean(matches[1]);
|
|
|
|
}
|