@docker/actions-toolkit
Version:
Toolkit for Docker (GitHub) Actions
91 lines • 3.08 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 { Docker } from '../docker/docker.js';
import { Exec } from '../exec.js';
export class Compose {
_version;
_versionOnce;
_standalone;
constructor(opts) {
this._standalone = opts?.standalone;
this._version = '';
this._versionOnce = false;
}
async isStandalone() {
const standalone = this._standalone ?? !(await Docker.isAvailable());
core.debug(`Compose.isStandalone: ${standalone}`);
return standalone;
}
async getCommand(args) {
const standalone = await this.isStandalone();
return {
command: standalone ? 'compose' : 'docker',
args: standalone ? args : ['compose', ...args]
};
}
async isAvailable() {
const cmd = await this.getCommand([]);
const ok = await Exec.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.debug(`Compose.isAvailable cmd err: ${res.stderr.trim()}`);
return false;
}
return res.exitCode == 0;
})
.catch(error => {
core.debug(`Compose.isAvailable error: ${error}`);
return false;
});
core.debug(`Compose.isAvailable: ${ok}`);
return ok;
}
async version() {
if (this._versionOnce) {
return this._version;
}
this._versionOnce = true;
const cmd = await this.getCommand(['version']);
this._version = await Exec.getExecOutput(cmd.command, cmd.args, {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return Compose.parseVersion(res.stdout.trim());
});
return this._version;
}
async printVersion() {
const cmd = await this.getCommand(['version']);
await Exec.exec(cmd.command, cmd.args, {
failOnStdErr: false
});
}
static parseVersion(stdout) {
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse compose version`);
}
return matches[1];
}
}
//# sourceMappingURL=compose.js.map