the-shepherd
Version:
Control a herd of wild processes.
134 lines (125 loc) • 3.76 kB
JavaScript
// Generated by CoffeeScript 2.5.1
(function() {
var $, Groups, Output, echo, echoResponse, exists, required, saveConfig, trueFalse, verbose, warn;
({$, echo, warn, verbose, required, echoResponse} = require('../common'));
({saveConfig} = require('../util/config'));
({trueFalse} = require("../util/format"));
({Groups} = require("../daemon/groups"));
({exists} = require("../files"));
Output = require('../daemon/output');
Object.assign(module.exports, {
options: [["--list", "List the current output file."], ["--file <file>", "Send output to this log file."], ["--disable", "Stop logging to file."], ["--clear", "Clear the log file."], ["--purge", "(alias for clear)"], ["--tail", "Pipe the log output to your console now."]],
toMessage: function(cmd) {
var ref;
return {
c: 'log',
l: trueFalse(cmd.list),
d: cmd.disable,
f: cmd.file,
t: trueFalse(cmd.tail),
p: (ref = cmd.clear) != null ? ref : cmd.purge
};
},
onMessage: function(msg, client, cb) {
var detach, handler, outputFile, ret, send;
ret = false;
send = (obj) => {
var err;
try {
return client != null ? client.write($.TNET.stringify([msg, obj])) : void 0;
} catch (error) {
err = error;
return typeof cb === "function" ? cb(err, false) : void 0;
}
};
if (msg.f) {
Output.setOutput(msg.f, (err, acted) => {
return acted && send(msg.f);
});
}
if (msg.d) {
Output.setOutput(null, (err, acted) => {
return acted && send(msg.d);
});
}
if (msg.p) {
outputFile = Output.getOutputFile();
if (exists(outputFile)) {
ChildProcess.spawn(`echo Log purged at \`date\`> ${expandPath(outputFile)}`, {
shell: true
}).on('exit', () => {
return send(msg.p);
});
}
}
if (client != null) {
if (msg.l) {
send(Output.getOutputFile());
}
if (msg.t) {
send(null); // signal the other side to init the stream
handler = (data) => {
var err, ref;
try {
return client.write($.TNET.stringify([
{
t: true
},
String(data)
]));
} catch (error) {
err = error;
echo("tail socket error:", (ref = err.stack) != null ? ref : err);
return detach();
}
};
detach = () => {
try {
return Output.stream.removeListener('tail', handler);
} catch (error) {}
};
client.on('close', detach);
client.on('error', detach);
Output.stream.on('tail', handler);
}
}
ret = msg.f || msg.d;
if (ret) {
if (typeof saveConfig === "function") {
saveConfig();
}
}
if (typeof cb === "function") {
cb(null, ret);
}
return ret;
},
onResponse: function(item, socket) {
var file, i, len, msg, resp;
[msg, resp] = item;
if (msg.f) {
echo("Log file set:", msg.f);
}
if (msg.d) {
echo("Log file disabled.");
}
if (msg.l) {
echo('Output files:');
for (i = 0, len = resp.length; i < len; i++) {
file = resp[i];
echo(file);
}
}
if (msg.t) {
if (resp) {
process.stdout.write(resp);
} else {
echo("Connecting to log tail...");
}
} else {
socket.end();
}
return false;
}
});
}).call(this);