Use built-in getExecOutput

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2021-06-22 11:09:26 +02:00
parent 4608add020
commit 39efbd2c12
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
5 changed files with 107 additions and 170 deletions

View File

@ -7,9 +7,14 @@ import * as exec from '@actions/exec';
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner'); process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');
test('loginStandard calls exec', async () => { test('loginStandard calls exec', async () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec'); const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
// don't let exec try to actually run the commands execSpy.mockImplementation(() =>
execSpy.mockImplementation(() => {}); Promise.resolve({
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
})
);
const username: string = 'dbowie'; const username: string = 'dbowie';
const password: string = 'groundcontrol'; const password: string = 'groundcontrol';
@ -20,30 +25,25 @@ test('loginStandard calls exec', async () => {
expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], { expect(execSpy).toHaveBeenCalledWith(`docker`, ['login', '--password-stdin', '--username', username, registry], {
input: Buffer.from(password), input: Buffer.from(password),
silent: true, silent: true,
ignoreReturnCode: true, ignoreReturnCode: true
listeners: expect.objectContaining({
stdout: expect.any(Function),
stderr: expect.any(Function)
})
}); });
}); });
test('logout calls exec', async () => { test('logout calls exec', async () => {
const execSpy: jest.SpyInstance = jest.spyOn(exec, 'exec'); const execSpy: jest.SpyInstance = jest.spyOn(exec, 'getExecOutput');
// don't let exec try to actually run the commands execSpy.mockImplementation(() =>
execSpy.mockImplementation(() => {}); Promise.resolve({
exitCode: expect.any(Number),
stdout: expect.any(Function),
stderr: expect.any(Function)
})
);
const registry: string = 'https://ghcr.io'; const registry: string = 'https://ghcr.io';
await logout(registry); await logout(registry);
expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], { expect(execSpy).toHaveBeenCalledWith(`docker`, ['logout', registry], {
silent: false, ignoreReturnCode: true
ignoreReturnCode: true,
input: Buffer.from(''),
listeners: expect.objectContaining({
stdout: expect.any(Function),
stderr: expect.any(Function)
})
}); });
}); });

115
dist/index.js generated vendored
View File

@ -3947,69 +3947,6 @@ module.exports = require("fs");
/***/ }), /***/ }),
/***/ 757:
/***/ (function(__unusedmodule, exports, __webpack_require__) {
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = void 0;
const actionsExec = __importStar(__webpack_require__(514));
exports.exec = (command, args = [], silent, stdin) => __awaiter(void 0, void 0, void 0, function* () {
let stdout = '';
let stderr = '';
const options = {
silent: silent,
ignoreReturnCode: true,
input: Buffer.from(stdin || '')
};
options.listeners = {
stdout: (data) => {
stdout += data.toString();
},
stderr: (data) => {
stderr += data.toString();
}
};
const returnCode = yield actionsExec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
});
//# sourceMappingURL=exec.js.map
/***/ }),
/***/ 758: /***/ 758:
/***/ (function(__unusedmodule, exports, __webpack_require__) { /***/ (function(__unusedmodule, exports, __webpack_require__) {
@ -4045,9 +3982,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.loginECR = exports.loginStandard = exports.logout = exports.login = void 0; exports.loginECR = exports.loginStandard = exports.logout = exports.login = void 0;
const core = __importStar(__webpack_require__(186));
const aws = __importStar(__webpack_require__(981)); const aws = __importStar(__webpack_require__(981));
const execm = __importStar(__webpack_require__(757)); const core = __importStar(__webpack_require__(186));
const exec = __importStar(__webpack_require__(514));
function login(registry, username, password) { function login(registry, username, password) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
if (yield aws.isECR(registry)) { if (yield aws.isECR(registry)) {
@ -4061,9 +3998,13 @@ function login(registry, username, password) {
exports.login = login; exports.login = login;
function logout(registry) { function logout(registry) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield execm.exec('docker', ['logout', registry], false).then(res => { yield exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', ['logout', registry], {
core.warning(res.stderr); ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
} }
}); });
}); });
@ -4083,9 +4024,15 @@ function loginStandard(registry, username, password) {
else { else {
core.info(`Logging into Docker Hub...`); core.info(`Logging into Docker Hub...`);
} }
yield execm.exec('docker', loginArgs, true, password).then(res => { yield exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', loginArgs, {
throw new Error(res.stderr); ignoreReturnCode: true,
silent: true,
input: Buffer.from(password)
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
} }
core.info(`Login Succeeded!`); core.info(`Login Succeeded!`);
}); });
@ -4110,9 +4057,14 @@ function loginECR(registry, username, password) {
const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region, accountIDs); const loginCmds = yield aws.getDockerLoginCmds(cliVersion, registry, region, accountIDs);
core.info(`Logging into ${registry}...`); core.info(`Logging into ${registry}...`);
loginCmds.forEach((loginCmd, index) => { loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => { exec
if (res.stderr != '' && !res.success) { .getExecOutput(loginCmd, [], {
throw new Error(res.stderr); ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
} }
if (loginCmds.length > 1) { if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`); core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
@ -5289,8 +5241,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getAccountIDs = exports.getRegion = exports.isPubECR = exports.isECR = void 0; exports.getDockerLoginCmds = exports.parseCLIVersion = exports.getCLIVersion = exports.execCLI = exports.getCLI = exports.getAccountIDs = exports.getRegion = exports.isPubECR = exports.isECR = void 0;
const semver = __importStar(__webpack_require__(383)); const semver = __importStar(__webpack_require__(383));
const exec = __importStar(__webpack_require__(514));
const io = __importStar(__webpack_require__(436)); const io = __importStar(__webpack_require__(436));
const execm = __importStar(__webpack_require__(757));
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/; const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
exports.isECR = (registry) => { exports.isECR = (registry) => {
return ecrRegistryRegex.test(registry) || exports.isPubECR(registry); return ecrRegistryRegex.test(registry) || exports.isPubECR(registry);
@ -5326,11 +5278,16 @@ exports.getCLI = () => __awaiter(void 0, void 0, void 0, function* () {
return io.which('aws', true); return io.which('aws', true);
}); });
exports.execCLI = (args) => __awaiter(void 0, void 0, void 0, function* () { exports.execCLI = (args) => __awaiter(void 0, void 0, void 0, function* () {
return execm.exec(yield exports.getCLI(), args, true).then(res => { return exec
if (res.stderr != '' && !res.success) { .getExecOutput(yield exports.getCLI(), args, {
throw new Error(res.stderr); ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
} }
else if (res.stderr != '') { else if (res.stderr.length > 0) {
return res.stderr.trim(); return res.stderr.trim();
} }
else { else {

View File

@ -1,6 +1,6 @@
import * as semver from 'semver'; import * as semver from 'semver';
import * as exec from '@actions/exec';
import * as io from '@actions/io'; import * as io from '@actions/io';
import * as execm from './exec';
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/; const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
@ -43,15 +43,20 @@ export const getCLI = async (): Promise<string> => {
}; };
export const execCLI = async (args: string[]): Promise<string> => { export const execCLI = async (args: string[]): Promise<string> => {
return execm.exec(await getCLI(), args, true).then(res => { return exec
if (res.stderr != '' && !res.success) { .getExecOutput(await getCLI(), args, {
throw new Error(res.stderr); ignoreReturnCode: true,
} else if (res.stderr != '') { silent: true
return res.stderr.trim(); })
} else { .then(res => {
return res.stdout.trim(); if (res.stderr.length > 0 && res.exitCode != 0) {
} throw new Error(res.stderr.trim());
}); } else if (res.stderr.length > 0) {
return res.stderr.trim();
} else {
return res.stdout.trim();
}
});
}; };
export const getCLIVersion = async (): Promise<string> => { export const getCLIVersion = async (): Promise<string> => {

View File

@ -1,6 +1,6 @@
import * as core from '@actions/core';
import * as aws from './aws'; import * as aws from './aws';
import * as execm from './exec'; import * as core from '@actions/core';
import * as exec from '@actions/exec';
export async function login(registry: string, username: string, password: string): Promise<void> { export async function login(registry: string, username: string, password: string): Promise<void> {
if (await aws.isECR(registry)) { if (await aws.isECR(registry)) {
@ -11,11 +11,15 @@ export async function login(registry: string, username: string, password: string
} }
export async function logout(registry: string): Promise<void> { export async function logout(registry: string): Promise<void> {
await execm.exec('docker', ['logout', registry], false).then(res => { await exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', ['logout', registry], {
core.warning(res.stderr); ignoreReturnCode: true
} })
}); .then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
});
} }
export async function loginStandard(registry: string, username: string, password: string): Promise<void> { export async function loginStandard(registry: string, username: string, password: string): Promise<void> {
@ -32,12 +36,18 @@ export async function loginStandard(registry: string, username: string, password
} else { } else {
core.info(`Logging into Docker Hub...`); core.info(`Logging into Docker Hub...`);
} }
await execm.exec('docker', loginArgs, true, password).then(res => { await exec
if (res.stderr != '' && !res.success) { .getExecOutput('docker', loginArgs, {
throw new Error(res.stderr); ignoreReturnCode: true,
} silent: true,
core.info(`Login Succeeded!`); input: Buffer.from(password)
}); })
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
core.info(`Login Succeeded!`);
});
} }
export async function loginECR(registry: string, username: string, password: string): Promise<void> { export async function loginECR(registry: string, username: string, password: string): Promise<void> {
@ -60,15 +70,20 @@ export async function loginECR(registry: string, username: string, password: str
core.info(`Logging into ${registry}...`); core.info(`Logging into ${registry}...`);
loginCmds.forEach((loginCmd, index) => { loginCmds.forEach((loginCmd, index) => {
execm.exec(loginCmd, [], true).then(res => { exec
if (res.stderr != '' && !res.success) { .getExecOutput(loginCmd, [], {
throw new Error(res.stderr); ignoreReturnCode: true,
} silent: true
if (loginCmds.length > 1) { })
core.info(`Login Succeeded! (${index}/${loginCmds.length})`); .then(res => {
} else { if (res.stderr.length > 0 && res.exitCode != 0) {
core.info('Login Succeeded!'); throw new Error(res.stderr.trim());
} }
}); if (loginCmds.length > 1) {
core.info(`Login Succeeded! (${index}/${loginCmds.length})`);
} else {
core.info('Login Succeeded!');
}
});
}); });
} }

View File

@ -1,40 +0,0 @@
import * as actionsExec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (
command: string,
args: string[] = [],
silent: boolean,
stdin?: string
): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true,
input: Buffer.from(stdin || '')
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await actionsExec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};