buildx: reduce calls to getVersion
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -169,8 +169,7 @@ describe('printVersion', () => {
|
|||||||
describe('getVersion', () => {
|
describe('getVersion', () => {
|
||||||
it('valid', async () => {
|
it('valid', async () => {
|
||||||
const buildx = new Buildx();
|
const buildx = new Buildx();
|
||||||
const version = await buildx.getVersion();
|
expect(semver.valid(await buildx.version())).not.toBeNull();
|
||||||
expect(semver.valid(version)).not.toBeNull();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -13,17 +13,17 @@ export interface BuildxOpts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class Buildx {
|
export class Buildx {
|
||||||
private standalone: boolean;
|
private _standalone: boolean;
|
||||||
private version: Promise<string>;
|
private _version: Promise<string>;
|
||||||
private tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
private _tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-actions-toolkit-')).split(path.sep).join(path.posix.sep);
|
||||||
|
|
||||||
constructor(opts?: BuildxOpts) {
|
constructor(opts?: BuildxOpts) {
|
||||||
this.standalone = opts?.standalone ?? !Docker.isAvailable();
|
this._standalone = opts?.standalone ?? !Docker.isAvailable();
|
||||||
this.version = this.getVersion();
|
this._version = this.getVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
public tmpDir() {
|
public tmpDir() {
|
||||||
return this.tmpdir;
|
return this._tmpdir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public tmpName(options?: tmp.TmpNameOptions): string {
|
public tmpName(options?: tmp.TmpNameOptions): string {
|
||||||
@@ -32,8 +32,8 @@ export class Buildx {
|
|||||||
|
|
||||||
public getCommand(args: Array<string>) {
|
public getCommand(args: Array<string>) {
|
||||||
return {
|
return {
|
||||||
command: this.standalone ? 'buildx' : 'docker',
|
command: this._standalone ? 'buildx' : 'docker',
|
||||||
args: this.standalone ? args : ['buildx', ...args]
|
args: this._standalone ? args : ['buildx', ...args]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ export class Buildx {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getVersion(): Promise<string> {
|
private async getVersion(): Promise<string> {
|
||||||
const cmd = this.getCommand(['version']);
|
const cmd = this.getCommand(['version']);
|
||||||
return await exec
|
return await exec
|
||||||
.getExecOutput(cmd.command, cmd.args, {
|
.getExecOutput(cmd.command, cmd.args, {
|
||||||
@@ -71,6 +71,10 @@ export class Buildx {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async version(): Promise<string> {
|
||||||
|
return this._version;
|
||||||
|
}
|
||||||
|
|
||||||
public async printVersion() {
|
public async printVersion() {
|
||||||
const cmd = this.getCommand(['version']);
|
const cmd = this.getCommand(['version']);
|
||||||
await exec.exec(cmd.command, cmd.args, {
|
await exec.exec(cmd.command, cmd.args, {
|
||||||
@@ -87,7 +91,7 @@ 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.getVersion());
|
const ver = version ?? (await this.version());
|
||||||
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
|
return semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user