2023-01-31 03:34:51 +01:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-01 16:47:01 +01:00
|
|
|
import os from 'os';
|
|
|
|
|
import path from 'path';
|
2023-02-18 04:36:14 +01:00
|
|
|
import * as core from '@actions/core';
|
2023-02-21 08:40:27 +01:00
|
|
|
import * as io from '@actions/io';
|
2023-02-20 09:01:01 +01:00
|
|
|
import {Exec} from './exec';
|
2023-01-17 11:53:57 +01:00
|
|
|
|
2023-01-23 10:07:14 +01:00
|
|
|
export class Docker {
|
2023-02-01 16:47:01 +01:00
|
|
|
static get configDir(): string {
|
|
|
|
|
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 02:37:43 +01:00
|
|
|
public static async isAvailable(): Promise<boolean> {
|
2023-02-21 08:40:27 +01:00
|
|
|
return await io
|
|
|
|
|
.which('docker', true)
|
2023-02-19 02:37:43 +01:00
|
|
|
.then(res => {
|
2023-02-21 08:40:27 +01:00
|
|
|
core.debug(`Docker.isAvailable ok: ${res}`);
|
|
|
|
|
return true;
|
2023-02-19 02:37:43 +01:00
|
|
|
})
|
|
|
|
|
.catch(error => {
|
|
|
|
|
core.debug(`Docker.isAvailable error: ${error}`);
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2023-01-23 10:07:14 +01:00
|
|
|
}
|
2023-01-23 10:39:14 +01:00
|
|
|
|
2023-02-25 16:40:35 +01:00
|
|
|
public static async context(name?: string): Promise<string> {
|
|
|
|
|
const args = ['context', 'inspect', '--format', '{{.Name}}'];
|
|
|
|
|
if (name) {
|
|
|
|
|
args.push(name);
|
|
|
|
|
}
|
|
|
|
|
return await Exec.getExecOutput(`docker`, args, {
|
2023-02-25 12:20:06 +01:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
|
silent: true
|
|
|
|
|
}).then(res => {
|
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
|
throw new Error(res.stderr);
|
|
|
|
|
}
|
|
|
|
|
return res.stdout.trim();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 02:47:49 +01:00
|
|
|
public static async printVersion(): Promise<void> {
|
2023-02-20 09:01:01 +01:00
|
|
|
await Exec.exec('docker', ['version']);
|
2023-01-24 00:06:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-19 02:47:49 +01:00
|
|
|
public static async printInfo(): Promise<void> {
|
2023-02-20 09:01:01 +01:00
|
|
|
await Exec.exec('docker', ['info']);
|
2023-01-23 10:39:14 +01:00
|
|
|
}
|
2023-01-17 11:53:57 +01:00
|
|
|
}
|