sudo: add explicit captureStdout flag

This commit is contained in:
Girish Ramakrishnan
2024-07-08 09:58:25 +02:00
parent 7d8ba8d42c
commit 60c4dd3875
3 changed files with 9 additions and 7 deletions

View File

@@ -91,11 +91,13 @@ function sudo(tag, args, options, callback) {
const sudoArgs = [ '-S' ]; // -S makes sudo read stdin for password
if (options.preserveEnv) sudoArgs.push('-E'); // -E preserves environment
if (options.ipc) sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
callback = once(callback); // exit may or may not be called after an 'error'
if (options.ipc) options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
if (options.ipc) {
sudoArgs.push('--close-from=4'); // keep the ipc open. requires closefrom_override in sudoers file
options.stdio = ['pipe', 'pipe', 'pipe', 'ipc'];
}
const spawnArgs = sudoArgs.concat(args);
@@ -103,10 +105,10 @@ function sudo(tag, args, options, callback) {
const cp = child_process.spawn(SUDO, spawnArgs, options);
let stdoutResult = '';
if (!options.streamStdout) cp.stdout.on('data', (data) => stdoutResult += data.toString('utf8'));
if (options.captureStdout) cp.stdout.on('data', (data) => stdoutResult += data.toString('utf8'));
cp.on('exit', function (code, signal) {
if (code === 0) return callback(null, options.streamStdout ? null : stdoutResult);
if (code === 0) return callback(null, options.captureStdout ? stdoutResult : null);
const e = new BoxError(BoxError.SHELL_ERROR, `${tag} exited with code ${code} signal ${signal}`);
e.code = code;
e.signal = signal;