@docker/actions-toolkit
Version:
Toolkit for Docker (GitHub) Actions
102 lines • 3.61 kB
JavaScript
/**
* Copyright 2025 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 * as core from '@actions/core';
import * as semver from 'semver';
import { Exec } from '../exec.js';
export class Regctl {
binPath;
_version;
_versionOnce;
constructor(opts) {
this.binPath = opts?.binPath || 'regctl';
this._version = '';
this._versionOnce = false;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async blobGet(opts) {
return await Exec.getExecOutput(this.binPath, ['blob', 'get', opts.repository, opts.digest], {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return res.stdout;
});
}
async manifestGet(opts) {
return await Exec.getExecOutput(this.binPath, ['manifest', 'get', opts.image, `--platform=${opts.platform ?? 'local'}`, `--format={{json .}}`], {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return JSON.parse(res.stdout.trim());
});
}
async isAvailable() {
const ok = await Exec.getExecOutput(this.binPath, [], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.debug(`Regctl.isAvailable cmd err: ${res.stderr.trim()}`);
return false;
}
return res.exitCode == 0;
})
.catch(error => {
core.debug(`Regctl.isAvailable error: ${error}`);
return false;
});
core.debug(`Regctl.isAvailable: ${ok}`);
return ok;
}
async version() {
if (this._versionOnce) {
return this._version;
}
this._versionOnce = true;
this._version = await Exec.getExecOutput(this.binPath, ['version', '--format', '{{.VCSTag}}'], {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return res.stdout.trim();
});
return this._version;
}
async printVersion() {
await Exec.exec(this.binPath, ['version'], {
failOnStdErr: false
});
}
async versionSatisfies(range, version) {
const ver = version ?? (await this.version());
if (!ver) {
core.debug(`Regctl.versionSatisfies false: undefined version`);
return false;
}
const res = semver.satisfies(ver, range) || /^[0-9a-f]{7}$/.exec(ver) !== null;
core.debug(`Regctl.versionSatisfies ${ver} statisfies ${range}: ${res}`);
return res;
}
}
//# sourceMappingURL=regctl.js.map