Allow msg.start to append arguments

This commit is contained in:
kaffetorsk 2022-08-03 10:17:00 +02:00 committed by GitHub
parent 5bb8b4d968
commit 99827f5019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,15 +15,20 @@ module.exports = function(RED) {
this.closer = n.closer || "SIGKILL"; this.closer = n.closer || "SIGKILL";
this.autorun = true; this.autorun = true;
if (n.autorun === false) { this.autorun = false; } if (n.autorun === false) { this.autorun = false; }
if (this.args.match(/^\[.*\]$/)) { this.args = parseArgs(this.args);
try { this.args = JSON.parse(this.args); } var node = this;
var lastmsg = {};
function parseArgs(args) {
if (args.match(/^\[.*\]$/)) {
try { args = JSON.parse(args); }
catch(e) { catch(e) {
node.warn(RED._("daemon.errors.badparams")) node.warn(RED._("daemon.errors.badparams"))
} }
} }
else { this.args = this.args.match(/("[^"]*")|[^ ]+/g); } else { args = args.match(/("[^"]*")|[^ ]+/g); }
var node = this; return args;
var lastmsg = {}; }
function inputlistener(msg) { function inputlistener(msg) {
if (msg != null) { if (msg != null) {
@ -32,7 +37,11 @@ module.exports = function(RED) {
node.child.kill(msg.kill.toUpperCase()); node.child.kill(msg.kill.toUpperCase());
} }
else if (msg.hasOwnProperty("start") && !node.running) { else if (msg.hasOwnProperty("start") && !node.running) {
runit(); let args = "";
if (msg.hasOwnProperty("args") && msg.args.length > 0) {
args = parseArgs(msg.args.trim());
}
runit(args);
} }
else { else {
if (!Buffer.isBuffer(msg.payload)) { if (!Buffer.isBuffer(msg.payload)) {
@ -48,15 +57,20 @@ module.exports = function(RED) {
} }
} }
function runit() { function runit(appendArgs) {
var line = ""; var line = "";
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) { if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")}); node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")});
return; return;
} }
let args = node.args;
if (appendArgs !== undefined && appendArgs.length > 0) {
args = args.concat(appendArgs);
}
try { try {
node.child = spawn(node.cmd, node.args); node.child = spawn(node.cmd, args);
node.debug(node.cmd+" "+JSON.stringify(node.args)); node.debug(node.cmd+" "+JSON.stringify(args));
node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")}); node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")});
node.running = true; node.running = true;