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

Widen support for env var to use ${} or $() syntax

This commit is contained in:
Nick O'Leary 2018-05-21 15:19:50 +01:00
parent eb6d093e56
commit e13fed9fc6
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 23 additions and 8 deletions

View File

@ -37,7 +37,8 @@ function diffNodes(oldNode,newNode) {
return false; return false;
} }
var EnvVarPropertyRE = /^\$\((\S+)\)$/; var EnvVarPropertyRE_old = /^\$\((\S+)\)$/;
var EnvVarPropertyRE = /^\${(\S+)}$/;
function mapEnvVarProperties(obj,prop) { function mapEnvVarProperties(obj,prop) {
if (Buffer.isBuffer(obj[prop])) { if (Buffer.isBuffer(obj[prop])) {
@ -47,11 +48,9 @@ function mapEnvVarProperties(obj,prop) {
mapEnvVarProperties(obj[prop],i); mapEnvVarProperties(obj[prop],i);
} }
} else if (typeof obj[prop] === 'string') { } else if (typeof obj[prop] === 'string') {
var m; if (obj[prop][0] === "$" && (EnvVarPropertyRE_old.test(obj[prop]) || EnvVarPropertyRE.test(obj[prop])) ) {
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) { var envVar = obj[prop].substring(2,obj[prop].length-1);
if (process.env.hasOwnProperty(m[1])) { obj[prop] = process.env.hasOwnProperty(envVar)?process.env[envVar]:obj[prop];
obj[prop] = process.env[m[1]];
}
} }
} else { } else {
for (var p in obj[prop]) { for (var p in obj[prop]) {
@ -203,7 +202,7 @@ module.exports = {
var linkMap = {}; var linkMap = {};
var changedTabs = {}; var changedTabs = {};
// Look for tabs that have been removed // Look for tabs that have been removed
for (id in oldConfig.flows) { for (id in oldConfig.flows) {
if (oldConfig.flows.hasOwnProperty(id) && (!newConfig.flows.hasOwnProperty(id))) { if (oldConfig.flows.hasOwnProperty(id) && (!newConfig.flows.hasOwnProperty(id))) {

View File

@ -35,10 +35,17 @@ describe('flows/util', function() {
}); });
describe('#mapEnvVarProperties',function() { describe('#mapEnvVarProperties',function() {
it('handles ENV substitutions in an object', function() { before(function() {
process.env.foo1 = "bar1"; process.env.foo1 = "bar1";
process.env.foo2 = "bar2"; process.env.foo2 = "bar2";
process.env.foo3 = "bar3"; process.env.foo3 = "bar3";
})
after(function() {
delete process.env.foo1;
delete process.env.foo2;
delete process.env.foo3;
})
it('handles ENV substitutions in an object - $()', function() {
var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}}; var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};
for (var p in foo) { for (var p in foo) {
if (foo.hasOwnProperty(p)) { if (foo.hasOwnProperty(p)) {
@ -47,6 +54,15 @@ 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('handles ENV substitutions in an object - ${}', function() {
var foo = {a:"${foo1}",b:"${foo2}",c:{d:"${foo3}"}};
for (var p in foo) {
if (foo.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(foo,p);
}
}
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
});
}); });
describe('#diffNodes',function() { describe('#diffNodes',function() {