make Context static

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-20 07:19:57 +01:00
parent 580aee99c0
commit a0e8f0bf18
14 changed files with 80 additions and 145 deletions

View File

@@ -17,26 +17,18 @@
import * as exec from '@actions/exec';
import {Buildx} from './buildx';
import {Context} from '../context';
import {BuilderInfo, NodeInfo} from '../types/builder';
export interface BuilderOpts {
context: Context;
buildx?: Buildx;
}
export class Builder {
private readonly context: Context;
private readonly buildx: Buildx;
constructor(opts: BuilderOpts) {
this.context = opts.context;
this.buildx =
opts?.buildx ||
new Buildx({
context: this.context
});
constructor(opts?: BuilderOpts) {
this.buildx = opts?.buildx || new Buildx();
}
public async inspect(name: string): Promise<BuilderInfo> {

View File

@@ -21,13 +21,11 @@ import * as exec from '@actions/exec';
import * as semver from 'semver';
import {Docker} from '../docker';
import {Context} from '../context';
import {Inputs} from './inputs';
import {Cert} from '../types/buildx';
export interface BuildxOpts {
context: Context;
standalone?: boolean;
}
@@ -35,15 +33,13 @@ export class Buildx {
private _version: string | undefined;
private readonly _standalone: boolean | undefined;
private readonly context: Context;
public readonly inputs: Inputs;
public static readonly containerNamePrefix = 'buildx_buildkit_';
constructor(opts: BuildxOpts) {
constructor(opts?: BuildxOpts) {
this._standalone = opts?.standalone;
this.context = opts.context;
this.inputs = new Inputs(this.context);
this.inputs = new Inputs();
}
static get configDir(): string {

View File

@@ -22,18 +22,12 @@ import {parse} from 'csv-parse/sync';
import {Context} from '../context';
export class Inputs {
private readonly context: Context;
constructor(context: Context) {
this.context = context;
}
public getBuildImageIDFilePath(): string {
return path.join(this.context.tmpDir(), 'iidfile');
return path.join(Context.tmpDir(), 'iidfile');
}
public getBuildMetadataFilePath(): string {
return path.join(this.context.tmpDir(), 'metadata-file');
return path.join(Context.tmpDir(), 'metadata-file');
}
public resolveBuildImageID(): string | undefined {
@@ -89,7 +83,7 @@ export class Inputs {
}
value = fs.readFileSync(value, {encoding: 'utf-8'});
}
const secretFile = this.context.tmpName({tmpdir: this.context.tmpDir()});
const secretFile = Context.tmpName({tmpdir: Context.tmpDir()});
fs.writeFileSync(secretFile, value);
return `id=${key},src=${secretFile}`;
}
@@ -100,9 +94,8 @@ export class Inputs {
// if input is not set returns empty string
return input;
}
const builderID = this.context.provenanceBuilderID;
try {
return core.getBooleanInput(name) ? `builder-id=${builderID}` : 'false';
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);
@@ -111,7 +104,7 @@ export class Inputs {
public resolveProvenanceAttrs(input: string): string {
if (!input) {
return `builder-id=${this.context.provenanceBuilderID}`;
return `builder-id=${Context.provenanceBuilderID()}`;
}
// parse attributes from input
const fields = parse(input, {
@@ -129,7 +122,7 @@ export class Inputs {
}
}
// if not add builder-id attribute
return `${input},builder-id=${this.context.provenanceBuilderID}`;
return `${input},builder-id=${Context.provenanceBuilderID()}`;
}
public static hasLocalExporter(exporters: string[]): boolean {

View File

@@ -32,17 +32,13 @@ import {Git} from '../git';
import {GitHubRelease} from '../types/github';
export interface InstallOpts {
context?: Context;
standalone?: boolean;
}
export class Install {
private readonly _standalone: boolean | undefined;
private readonly context: Context;
constructor(opts?: InstallOpts) {
this.context = opts?.context || new Context();
this._standalone = opts?.standalone;
}
@@ -84,7 +80,7 @@ export class Install {
let toolPath: string;
toolPath = tc.find('buildx', vspec);
if (!toolPath) {
const outputDir = path.join(this.context.tmpDir(), 'build-cache');
const outputDir = path.join(Context.tmpDir(), 'build-cache');
const buildCmd = await this.buildCommand(gitContext, outputDir);
toolPath = await exec
.getExecOutput(buildCmd.command, buildCmd.args, {
@@ -103,7 +99,7 @@ export class Install {
public async installStandalone(toolPath: string, dest?: string): Promise<string> {
core.info('Standalone mode');
dest = dest || this.context.tmpDir();
dest = dest || Context.tmpDir();
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
const binDir = path.join(dest, 'bin');
if (!fs.existsSync(binDir)) {
@@ -143,8 +139,8 @@ export class Install {
}
private async buildCommand(gitContext: string, outputDir: string): Promise<{args: Array<string>; command: string}> {
const buildxStandaloneFound = await new Buildx({context: this.context, standalone: true}).isAvailable();
const buildxPluginFound = await new Buildx({context: this.context, standalone: false}).isAvailable();
const buildxStandaloneFound = await new Buildx({standalone: true}).isAvailable();
const buildxPluginFound = await new Buildx({standalone: false}).isAvailable();
let buildStandalone = false;
if ((await this.isStandalone()) && buildxStandaloneFound) {
@@ -164,7 +160,7 @@ export class Install {
}
//prettier-ignore
return await new Buildx({context: this.context, standalone: buildStandalone}).getCommand([
return await new Buildx({standalone: buildStandalone}).getCommand([
'build',
'--target', 'binaries',
'--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1',