Use built-in getExecOutput (#102)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2021-08-10 09:00:29 +02:00 committed by GitHub
parent 93f53be329
commit dd220e93c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 180 additions and 205 deletions

137
dist/index.js generated vendored
View file

@ -64,68 +64,6 @@ exports.setOutput = setOutput;
/***/ }),
/***/ 757:
/***/ (function(__unused_webpack_module, 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.prototype.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) => __awaiter(void 0, void 0, void 0, function* () {
let stdout = '';
let stderr = '';
const options = {
silent: silent,
ignoreReturnCode: true
};
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
/***/ }),
/***/ 374:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
@ -161,10 +99,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.setConfig = void 0;
const exec = __importStar(__webpack_require__(757));
const exec = __importStar(__webpack_require__(514));
const git = (args = []) => __awaiter(void 0, void 0, void 0, function* () {
return yield exec.exec(`git`, args, true).then(res => {
if (res.stderr != '' && !res.success) {
return yield exec
.getExecOutput(`git`, args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
return res.stdout.trim();
@ -215,10 +158,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.killAgent = exports.deleteKey = exports.presetPassphrase = exports.configureAgent = exports.getKeygrips = exports.importKey = exports.getDirs = exports.getVersion = exports.agentConfig = void 0;
const exec = __importStar(__webpack_require__(514));
const fs = __importStar(__webpack_require__(747));
const path = __importStar(__webpack_require__(622));
const os = __importStar(__webpack_require__(87));
const exec = __importStar(__webpack_require__(757));
const openpgp = __importStar(__webpack_require__(666));
exports.agentConfig = `default-cache-ttl 7200
max-cache-ttl 31536000
@ -234,8 +177,13 @@ const getGnupgHome = () => __awaiter(void 0, void 0, void 0, function* () {
return homedir;
});
const gpgConnectAgent = (command) => __awaiter(void 0, void 0, void 0, function* () {
return yield exec.exec(`gpg-connect-agent "${command}" /bye`, [], true).then(res => {
if (res.stderr != '' && !res.success) {
return yield exec
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
@ -247,8 +195,13 @@ const gpgConnectAgent = (command) => __awaiter(void 0, void 0, void 0, function*
});
});
exports.getVersion = () => __awaiter(void 0, void 0, void 0, function* () {
return yield exec.exec('gpg', ['--version'], true).then(res => {
if (res.stderr != '') {
return yield exec
.getExecOutput('gpg', ['--version'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
let gnupgVersion = '';
@ -271,8 +224,13 @@ exports.getVersion = () => __awaiter(void 0, void 0, void 0, function* () {
});
});
exports.getDirs = () => __awaiter(void 0, void 0, void 0, function* () {
return yield exec.exec('gpgconf', ['--list-dirs'], true).then(res => {
if (res.stderr != '' && !res.success) {
return yield exec
.getExecOutput('gpgconf', ['--list-dirs'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
let libdir = '';
@ -306,9 +264,12 @@ exports.importKey = (key) => __awaiter(void 0, void 0, void 0, function* () {
const keyPath = `${keyFolder}/key.pgp`;
fs.writeFileSync(keyPath, (yield openpgp.isArmored(key)) ? key : Buffer.from(key, 'base64').toString(), { mode: 0o600 });
return yield exec
.exec('gpg', ['--import', '--batch', '--yes', keyPath], true)
.getExecOutput('gpg', ['--import', '--batch', '--yes', keyPath], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr != '' && !res.success) {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
if (res.stderr != '') {
@ -321,10 +282,12 @@ exports.importKey = (key) => __awaiter(void 0, void 0, void 0, function* () {
});
});
exports.getKeygrips = (fingerprint) => __awaiter(void 0, void 0, void 0, function* () {
return yield exec.exec('gpg', ['--batch', '--with-colons', '--with-keygrip', '--list-secret-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return yield exec
.getExecOutput('gpg', ['--batch', '--with-colons', '--with-keygrip', '--list-secret-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
let keygrips = [];
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('grp')) {
@ -348,13 +311,23 @@ exports.presetPassphrase = (keygrip, passphrase) => __awaiter(void 0, void 0, vo
return yield gpgConnectAgent(`KEYINFO ${keygrip}`);
});
exports.deleteKey = (fingerprint) => __awaiter(void 0, void 0, void 0, function* () {
yield exec.exec('gpg', ['--batch', '--yes', '--delete-secret-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
yield exec
.getExecOutput('gpg', ['--batch', '--yes', '--delete-secret-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
});
yield exec.exec('gpg', ['--batch', '--yes', '--delete-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
yield exec
.getExecOutput('gpg', ['--batch', '--yes', '--delete-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
});

View file

@ -1,5 +1,6 @@
module.exports = {
clearMocks: true,
restoreMocks: true,
coverageDirectory: 'coverage',
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',

View file

@ -1,34 +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): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true
};
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()
};
};

View file

@ -1,12 +1,17 @@
import * as exec from './exec';
import * as exec from '@actions/exec';
const git = async (args: string[] = []): Promise<string> => {
return await exec.exec(`git`, args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return res.stdout.trim();
});
return await exec
.getExecOutput(`git`, args, {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
return res.stdout.trim();
});
};
export async function setConfig(key: string, value: string): Promise<void> {

View file

@ -1,7 +1,7 @@
import * as exec from '@actions/exec';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as exec from './exec';
import * as openpgp from './openpgp';
export const agentConfig = `default-cache-ttl 7200
@ -32,75 +32,90 @@ const getGnupgHome = async (): Promise<string> => {
};
const gpgConnectAgent = async (command: string): Promise<string> => {
return await exec.exec(`gpg-connect-agent "${command}" /bye`, [], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('ERR')) {
throw new Error(line);
return await exec
.getExecOutput(`gpg-connect-agent "${command}" /bye`, [], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
}
return res.stdout.trim();
});
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('ERR')) {
throw new Error(line);
}
}
return res.stdout.trim();
});
};
export const getVersion = async (): Promise<Version> => {
return await exec.exec('gpg', ['--version'], true).then(res => {
if (res.stderr != '') {
throw new Error(res.stderr);
}
let gnupgVersion: string = '';
let libgcryptVersion: string = '';
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('gpg (GnuPG) ')) {
gnupgVersion = line.substr('gpg (GnuPG) '.length).trim();
} else if (line.startsWith('gpg (GnuPG/MacGPG2) ')) {
gnupgVersion = line.substr('gpg (GnuPG/MacGPG2) '.length).trim();
} else if (line.startsWith('libgcrypt ')) {
libgcryptVersion = line.substr('libgcrypt '.length).trim();
return await exec
.getExecOutput('gpg', ['--version'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
}
return {
gnupg: gnupgVersion,
libgcrypt: libgcryptVersion
};
});
let gnupgVersion: string = '';
let libgcryptVersion: string = '';
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('gpg (GnuPG) ')) {
gnupgVersion = line.substr('gpg (GnuPG) '.length).trim();
} else if (line.startsWith('gpg (GnuPG/MacGPG2) ')) {
gnupgVersion = line.substr('gpg (GnuPG/MacGPG2) '.length).trim();
} else if (line.startsWith('libgcrypt ')) {
libgcryptVersion = line.substr('libgcrypt '.length).trim();
}
}
return {
gnupg: gnupgVersion,
libgcrypt: libgcryptVersion
};
});
};
export const getDirs = async (): Promise<Dirs> => {
return await exec.exec('gpgconf', ['--list-dirs'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
let libdir: string = '';
let libexecdir: string = '';
let datadir: string = '';
let homedir: string = '';
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('libdir:')) {
libdir = line.substr('libdir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('libexecdir:')) {
libexecdir = line.substr('libexecdir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('datadir:')) {
datadir = line.substr('datadir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('homedir:')) {
homedir = line.substr('homedir:'.length).replace('%3a', ':').trim();
return await exec
.getExecOutput('gpgconf', ['--list-dirs'], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
}
return {
libdir: libdir,
libexecdir: libexecdir,
datadir: datadir,
homedir: homedir
};
});
let libdir: string = '';
let libexecdir: string = '';
let datadir: string = '';
let homedir: string = '';
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('libdir:')) {
libdir = line.substr('libdir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('libexecdir:')) {
libexecdir = line.substr('libexecdir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('datadir:')) {
datadir = line.substr('datadir:'.length).replace('%3a', ':').trim();
} else if (line.startsWith('homedir:')) {
homedir = line.substr('homedir:'.length).replace('%3a', ':').trim();
}
}
return {
libdir: libdir,
libexecdir: libexecdir,
datadir: datadir,
homedir: homedir
};
});
};
export const importKey = async (key: string): Promise<string> => {
@ -109,9 +124,12 @@ export const importKey = async (key: string): Promise<string> => {
fs.writeFileSync(keyPath, (await openpgp.isArmored(key)) ? key : Buffer.from(key, 'base64').toString(), {mode: 0o600});
return await exec
.exec('gpg', ['--import', '--batch', '--yes', keyPath], true)
.getExecOutput('gpg', ['--import', '--batch', '--yes', keyPath], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr != '' && !res.success) {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
if (res.stderr != '') {
@ -125,18 +143,20 @@ export const importKey = async (key: string): Promise<string> => {
};
export const getKeygrips = async (fingerprint: string): Promise<Array<string>> => {
return await exec.exec('gpg', ['--batch', '--with-colons', '--with-keygrip', '--list-secret-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
let keygrips: Array<string> = [];
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('grp')) {
keygrips.push(line.replace(/(grp|:)/g, '').trim());
return await exec
.getExecOutput('gpg', ['--batch', '--with-colons', '--with-keygrip', '--list-secret-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
let keygrips: Array<string> = [];
for (let line of res.stdout.replace(/\r/g, '').trim().split(/\n/g)) {
if (line.startsWith('grp')) {
keygrips.push(line.replace(/(grp|:)/g, '').trim());
}
}
}
return keygrips;
});
return keygrips;
});
};
export const configureAgent = async (config: string): Promise<void> => {
@ -154,16 +174,26 @@ export const presetPassphrase = async (keygrip: string, passphrase: string): Pro
};
export const deleteKey = async (fingerprint: string): Promise<void> => {
await exec.exec('gpg', ['--batch', '--yes', '--delete-secret-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
});
await exec.exec('gpg', ['--batch', '--yes', '--delete-keys', fingerprint], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
});
await exec
.getExecOutput('gpg', ['--batch', '--yes', '--delete-secret-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
});
await exec
.getExecOutput('gpg', ['--batch', '--yes', '--delete-keys', fingerprint], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
});
};
export const killAgent = async (): Promise<void> => {