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

add don't add payload to exec node

to close #578
This commit is contained in:
dceejay 2015-03-11 17:43:42 +00:00
parent f6203fe60a
commit 1f8c6f87c9
2 changed files with 26 additions and 9 deletions

View File

@ -1,5 +1,5 @@
<!-- <!--
Copyright 2013 IBM Corp. Copyright 2013,2015 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -20,8 +20,13 @@
<input type="text" id="node-input-command" placeholder="command"> <input type="text" id="node-input-command" placeholder="command">
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-append"><i class="fa fa-list"></i> Append</label> <label><i class="fa fa-plus"></i> Append</label>
<input type="text" id="node-input-append" placeholder="extra input"> <input type="checkbox" id="node-input-addpay" style="display: inline-block; width: auto; vertical-align: top;">
&nbsp;msg.payload
</div>
<div class="form-row">
<label for="node-input-append"> </label>
<input type="text" id="node-input-append" placeholder="extra input parameters">
</div> </div>
<div class="form-row"> <div class="form-row">
<label>&nbsp;</label> <label>&nbsp;</label>
@ -32,7 +37,7 @@
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">Tip: <i>spawn</i> expects only one command word - and appended args to be comma separated.</div> <div class="form-tips" id="spawnTip">Tip: <i>spawn</i> expects only one command word - and appended args to be comma separated.</div>
</script> </script>
<script type="text/x-red" data-help-name="exec"> <script type="text/x-red" data-help-name="exec">
@ -40,7 +45,7 @@
<p>Provides 3 outputs... stdout, stderr, and return code.</p> <p>Provides 3 outputs... stdout, stderr, and return code.</p>
<p>By default uses exec() which calls the command, blocks while waiting for completion, and then returns the complete result in one go, along with any errors.</p> <p>By default uses exec() which calls the command, blocks while waiting for completion, and then returns the complete result in one go, along with any errors.</p>
<p>Optionally can use spawn() instead, which returns output from stdout and stderr as the command runs (ie one line at a time). On completion it then returns a return code (on the 3rd output).</p> <p>Optionally can use spawn() instead, which returns output from stdout and stderr as the command runs (ie one line at a time). On completion it then returns a return code (on the 3rd output).</p>
<p>Spawn only expect one command word, with all extra parameters to be comma separated and passed as the append.</p> <p>Spawn only expects one command word, with all extra parameters to be comma separated and passed as the append.</p>
<p>The optional append gets added to the command after the <b>msg.payload</b> - so you can do things like pipe the result to another command.</p> <p>The optional append gets added to the command after the <b>msg.payload</b> - so you can do things like pipe the result to another command.</p>
<p>If stdout is binary a <i>buffer</i> is returned - otherwise returns a <i>string</i>.</p> <p>If stdout is binary a <i>buffer</i> is returned - otherwise returns a <i>string</i>.</p>
</script> </script>
@ -51,6 +56,7 @@
color:"darksalmon", color:"darksalmon",
defaults: { defaults: {
command: {value:"",required:true}, command: {value:"",required:true},
addpay: {value:true},
append: {value:""}, append: {value:""},
useSpawn: {value:""}, useSpawn: {value:""},
name: {value:""} name: {value:""}
@ -64,6 +70,15 @@
}, },
labelStyle: function() { labelStyle: function() {
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
},
oneditprepare: function() {
$("#node-input-useSpawn").on("change",function() {
if ($("#node-input-useSpawn").is(':checked')) {
$("#spawnTip").show();
} else {
$("#spawnTip").hide();
}
});
} }
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2015 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ module.exports = function(RED) {
function ExecNode(n) { function ExecNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.cmd = n.command.trim(); this.cmd = n.command.trim();
this.addpay = n.addpay;
this.append = n.append.trim() || ""; this.append = n.append.trim() || "";
this.useSpawn = n.useSpawn; this.useSpawn = n.useSpawn;
@ -35,7 +36,7 @@ module.exports = function(RED) {
if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); } if (typeof(msg.payload !== "string")) { msg.payload = msg.payload.toString(); }
var arg = []; var arg = [];
if (node.append.length > 0) { arg = node.append.split(","); } if (node.append.length > 0) { arg = node.append.split(","); }
if (msg.payload.trim() !== "") { arg.unshift(msg.payload); } if ((node.addpay === true) && (msg.payload.trim() !== "")) { arg.unshift(msg.payload); }
if (RED.settings.verbose) { node.log(node.cmd+" ["+arg+"]"); } if (RED.settings.verbose) { node.log(node.cmd+" ["+arg+"]"); }
if (node.cmd.indexOf(" ") == -1) { if (node.cmd.indexOf(" ") == -1) {
var ex = spawn(node.cmd,arg); var ex = spawn(node.cmd,arg);
@ -64,7 +65,9 @@ module.exports = function(RED) {
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); } else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }
} }
else { else {
var cl = node.cmd+" "+msg.payload+" "+node.append; var cl = node.cmd;
if ((node.addpay === true) && (msg.payload.trim() !== "")) { cl += " "+msg.payload; }
if (node.append.trim() !== "") { cl += " "+node.append; }
if (RED.settings.verbose) { node.log(cl); } if (RED.settings.verbose) { node.log(cl); }
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) { var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
msg.payload = new Buffer(stdout,"binary"); msg.payload = new Buffer(stdout,"binary");
@ -83,6 +86,5 @@ module.exports = function(RED) {
} }
}); });
} }
RED.nodes.registerType("exec",ExecNode); RED.nodes.registerType("exec",ExecNode);
} }