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

Add Flow.getSetting for resolving env-var properties

This lays the groundwork for subflow-specific settings
This commit is contained in:
Nick O'Leary 2019-01-16 22:38:04 +00:00
parent 81f4e0de56
commit 6286b34d00
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
6 changed files with 38 additions and 15 deletions

View File

@ -283,6 +283,10 @@ class Flow {
return this.activeNodes; return this.activeNodes;
} }
getSetting(key) {
return this.parent.getSetting(key);
}
handleStatus(node,statusMessage) { handleStatus(node,statusMessage) {
events.emit("node-status",{ events.emit("node-status",{
id: node.id, id: node.id,

View File

@ -677,7 +677,8 @@ function removeFlow(id) {
const flowAPI = { const flowAPI = {
getNode: getNode, getNode: getNode,
handleError: () => false, handleError: () => false,
handleStatus: () => false handleStatus: () => false,
getSetting: k => process.env[k]
} }

View File

@ -41,22 +41,28 @@ function diffNodes(oldNode,newNode) {
var EnvVarPropertyRE_old = /^\$\((\S+)\)$/; var EnvVarPropertyRE_old = /^\$\((\S+)\)$/;
var EnvVarPropertyRE = /^\${(\S+)}$/; var EnvVarPropertyRE = /^\${(\S+)}$/;
function mapEnvVarProperties(obj,prop) { function mapEnvVarProperties(obj,prop,flow) {
if (Buffer.isBuffer(obj[prop])) { var v = obj[prop];
if (Buffer.isBuffer(v)) {
return; return;
} else if (Array.isArray(obj[prop])) { } else if (Array.isArray(v)) {
for (var i=0;i<obj[prop].length;i++) { for (var i=0;i<v.length;i++) {
mapEnvVarProperties(obj[prop],i); mapEnvVarProperties(v,i,flow);
} }
} else if (typeof obj[prop] === 'string') { } else if (typeof obj[prop] === 'string') {
if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(obj[prop]) || EnvVarPropertyRE.test(obj[prop])) ) { if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(v) || EnvVarPropertyRE.test(v)) ) {
var envVar = obj[prop].substring(2,obj[prop].length-1); var envVar = v.substring(2,v.length-1);
obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:obj[prop]; if (!flow) {
obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:v;
} else {
var r = flow.getSetting(envVar);
obj[prop] = r!==undefined?r:obj[prop];
}
} }
} else { } else {
for (var p in obj[prop]) { for (var p in v) {
if (obj[prop].hasOwnProperty(p)) { if (v.hasOwnProperty(p)) {
mapEnvVarProperties(obj[prop],p); mapEnvVarProperties(v,p,flow);
} }
} }
} }
@ -463,7 +469,7 @@ module.exports = {
delete conf.credentials; delete conf.credentials;
for (var p in conf) { for (var p in conf) {
if (conf.hasOwnProperty(p)) { if (conf.hasOwnProperty(p)) {
mapEnvVarProperties(conf,p); mapEnvVarProperties(conf,p,flow);
} }
} }
try { try {

View File

@ -104,7 +104,7 @@ function createNode(node,def) {
// allow $(foo) syntax to substitute env variables for credentials also... // allow $(foo) syntax to substitute env variables for credentials also...
for (var p in creds) { for (var p in creds) {
if (creds.hasOwnProperty(p)) { if (creds.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(creds,p); flowUtil.mapEnvVarProperties(creds,p,node._flow);
} }
} }
node.credentials = creds; node.credentials = creds;

View File

@ -300,7 +300,7 @@ describe('Flow', function() {
{id:"5",x:10,y:10,z:"t1",type:"test",foo:"$(NODE_RED_TEST_VALUE_NONE)",wires:[]}, {id:"5",x:10,y:10,z:"t1",type:"test",foo:"$(NODE_RED_TEST_VALUE_NONE)",wires:[]},
{id:"6",x:10,y:10,z:"t1",type:"test",foo:["$(NODE_RED_TEST_VALUE)"],wires:[]} {id:"6",x:10,y:10,z:"t1",type:"test",foo:["$(NODE_RED_TEST_VALUE)"],wires:[]}
]); ]);
var flow = Flow.create({},config,config.flows["t1"]); var flow = Flow.create({getSetting:v=>process.env[v]},config,config.flows["t1"]);
flow.start(); flow.start();
var activeNodes = flow.getActiveNodes(); var activeNodes = flow.getActiveNodes();

View File

@ -64,6 +64,18 @@ describe('flows/util', function() {
} }
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } ); foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
}); });
it('gets ENV from parent flow', function() {
var foo = {a:"$(unknown)",b:"$(foo2)",c:{d:"$(foo3)"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p,{
getSetting: name => name[0]==='f'?name.toUpperCase():undefined
});
}
}
foo.should.eql({ a: '$(unknown)', b: 'FOO2', c: { d: 'FOO3' } } );
});
}); });
describe('#diffNodes',function() { describe('#diffNodes',function() {