buildkit: split module

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-02-01 11:47:01 +01:00
parent cc48ecede1
commit dea2294b93
5 changed files with 141 additions and 86 deletions

View File

@@ -14,16 +14,16 @@
* limitations under the License.
*/
import fs from 'fs';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as semver from 'semver';
import {Context} from './context';
import {Buildx} from './buildx/buildx';
import {Builder} from './buildx/builder';
import {Context} from '../context';
import {Buildx} from '../buildx/buildx';
import {Builder} from '../buildx/builder';
import {Config} from './config';
import {BuilderInfo} from './types/builder';
import {BuilderInfo} from '../types/builder';
export interface BuildKitOpts {
context: Context;
@@ -35,8 +35,11 @@ export class BuildKit {
private readonly buildx: Buildx;
private containerNamePrefix = 'buildx_buildkit_';
public readonly config: Config;
constructor(opts: BuildKitOpts) {
this.context = opts.context;
this.config = new Config(this.context);
this.buildx =
opts?.buildx ||
new Buildx({
@@ -112,26 +115,6 @@ export class BuildKit {
return true;
}
public generateConfigInline(s: string): string {
return this.generateConfig(s, false);
}
public generateConfigFile(s: string): string {
return this.generateConfig(s, true);
}
private generateConfig(s: string, file: boolean): string {
if (file) {
if (!fs.existsSync(s)) {
throw new Error(`config file ${s} not found`);
}
s = fs.readFileSync(s, {encoding: 'utf-8'});
}
const configFile = this.context.tmpName({tmpdir: this.context.tmpDir()});
fs.writeFileSync(configFile, s);
return configFile;
}
private async getBuilderInfo(name: string): Promise<BuilderInfo> {
const builder = new Builder({
context: this.context,

47
src/buildkit/config.ts Normal file
View File

@@ -0,0 +1,47 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs';
import {Context} from '../context';
export class Config {
private readonly context: Context;
constructor(context: Context) {
this.context = context;
}
public generateFromString(s: string): string {
return this.generate(s, false);
}
public generateFromFile(s: string): string {
return this.generate(s, true);
}
private generate(s: string, file: boolean): string {
if (file) {
if (!fs.existsSync(s)) {
throw new Error(`config file ${s} not found`);
}
s = fs.readFileSync(s, {encoding: 'utf-8'});
}
const configFile = this.context.tmpName({tmpdir: this.context.tmpDir()});
fs.writeFileSync(configFile, s);
return configFile;
}
}

View File

@@ -16,11 +16,11 @@
import {Context} from './context';
import {Buildx} from './buildx/buildx';
import {BuildKit} from './buildkit';
import {BuildKit} from './buildkit/buildkit';
import {GitHub} from './github';
export {Builder, BuilderOpts} from './buildx/builder';
export {BuildKit, BuildKitOpts} from './buildkit';
export {BuildKit, BuildKitOpts} from './buildkit/buildkit';
export {Buildx, BuildxOpts} from './buildx/buildx';
export {Context} from './context';
export {Docker} from './docker';