buildx: make inputs methods static

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-03-12 23:40:54 +01:00
parent 8df8cee02f
commit 03977693c1
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 26 additions and 37 deletions

View File

@ -20,7 +20,6 @@ import * as path from 'path';
import * as rimraf from 'rimraf';
import {Context} from '../../src/context';
import {Buildx} from '../../src/buildx/buildx';
import {Inputs} from '../../src/buildx/inputs';
const fixturesDir = path.join(__dirname, '..', 'fixtures');
@ -53,31 +52,28 @@ afterEach(() => {
describe('resolveBuildImageID', () => {
it('matches', async () => {
const buildx = new Buildx();
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = buildx.inputs.getBuildImageIDFilePath();
const imageIDFile = Inputs.getBuildImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID);
const expected = buildx.inputs.resolveBuildImageID();
const expected = Inputs.resolveBuildImageID();
expect(expected).toEqual(imageID);
});
});
describe('resolveBuildMetadata', () => {
it('matches', async () => {
const buildx = new Buildx();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
const metadataFile = Inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveBuildMetadata();
const expected = Inputs.resolveBuildMetadata();
expect(expected).toEqual(metadata);
});
});
describe('resolveDigest', () => {
it('matches', async () => {
const buildx = new Buildx();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
const metadataFile = Inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveDigest();
const expected = Inputs.resolveDigest();
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
});
});
@ -124,8 +120,7 @@ describe('getProvenanceInput', () => {
],
])('given input %p', async (input: string, expected: string) => {
await setInput('provenance', input);
const buildx = new Buildx();
expect(buildx.inputs.getProvenanceInput('provenance')).toEqual(expected);
expect(Inputs.getProvenanceInput('provenance')).toEqual(expected);
});
});
@ -153,8 +148,7 @@ describe('resolveProvenanceAttrs', () => {
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/123'
],
])('given %p', async (input: string, expected: string) => {
const buildx = new Buildx();
expect(buildx.inputs.resolveProvenanceAttrs(input)).toEqual(expected);
expect(Inputs.resolveProvenanceAttrs(input)).toEqual(expected);
});
});
@ -170,12 +164,11 @@ describe('resolveBuildSecret', () => {
[`notfound=secret`, true, '', '', new Error('secret file secret not found')]
])('given %p key and %p secret', async (kvp: string, file: boolean, exKey: string, exValue: string, error: Error) => {
try {
const buildx = new Buildx();
let secret: string;
if (file) {
secret = buildx.inputs.resolveBuildSecretFile(kvp);
secret = Inputs.resolveBuildSecretFile(kvp);
} else {
secret = buildx.inputs.resolveBuildSecretString(kvp);
secret = Inputs.resolveBuildSecretString(kvp);
}
expect(secret).toEqual(`id=${exKey},src=${tmpName}`);
expect(fs.readFileSync(tmpName, 'utf-8')).toEqual(exValue);

View File

@ -21,7 +21,6 @@ import * as semver from 'semver';
import {Docker} from '../docker/docker';
import {Exec} from '../exec';
import {Inputs} from './inputs';
import {Cert} from '../types/buildx';
@ -34,15 +33,12 @@ export class Buildx {
private _versionOnce: boolean;
private readonly _standalone: boolean | undefined;
public readonly inputs: Inputs;
public static readonly containerNamePrefix = 'buildx_buildkit_';
constructor(opts?: BuildxOpts) {
this._standalone = opts?.standalone;
this._version = '';
this._versionOnce = false;
this.inputs = new Inputs();
}
static get configDir(): string {

View File

@ -22,24 +22,24 @@ import {parse} from 'csv-parse/sync';
import {Context} from '../context';
export class Inputs {
public getBuildImageIDFilePath(): string {
public static getBuildImageIDFilePath(): string {
return path.join(Context.tmpDir(), 'iidfile');
}
public getBuildMetadataFilePath(): string {
public static getBuildMetadataFilePath(): string {
return path.join(Context.tmpDir(), 'metadata-file');
}
public resolveBuildImageID(): string | undefined {
const iidFile = this.getBuildImageIDFilePath();
public static resolveBuildImageID(): string | undefined {
const iidFile = Inputs.getBuildImageIDFilePath();
if (!fs.existsSync(iidFile)) {
return undefined;
}
return fs.readFileSync(iidFile, {encoding: 'utf-8'}).trim();
}
public resolveBuildMetadata(): string | undefined {
const metadataFile = this.getBuildMetadataFilePath();
public static resolveBuildMetadata(): string | undefined {
const metadataFile = Inputs.getBuildMetadataFilePath();
if (!fs.existsSync(metadataFile)) {
return undefined;
}
@ -50,8 +50,8 @@ export class Inputs {
return content;
}
public resolveDigest(): string | undefined {
const metadata = this.resolveBuildMetadata();
public static resolveDigest(): string | undefined {
const metadata = Inputs.resolveBuildMetadata();
if (metadata === undefined) {
return undefined;
}
@ -62,15 +62,15 @@ export class Inputs {
return undefined;
}
public resolveBuildSecretString(kvp: string): string {
return this.resolveBuildSecret(kvp, false);
public static resolveBuildSecretString(kvp: string): string {
return Inputs.resolveBuildSecret(kvp, false);
}
public resolveBuildSecretFile(kvp: string): string {
return this.resolveBuildSecret(kvp, true);
public static resolveBuildSecretFile(kvp: string): string {
return Inputs.resolveBuildSecret(kvp, true);
}
public resolveBuildSecret(kvp: string, file: boolean): string {
public static resolveBuildSecret(kvp: string, file: boolean): string {
const delimiterIndex = kvp.indexOf('=');
const key = kvp.substring(0, delimiterIndex);
let value = kvp.substring(delimiterIndex + 1);
@ -88,7 +88,7 @@ export class Inputs {
return `id=${key},src=${secretFile}`;
}
public getProvenanceInput(name: string): string {
public static getProvenanceInput(name: string): string {
const input = core.getInput(name);
if (!input) {
// if input is not set returns empty string
@ -98,11 +98,11 @@ export class Inputs {
return core.getBooleanInput(name) ? `builder-id=${Context.provenanceBuilderID()}` : 'false';
} catch (err) {
// not a valid boolean, so we assume it's a string
return this.resolveProvenanceAttrs(input);
return Inputs.resolveProvenanceAttrs(input);
}
}
public resolveProvenanceAttrs(input: string): string {
public static resolveProvenanceAttrs(input: string): string {
if (!input) {
return `builder-id=${Context.provenanceBuilderID()}`;
}