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 * as rimraf from 'rimraf';
import {Context} from '../../src/context'; import {Context} from '../../src/context';
import {Buildx} from '../../src/buildx/buildx';
import {Inputs} from '../../src/buildx/inputs'; import {Inputs} from '../../src/buildx/inputs';
const fixturesDir = path.join(__dirname, '..', 'fixtures'); const fixturesDir = path.join(__dirname, '..', 'fixtures');
@ -53,31 +52,28 @@ afterEach(() => {
describe('resolveBuildImageID', () => { describe('resolveBuildImageID', () => {
it('matches', async () => { it('matches', async () => {
const buildx = new Buildx();
const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9'; const imageID = 'sha256:bfb45ab72e46908183546477a08f8867fc40cebadd00af54b071b097aed127a9';
const imageIDFile = buildx.inputs.getBuildImageIDFilePath(); const imageIDFile = Inputs.getBuildImageIDFilePath();
await fs.writeFileSync(imageIDFile, imageID); await fs.writeFileSync(imageIDFile, imageID);
const expected = buildx.inputs.resolveBuildImageID(); const expected = Inputs.resolveBuildImageID();
expect(expected).toEqual(imageID); expect(expected).toEqual(imageID);
}); });
}); });
describe('resolveBuildMetadata', () => { describe('resolveBuildMetadata', () => {
it('matches', async () => { it('matches', async () => {
const buildx = new Buildx(); const metadataFile = Inputs.getBuildMetadataFilePath();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata); await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveBuildMetadata(); const expected = Inputs.resolveBuildMetadata();
expect(expected).toEqual(metadata); expect(expected).toEqual(metadata);
}); });
}); });
describe('resolveDigest', () => { describe('resolveDigest', () => {
it('matches', async () => { it('matches', async () => {
const buildx = new Buildx(); const metadataFile = Inputs.getBuildMetadataFilePath();
const metadataFile = buildx.inputs.getBuildMetadataFilePath();
await fs.writeFileSync(metadataFile, metadata); await fs.writeFileSync(metadataFile, metadata);
const expected = buildx.inputs.resolveDigest(); const expected = Inputs.resolveDigest();
expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c'); expect(expected).toEqual('sha256:b09b9482c72371486bb2c1d2c2a2633ed1d0b8389e12c8d52b9e052725c0c83c');
}); });
}); });
@ -124,8 +120,7 @@ describe('getProvenanceInput', () => {
], ],
])('given input %p', async (input: string, expected: string) => { ])('given input %p', async (input: string, expected: string) => {
await setInput('provenance', input); await setInput('provenance', input);
const buildx = new Buildx(); expect(Inputs.getProvenanceInput('provenance')).toEqual(expected);
expect(buildx.inputs.getProvenanceInput('provenance')).toEqual(expected);
}); });
}); });
@ -153,8 +148,7 @@ describe('resolveProvenanceAttrs', () => {
'builder-id=https://github.com/docker/actions-toolkit/actions/runs/123' 'builder-id=https://github.com/docker/actions-toolkit/actions/runs/123'
], ],
])('given %p', async (input: string, expected: string) => { ])('given %p', async (input: string, expected: string) => {
const buildx = new Buildx(); expect(Inputs.resolveProvenanceAttrs(input)).toEqual(expected);
expect(buildx.inputs.resolveProvenanceAttrs(input)).toEqual(expected);
}); });
}); });
@ -170,12 +164,11 @@ describe('resolveBuildSecret', () => {
[`notfound=secret`, true, '', '', new Error('secret file secret not found')] [`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) => { ])('given %p key and %p secret', async (kvp: string, file: boolean, exKey: string, exValue: string, error: Error) => {
try { try {
const buildx = new Buildx();
let secret: string; let secret: string;
if (file) { if (file) {
secret = buildx.inputs.resolveBuildSecretFile(kvp); secret = Inputs.resolveBuildSecretFile(kvp);
} else { } else {
secret = buildx.inputs.resolveBuildSecretString(kvp); secret = Inputs.resolveBuildSecretString(kvp);
} }
expect(secret).toEqual(`id=${exKey},src=${tmpName}`); expect(secret).toEqual(`id=${exKey},src=${tmpName}`);
expect(fs.readFileSync(tmpName, 'utf-8')).toEqual(exValue); 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 {Docker} from '../docker/docker';
import {Exec} from '../exec'; import {Exec} from '../exec';
import {Inputs} from './inputs';
import {Cert} from '../types/buildx'; import {Cert} from '../types/buildx';
@ -34,15 +33,12 @@ export class Buildx {
private _versionOnce: boolean; private _versionOnce: boolean;
private readonly _standalone: boolean | undefined; private readonly _standalone: boolean | undefined;
public readonly inputs: Inputs;
public static readonly containerNamePrefix = 'buildx_buildkit_'; public static readonly containerNamePrefix = 'buildx_buildkit_';
constructor(opts?: BuildxOpts) { constructor(opts?: BuildxOpts) {
this._standalone = opts?.standalone; this._standalone = opts?.standalone;
this._version = ''; this._version = '';
this._versionOnce = false; this._versionOnce = false;
this.inputs = new Inputs();
} }
static get configDir(): string { static get configDir(): string {

View File

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