From 99827f5019e1edcaab0e518a390ee6114ba7d695 Mon Sep 17 00:00:00 2001 From: kaffetorsk <93540369+kaffetorsk@users.noreply.github.com> Date: Wed, 3 Aug 2022 10:17:00 +0200 Subject: [PATCH] Allow msg.start to append arguments --- utility/daemon/daemon.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/utility/daemon/daemon.js b/utility/daemon/daemon.js index bbee3332..928f8143 100644 --- a/utility/daemon/daemon.js +++ b/utility/daemon/daemon.js @@ -15,16 +15,21 @@ module.exports = function(RED) { this.closer = n.closer || "SIGKILL"; this.autorun = true; if (n.autorun === false) { this.autorun = false; } - if (this.args.match(/^\[.*\]$/)) { - try { this.args = JSON.parse(this.args); } - catch(e) { - node.warn(RED._("daemon.errors.badparams")) - } - } - else { this.args = this.args.match(/("[^"]*")|[^ ]+/g); } + this.args = parseArgs(this.args); var node = this; var lastmsg = {}; + function parseArgs(args) { + if (args.match(/^\[.*\]$/)) { + try { args = JSON.parse(args); } + catch(e) { + node.warn(RED._("daemon.errors.badparams")) + } + } + else { args = args.match(/("[^"]*")|[^ ]+/g); } + return args; + } + function inputlistener(msg) { if (msg != null) { if (msg.hasOwnProperty("kill") && node.running) { @@ -32,7 +37,11 @@ module.exports = function(RED) { node.child.kill(msg.kill.toUpperCase()); } 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 { if (!Buffer.isBuffer(msg.payload)) { @@ -48,15 +57,20 @@ module.exports = function(RED) { } } - function runit() { + function runit(appendArgs) { var line = ""; if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) { node.status({fill:"grey",shape:"ring",text:RED._("daemon.status.nocommand")}); return; } + let args = node.args; + if (appendArgs !== undefined && appendArgs.length > 0) { + args = args.concat(appendArgs); + } + try { - node.child = spawn(node.cmd, node.args); - node.debug(node.cmd+" "+JSON.stringify(node.args)); + node.child = spawn(node.cmd, args); + node.debug(node.cmd+" "+JSON.stringify(args)); node.status({fill:"green",shape:"dot",text:RED._("daemon.status.running")}); node.running = true;