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:
parent
81f4e0de56
commit
6286b34d00
@ -283,6 +283,10 @@ class Flow {
|
||||
return this.activeNodes;
|
||||
}
|
||||
|
||||
getSetting(key) {
|
||||
return this.parent.getSetting(key);
|
||||
}
|
||||
|
||||
handleStatus(node,statusMessage) {
|
||||
events.emit("node-status",{
|
||||
id: node.id,
|
||||
|
@ -677,7 +677,8 @@ function removeFlow(id) {
|
||||
const flowAPI = {
|
||||
getNode: getNode,
|
||||
handleError: () => false,
|
||||
handleStatus: () => false
|
||||
handleStatus: () => false,
|
||||
getSetting: k => process.env[k]
|
||||
}
|
||||
|
||||
|
||||
|
@ -41,22 +41,28 @@ function diffNodes(oldNode,newNode) {
|
||||
var EnvVarPropertyRE_old = /^\$\((\S+)\)$/;
|
||||
var EnvVarPropertyRE = /^\${(\S+)}$/;
|
||||
|
||||
function mapEnvVarProperties(obj,prop) {
|
||||
if (Buffer.isBuffer(obj[prop])) {
|
||||
function mapEnvVarProperties(obj,prop,flow) {
|
||||
var v = obj[prop];
|
||||
if (Buffer.isBuffer(v)) {
|
||||
return;
|
||||
} else if (Array.isArray(obj[prop])) {
|
||||
for (var i=0;i<obj[prop].length;i++) {
|
||||
mapEnvVarProperties(obj[prop],i);
|
||||
} else if (Array.isArray(v)) {
|
||||
for (var i=0;i<v.length;i++) {
|
||||
mapEnvVarProperties(v,i,flow);
|
||||
}
|
||||
} else if (typeof obj[prop] === 'string') {
|
||||
if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(obj[prop]) || EnvVarPropertyRE.test(obj[prop])) ) {
|
||||
var envVar = obj[prop].substring(2,obj[prop].length-1);
|
||||
obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:obj[prop];
|
||||
if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(v) || EnvVarPropertyRE.test(v)) ) {
|
||||
var envVar = v.substring(2,v.length-1);
|
||||
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 {
|
||||
for (var p in obj[prop]) {
|
||||
if (obj[prop].hasOwnProperty(p)) {
|
||||
mapEnvVarProperties(obj[prop],p);
|
||||
for (var p in v) {
|
||||
if (v.hasOwnProperty(p)) {
|
||||
mapEnvVarProperties(v,p,flow);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -463,7 +469,7 @@ module.exports = {
|
||||
delete conf.credentials;
|
||||
for (var p in conf) {
|
||||
if (conf.hasOwnProperty(p)) {
|
||||
mapEnvVarProperties(conf,p);
|
||||
mapEnvVarProperties(conf,p,flow);
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
@ -104,7 +104,7 @@ function createNode(node,def) {
|
||||
// allow $(foo) syntax to substitute env variables for credentials also...
|
||||
for (var p in creds) {
|
||||
if (creds.hasOwnProperty(p)) {
|
||||
flowUtil.mapEnvVarProperties(creds,p);
|
||||
flowUtil.mapEnvVarProperties(creds,p,node._flow);
|
||||
}
|
||||
}
|
||||
node.credentials = creds;
|
||||
|
@ -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:"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();
|
||||
|
||||
var activeNodes = flow.getActiveNodes();
|
||||
|
@ -64,6 +64,18 @@ describe('flows/util', function() {
|
||||
}
|
||||
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() {
|
||||
|
Loading…
Reference in New Issue
Block a user