buildx: do not set version in constructor

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-01-30 20:08:54 +01:00
parent 5360816e7b
commit 32a260e6e5
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
2 changed files with 25 additions and 17 deletions

View File

@ -107,12 +107,12 @@ describe('printVersion', () => {
}); });
}); });
describe('getVersion', () => { describe('version', () => {
it('valid', async () => { it('valid', async () => {
const buildx = new Buildx({ const buildx = new Buildx({
context: new Context() context: new Context()
}); });
expect(semver.valid(await buildx.version)).not.toBeNull(); expect(semver.valid(await buildx.version)).not.toBeUndefined();
}); });
}); });

View File

@ -15,13 +15,13 @@ export interface BuildxOpts {
export class Buildx { export class Buildx {
private readonly context: Context; private readonly context: Context;
private _version: string | undefined;
public standalone: boolean; public standalone: boolean;
public version: Promise<string>;
constructor(opts: BuildxOpts) { constructor(opts: BuildxOpts) {
this.context = opts.context; this.context = opts.context;
this.standalone = opts?.standalone ?? !Docker.isAvailable(); this.standalone = opts?.standalone ?? !Docker.isAvailable();
this.version = this.getVersion();
} }
public getCommand(args: Array<string>) { public getCommand(args: Array<string>) {
@ -57,9 +57,11 @@ export class Buildx {
}); });
} }
private async getVersion(): Promise<string> { get version() {
return (async () => {
if (!this._version) {
const cmd = this.getCommand(['version']); const cmd = this.getCommand(['version']);
return await exec this._version = await exec
.getExecOutput(cmd.command, cmd.args, { .getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true, ignoreReturnCode: true,
silent: true silent: true
@ -71,6 +73,9 @@ export class Buildx {
return Buildx.parseVersion(res.stdout.trim()); return Buildx.parseVersion(res.stdout.trim());
}); });
} }
return this._version;
})();
}
public async printVersion() { public async printVersion() {
const cmd = this.getCommand(['version']); const cmd = this.getCommand(['version']);
@ -89,6 +94,9 @@ export class Buildx {
public async versionSatisfies(range: string, version?: string): Promise<boolean> { public async versionSatisfies(range: string, version?: string): Promise<boolean> {
const ver = version ?? (await this.version); const ver = version ?? (await this.version);
if (!ver) {
return false;
}
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null; return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
} }