shell: rewrite exec to use execFile

this also renames execFile to execArgs
This commit is contained in:
Girish Ramakrishnan
2024-02-21 18:37:10 +01:00
parent 14c9260ab0
commit cfd5c0f82b
6 changed files with 33 additions and 49 deletions

View File

@@ -12,46 +12,15 @@ exports = module.exports = {
promises: {
exec: util.promisify(exec),
execFile: util.promisify(execFile),
execArgs: util.promisify(execArgs),
sudo: util.promisify(sudo)
}
};
const SUDO = '/usr/bin/sudo';
// default encoding utf8, shell, handles input, full command
function exec(tag, cmd, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
debug(`${tag} exec: ${cmd}`);
const execOptions = Object.assign({ encoding: 'utf8', shell: false }, options);
// https://github.com/nodejs/node/issues/25231
const cp = child_process.exec(cmd, execOptions, function (error, stdout, stderr) {
let e = null;
if (error) {
e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
e.stdout = stdout; // when promisified, this is the way to get stdout
e.stderr = stderr; // when promisified, this is the way to get stderr
debug(`${tag}: ${cmd} errored`, error);
}
callback(e, stdout);
});
if (options.input) {
cp.stdin.write(options.input);
cp.stdin.end();
}
}
// default encoding utf8, no shell, separate args
function execFile(tag, file, args, options, callback) {
function execArgs(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(Array.isArray(args));
@@ -82,6 +51,17 @@ function execFile(tag, file, args, options, callback) {
}
}
// default encoding utf8, shell, handles input, full command
function exec(tag, cmd, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
const [file, ...args] = cmd.split(' ');
execArgs(tag, file, args, options, callback);
}
// use this when you are afraid of how arguments will split up
function spawn(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');