reimplement $(env var) replace to share common code.

and add test to utils
This commit is contained in:
Dave Conway-Jones 2016-11-17 13:56:17 +00:00
parent 74f2180fa4
commit 8d5286703f
4 changed files with 53 additions and 48 deletions

View File

@ -259,32 +259,6 @@ function Flow(global,flow) {
}
}
}
}
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
function mapEnvVarProperties(obj,prop) {
if (Buffer.isBuffer(obj[prop])) {
return;
} else if (Array.isArray(obj[prop])) {
for (var i=0;i<obj[prop].length;i++) {
mapEnvVarProperties(obj[prop],i);
}
} else if (typeof obj[prop] === 'string') {
var m;
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
if (process.env.hasOwnProperty(m[1])) {
obj[prop] = process.env[m[1]];
}
}
} else {
for (var p in obj[prop]) {
if (obj[prop].hasOwnProperty(p)) {
mapEnvVarProperties(obj[prop],p);
}
}
}
}
function createNode(type,config) {
@ -295,7 +269,7 @@ function createNode(type,config) {
delete conf.credentials;
for (var p in conf) {
if (conf.hasOwnProperty(p)) {
mapEnvVarProperties(conf,p);
flowUtil.mapEnvVarProperties(conf,p);
}
}
try {

View File

@ -37,9 +37,35 @@ function diffNodes(oldNode,newNode) {
return false;
}
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
function mapEnvVarProperties(obj,prop) {
if (Buffer.isBuffer(obj[prop])) {
return;
} else if (Array.isArray(obj[prop])) {
for (var i=0;i<obj[prop].length;i++) {
mapEnvVarProperties(obj[prop],i);
}
} else if (typeof obj[prop] === 'string') {
var m;
if ( (m = EnvVarPropertyRE.exec(obj[prop])) !== null) {
if (process.env.hasOwnProperty(m[1])) {
obj[prop] = process.env[m[1]];
}
}
} else {
for (var p in obj[prop]) {
if (obj[prop].hasOwnProperty(p)) {
mapEnvVarProperties(obj[prop],p);
}
}
}
}
module.exports = {
diffNodes: diffNodes,
mapEnvVarProperties: mapEnvVarProperties,
parseConfig: function(config) {
var flow = {};

View File

@ -21,6 +21,7 @@ var fs = require("fs");
var registry = require("./registry");
var credentials = require("./credentials");
var flows = require("./flows");
var flowUtil = require("./flows/util")
var context = require("./context");
var Node = require("./Node");
var log = require("../log");
@ -70,19 +71,11 @@ function createNode(node,def) {
if (creds) {
//console.log("Attaching credentials to ",node.id);
// allow $(foo) syntax to substitute env variables for credentials also...
var EnvVarPropertyRE = /^\$\((\S+)\)$/;
var loopOver = function (obj) {
for (var o in obj) {
if (typeof obj[o] === "object" && obj[o] !== null) { loopOver(obj[o]); }
else {
var m;
if ( (m = EnvVarPropertyRE.exec(obj[o])) !== null ) {
if (process.env.hasOwnProperty(m[1])) { obj[o] = process.env[m[1]]; }
for (var p in creds) {
if (creds.hasOwnProperty(p)) {
flowUtil.mapEnvVarProperties(creds,p);
}
}
}
}
loopOver(creds);
node.credentials = creds;
} else if (credentials.getDefinition(node.type)) {
node.credentials = {};
@ -160,7 +153,6 @@ module.exports = {
// disableFlow: flows.disableFlow,
// enableFlow: flows.enableFlow,
// Credentials
addCredentials: credentials.add,
getCredentials: credentials.get,

View File

@ -34,7 +34,20 @@ describe('flows/util', function() {
getType.restore();
});
describe('#mapEnvVarProperties',function() {
it('handles ENV substitutions in an object', function() {
process.env.foo1 = "bar1";
process.env.foo2 = "bar2";
process.env.foo3 = "bar3";
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() {
it('handles a null old node', function() {