shell: add explicit bash() function

This commit is contained in:
Girish Ramakrishnan
2024-10-16 10:25:07 +02:00
parent d66db8ca40
commit df5ba25010
10 changed files with 46 additions and 63 deletions

View File

@@ -13,7 +13,7 @@ function shell(tag) {
assert.strictEqual(typeof tag, 'string');
return {
exec: exec.bind(null, tag),
bash: bash.bind(null, tag),
spawn: spawn.bind(null, tag),
sudo: sudo.bind(null, tag),
promises: { sudo: util.promisify(sudo.bind(null, tag)) }
@@ -22,19 +22,16 @@ function shell(tag) {
const SUDO = '/usr/bin/sudo';
// default no shell, handles input, separate args, wait for process to finish
function spawn(tag, file, args, options) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof options, 'object'); // note: spawn() has no encoding option of it's own
debug(`${tag}: ${file} ${JSON.stringify(args)}`);
const spawnOptions = Object.assign({ shell: false }, options); // note: no encoding!
debug(`${tag}: ${file} ${args.join(' ').replace(/\n/g, '\\n')}`);
return new Promise((resolve, reject) => {
const cp = child_process.spawn(file, args, spawnOptions);
const cp = child_process.spawn(file, args, options);
const stdoutBuffers = [], stderrBuffers = [];
cp.stdout.on('data', (data) => stdoutBuffers.push(data));
@@ -71,39 +68,12 @@ function spawn(tag, file, args, options) {
});
}
// default encoding utf8, no shell, handles input, full command, wait for process to finish
async function exec(tag, cmd, options) {
async function bash(tag, script, options) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
assert.strictEqual(typeof script, 'string');
assert.strictEqual(typeof options, 'object');
if (!options.shell) {
cmd = cmd.replace(/\s+/g, ' '); // collapse spaces when not using shell. note: no more complexity like parsing quotes here!
const [file, ...args] = cmd.split(' ');
return await spawn(tag, file, args, options);
}
debug(`${tag} exec: ${cmd}`);
return new Promise((resolve, reject) => {
const cp = child_process.exec(cmd, options, function (error, stdout, stderr) {
if (!error) return resolve(stdout);
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`);
e.code = error.code;
e.signal = error.signal;
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);
reject(e);
});
// https://github.com/nodejs/node/issues/25231
if (options.input) {
cp.stdin.write(options.input);
cp.stdin.end();
}
});
return await spawn(tag, '/bin/bash', [ '-c', script ], options);
}
function sudo(tag, args, options, callback) {