buildx: rename inputs class to build

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2024-04-24 17:08:15 +02:00
parent aece826a8c
commit e03b1899b0
No known key found for this signature in database
GPG Key ID: ADE44D8C9D44FBE4
3 changed files with 44 additions and 46 deletions

View File

@ -20,7 +20,7 @@ import * as path from 'path';
import * as rimraf from 'rimraf';
import {Context} from '../../src/context';
import {Inputs} from '../../src/buildx/inputs';
import {Build} from '../../src/buildx/build';
const fixturesDir = path.join(__dirname, '..', 'fixtures');
// prettier-ignore
@ -53,27 +53,27 @@ afterEach(() => {
describe('resolveBuildImageID', () => {
it('matches', async () => {
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = Inputs.getBuildImageIDFilePath();
const imageIDFile = Build.getBuildImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID);
const expected = Inputs.resolveBuildImageID();
const expected = Build.resolveBuildImageID();
expect(expected).toEqual(imageID);
});
});
describe('resolveBuildMetadata', () => {
it('matches', async () => {
const metadataFile = Inputs.getBuildMetadataFilePath();
const metadataFile = Build.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = Inputs.resolveBuildMetadata();
const expected = Build.resolveBuildMetadata();
expect(expected).toEqual(metadata);
});
});
describe('resolveDigest', () => {
it('matches', async () => {
const metadataFile = Inputs.getBuildMetadataFilePath();
const metadataFile = Build.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = Inputs.resolveDigest();
const expected = Build.resolveDigest();
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
});
});
@ -120,7 +120,7 @@ describe('getProvenanceInput', () => {
],
])('given input %p', async (input: string, expected: string) => {
await setInput('provenance', input);
expect(Inputs.getProvenanceInput('provenance')).toEqual(expected);
expect(Build.getProvenanceInput('provenance')).toEqual(expected);
});
});
@ -148,7 +148,7 @@ describe('resolveProvenanceAttrs', () => {
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/123'
],
])('given %p', async (input: string, expected: string) => {
expect(Inputs.resolveProvenanceAttrs(input)).toEqual(expected);
expect(Build.resolveProvenanceAttrs(input)).toEqual(expected);
});
});
@ -166,9 +166,9 @@ describe('resolveBuildSecret', () => {
try {
let secret: string;
if (file) {
secret = Inputs.resolveBuildSecretFile(kvp);
secret = Build.resolveBuildSecretFile(kvp);
} else {
secret = Inputs.resolveBuildSecretString(kvp);
secret = Build.resolveBuildSecretString(kvp);
}
expect(secret).toEqual(`id=${exKey},src=${tmpName}`);
expect(fs.readFileSync(tmpName, 'utf-8')).toEqual(exValue);
@ -185,7 +185,7 @@ describe('resolveBuildSecret', () => {
['FOO=bar=baz', 'FOO', 'bar=baz', null]
])('given %p key and %p env', async (kvp: string, exKey: string, exValue: string, error: Error | null) => {
try {
const secret = Inputs.resolveBuildSecretEnv(kvp);
const secret = Build.resolveBuildSecretEnv(kvp);
expect(secret).toEqual(`id=${exKey},env=${exValue}`);
} catch (e) {
// eslint-disable-next-line jest/no-conditional-expect
@ -206,7 +206,7 @@ describe('hasLocalExporter', () => {
[['" type= local" , dest=./release-out'], true],
[['.'], true]
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
expect(Inputs.hasLocalExporter(exporters)).toEqual(expected);
expect(Build.hasLocalExporter(exporters)).toEqual(expected);
});
});
@ -222,7 +222,7 @@ describe('hasTarExporter', () => {
[['" type= local" , dest=./release-out'], false],
[['.'], false]
])('given %p returns %p', async (exporters: Array<string>, expected: boolean) => {
expect(Inputs.hasTarExporter(exporters)).toEqual(expected);
expect(Build.hasTarExporter(exporters)).toEqual(expected);
});
});
@ -240,7 +240,7 @@ describe('hasDockerExporter', () => {
[['type=docker'], true, true],
[['.'], true, true],
])('given %p returns %p', async (exporters: Array<string>, expected: boolean, load: boolean | undefined) => {
expect(Inputs.hasDockerExporter(exporters, load)).toEqual(expected);
expect(Build.hasDockerExporter(exporters, load)).toEqual(expected);
});
});
@ -251,7 +251,7 @@ describe('hasAttestationType', () => {
['type=sbom,true', 'sbom', true],
['type=foo,bar', 'provenance', false],
])('given %p for %p returns %p', async (attrs: string, name: string, expected: boolean) => {
expect(Inputs.hasAttestationType(name, attrs)).toEqual(expected);
expect(Build.hasAttestationType(name, attrs)).toEqual(expected);
});
});
@ -275,7 +275,7 @@ describe('resolveAttestationAttrs', () => {
''
],
])('given %p', async (input: string, expected: string) => {
expect(Inputs.resolveAttestationAttrs(input)).toEqual(expected);
expect(Build.resolveAttestationAttrs(input)).toEqual(expected);
});
});
@ -285,7 +285,7 @@ describe('hasGitAuthTokenSecret', () => {
[['A_SECRET=abcdef0123456789'], false],
[['GIT_AUTH_TOKEN=abcdefghijklmno=0123456789'], true],
])('given %p secret', async (kvp: Array<string>, expected: boolean) => {
expect(Inputs.hasGitAuthTokenSecret(kvp)).toBe(expected);
expect(Build.hasGitAuthTokenSecret(kvp)).toBe(expected);
});
});

View File

@ -16,7 +16,7 @@
import {Buildx} from './buildx';
import {Exec} from '../exec';
import {Inputs} from './inputs';
import {Build} from './build';
import {Util} from '../util';
import {ExecOptions} from '@actions/exec';
@ -119,15 +119,15 @@ export class Bake {
}
public static hasLocalExporter(def: BakeDefinition): boolean {
return Inputs.hasExporterType('local', Bake.exporters(def));
return Build.hasExporterType('local', Bake.exporters(def));
}
public static hasTarExporter(def: BakeDefinition): boolean {
return Inputs.hasExporterType('tar', Bake.exporters(def));
return Build.hasExporterType('tar', Bake.exporters(def));
}
public static hasDockerExporter(def: BakeDefinition, load?: boolean): boolean {
return load || Inputs.hasExporterType('docker', Bake.exporters(def));
return load || Build.hasExporterType('docker', Bake.exporters(def));
}
private static exporters(def: BakeDefinition): Array<string> {

View File

@ -23,19 +23,7 @@ import {Context} from '../context';
import {GitHub} from '../github';
import {Util} from '../util';
const parseKvp = (kvp: string): [string, string] => {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
const value = kvp.substring(delimiterIndex + 1);
if (key.length == 0 || value.length == 0) {
throw new Error(`${kvp} is not a valid secret`);
}
return [key, value];
};
export class Inputs {
export class Build {
public static getBuildImageIDFilePath(): string {
return path.join(Context.tmpDir(), 'iidfile');
}
@ -45,7 +33,7 @@ export class Inputs {
}
public static resolveBuildImageID(): string | undefined {
const iidFile = Inputs.getBuildImageIDFilePath();
const iidFile = Build.getBuildImageIDFilePath();
if (!fs.existsSync(iidFile)) {
return undefined;
}
@ -53,7 +41,7 @@ export class Inputs {
}
public static resolveBuildMetadata(): string | undefined {
const metadataFile = Inputs.getBuildMetadataFilePath();
const metadataFile = Build.getBuildMetadataFilePath();
if (!fs.existsSync(metadataFile)) {
return undefined;
}
@ -65,7 +53,7 @@ export class Inputs {
}
public static resolveDigest(): string | undefined {
const metadata = Inputs.resolveBuildMetadata();
const metadata = Build.resolveBuildMetadata();
if (metadata === undefined) {
return undefined;
}
@ -77,22 +65,22 @@ export class Inputs {
}
public static resolveBuildSecretString(kvp: string): string {
const [key, file] = Inputs.resolveBuildSecret(kvp, false);
const [key, file] = Build.resolveBuildSecret(kvp, false);
return `id=${key},src=${file}`;
}
public static resolveBuildSecretFile(kvp: string): string {
const [key, file] = Inputs.resolveBuildSecret(kvp, true);
const [key, file] = Build.resolveBuildSecret(kvp, true);
return `id=${key},src=${file}`;
}
public static resolveBuildSecretEnv(kvp: string): string {
const [key, value] = parseKvp(kvp);
const [key, value] = Build.parseSecretKvp(kvp);
return `id=${key},env=${value}`;
}
public static resolveBuildSecret(kvp: string, file: boolean): [string, string] {
const [key, _value] = parseKvp(kvp);
const [key, _value] = Build.parseSecretKvp(kvp);
let value = _value;
if (file) {
if (!fs.existsSync(value)) {
@ -115,7 +103,7 @@ export class Inputs {
return core.getBooleanInput(name) ? `builder-id=${GitHub.workflowRunURL}` : 'false';
} catch (err) {
// not a valid boolean, so we assume it's a string
return Inputs.resolveProvenanceAttrs(input);
return Build.resolveProvenanceAttrs(input);
}
}
@ -143,15 +131,15 @@ export class Inputs {
}
public static hasLocalExporter(exporters: string[]): boolean {
return Inputs.hasExporterType('local', exporters);
return Build.hasExporterType('local', exporters);
}
public static hasTarExporter(exporters: string[]): boolean {
return Inputs.hasExporterType('tar', exporters);
return Build.hasExporterType('tar', exporters);
}
public static hasDockerExporter(exporters: string[], load?: boolean): boolean {
return load || Inputs.hasExporterType('docker', exporters);
return load || Build.hasExporterType('docker', exporters);
}
public static hasExporterType(name: string, exporters: string[]): boolean {
@ -223,4 +211,14 @@ export class Inputs {
}
return false;
}
private static parseSecretKvp(kvp: string): [string, string] {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
const value = kvp.substring(delimiterIndex + 1);
if (key.length == 0 || value.length == 0) {
throw new Error(`${kvp} is not a valid secret`);
}
return [key, value];
}
}