1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Support typedInput in msg.payload field of exec node

This commit is contained in:
Kazuhito Yokoi 2021-03-22 16:19:55 +09:00
parent 55f1e7ece1
commit f103533852
3 changed files with 43 additions and 12 deletions

View File

@ -21,8 +21,10 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<label><i class="fa fa-plus"></i> <span data-i18n="exec.label.append"></span></label> <label><i class="fa fa-plus"></i> <span data-i18n="exec.label.append"></span></label>
<input type="checkbox" id="node-input-addpay" style="display:inline-block; width:auto; vertical-align:top;"> <input type="checkbox" id="node-input-addpay" style="display:inline-block; width:auto;">
&nbsp;msg.payload &nbsp;
<input type="text" id="node-input-addpayValue" style="width:160px;">
<input type="hidden" id="node-input-addpayValueType">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-append"> </label> <label for="node-input-append"> </label>
@ -53,6 +55,8 @@
defaults: { defaults: {
command: {value:""}, command: {value:""},
addpay: {value:false}, addpay: {value:false},
addpayValue: {value:"payload"},
addpayValueType: {value:"msg"},
append: {value:""}, append: {value:""},
useSpawn: {value:"false"}, useSpawn: {value:"false"},
timer: {value:""}, timer: {value:""},
@ -79,6 +83,14 @@
if ($("#node-input-useSpawn").val() === null) { if ($("#node-input-useSpawn").val() === null) {
$("#node-input-useSpawn").val(this.useSpawn.toString()); $("#node-input-useSpawn").val(this.useSpawn.toString());
} }
if ($("#node-input-addpayValue").val() === "") {
$("#node-input-addpayValue").val("payload");
}
$("#node-input-addpayValue").typedInput({
default: "msg",
typeField: $("#node-input-addpayValueType"),
types: ["msg"]
});
} }
}); });
</script> </script>

View File

@ -26,6 +26,8 @@ module.exports = function(RED) {
this.cmd = (n.command || "").trim(); this.cmd = (n.command || "").trim();
if (n.addpay === undefined) { n.addpay = true; } if (n.addpay === undefined) { n.addpay = true; }
this.addpay = n.addpay; this.addpay = n.addpay;
this.addpayValue = n.addpayValue || "payload";
this.addpayValueType = n.addpayValueType || "msg";
this.append = (n.append || "").trim(); this.append = (n.append || "").trim();
this.useSpawn = (n.useSpawn == "true"); this.useSpawn = (n.useSpawn == "true");
this.timer = Number(n.timer || 0)*1000; this.timer = Number(n.timer || 0)*1000;
@ -61,12 +63,16 @@ module.exports = function(RED) {
} }
else { else {
var child; var child;
if (this.useSpawn === true) {
// make the extra args into an array // make the extra args into an array
// then prepend with the msg.payload // then prepend with the msg.payload
var arg = node.cmd; var arg = node.cmd;
if ((node.addpay === true) && msg.hasOwnProperty("payload")) { arg += " "+msg.payload; } if (node.addpay) {
if (node.append.trim() !== "") { arg += " "+node.append; } if (node.addpayValueType === "msg" && msg.hasOwnProperty(node.addpayValue)) {
arg += " " + RED.util.getMessageProperty(msg, node.addpayValue);
}
}
if (node.append.trim() !== "") { arg += " " + node.append; }
if (this.useSpawn === true) {
// slice whole line by spaces and removes any quotes since spawn can't handle them // slice whole line by spaces and removes any quotes since spawn can't handle them
arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g).map((a) => { arg = arg.match(/(?:[^\s"]+|"[^"]*")+/g).map((a) => {
if (/^".*"$/.test(a)) { if (/^".*"$/.test(a)) {
@ -126,12 +132,9 @@ module.exports = function(RED) {
}); });
} }
else { else {
var cl = node.cmd;
if ((node.addpay === true) && msg.hasOwnProperty("payload")) { cl += " "+msg.payload; }
if (node.append.trim() !== "") { cl += " "+node.append; }
/* istanbul ignore else */ /* istanbul ignore else */
if (RED.settings.verbose) { node.log(cl); } if (RED.settings.verbose) { node.log(arg); }
child = exec(cl, node.execOpt, function (error, stdout, stderr) { child = exec(arg, node.execOpt, function (error, stdout, stderr) {
var msg2, msg3; var msg2, msg3;
delete msg.payload; delete msg.payload;
if (stderr) { if (stderr) {

View File

@ -114,6 +114,22 @@ describe('exec node', function() {
}); });
}); });
it('should exec a simple command with appended value from message', function (done) {
var flow = [{id:"n1", type:"exec", wires:[["n2"]], command:"echo", addpay:true, addpayValue:"topic", addpayValueType:"msg", append:"more", oldrc:"false"},
{id:"n2", type:"helper"}];
helper.load(execNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function (msg) {
msg.should.have.property("payload");
msg.payload.should.be.a.String();
msg.payload.should.equal("bar more\n");
done();
});
n1.receive({payload:"foo", topic:"bar"});
});
});
it('should exec a simple command with extra parameters', function(done) { it('should exec a simple command with extra parameters', function(done) {
var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more", oldrc:"false"}, var flow = [{id:"n1",type:"exec",wires:[["n2"],["n3"],["n4"]],command:"echo", addpay:true, append:"more", oldrc:"false"},
{id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}]; {id:"n2", type:"helper"},{id:"n3", type:"helper"},{id:"n4", type:"helper"}];