logs: use child_process.spawn directly for sudo

shell.sudo will move to a promise based API shortly
This commit is contained in:
Girish Ramakrishnan
2025-07-16 19:04:23 +02:00
parent 80edfffc3f
commit e4ceedcac6
+12 -6
View File
@@ -1,9 +1,9 @@
'use strict';
const assert = require('assert'),
child_process = require('child_process'),
debug = require('debug')('box:logs'),
path = require('path'),
shell = require('./shell.js')('logs'),
spawn = require('child_process').spawn,
stream = require('stream'),
{ StringDecoder } = require('string_decoder'),
TransformStream = stream.Transform;
@@ -64,14 +64,20 @@ function tail(filePaths, options) {
assert.strictEqual(typeof options, 'object');
const lines = options.lines === -1 ? '+1' : options.lines;
const args = options.sudo ? [ LOGTAIL_CMD ] : [];
args.push(`--lines=${lines}`);
const args = [ `--lines=${lines}` ];
if (options.follow) args.push('--follow');
if (options.sudo) {
return shell.sudo(args.concat(filePaths), { quiet: true }, () => {});
const cp = child_process.spawn('/usr/bin/sudo', [ '-S', LOGTAIL_CMD, ...args, ...filePaths ]);
cp.terminate = () => { // see note in shell.js
child_process.spawn('kill', ['-SIGKILL', -cp.pid], { detached: true }, (error) => {
if (error) debug(`tail could not terminate`, error);
});
};
cp.stdin.end();
return cp;
} else {
const cp = spawn('/usr/bin/tail', args.concat(filePaths));
const cp = child_process.spawn('/usr/bin/tail', args.concat(filePaths));
cp.terminate = () => cp.kill('SIGKILL');
return cp;
}