mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
add support of environtment variable for tab & group
This commit is contained in:
87
packages/node_modules/@node-red/util/lib/util.js
vendored
87
packages/node_modules/@node-red/util/lib/util.js
vendored
@@ -515,18 +515,83 @@ function setObjectProperty(msg,prop,value,createMissing) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Get value of environment variable defined in group node.
|
||||
* @param {String} group - group node
|
||||
* @param {String} name - name of variable
|
||||
* @return {Object} object containing the value in val property or null if not defined
|
||||
*/
|
||||
function getGroupSetting(node, group, flow, name) {
|
||||
if (group) {
|
||||
if (!name.startsWith("$parent.")) {
|
||||
if (group.env) {
|
||||
if (!group._env) {
|
||||
const envs = group.env;
|
||||
const entries = envs.map((env) => [env.name, env]);
|
||||
group._env = Object.fromEntries(entries);
|
||||
}
|
||||
const env = group._env[name];
|
||||
if (env) {
|
||||
let value = env.value;
|
||||
const type = env.type;
|
||||
if ((type !== "env") || (value !== name)) {
|
||||
if (type === "env") {
|
||||
value = value.replace(new RegExp("\\${"+name+"}","g"),"${$parent."+name+"}");
|
||||
}
|
||||
try {
|
||||
if (type === "bool") {
|
||||
const val = ((value === "true") ||
|
||||
(value === true));
|
||||
return {
|
||||
val: val
|
||||
};
|
||||
}
|
||||
if (type === "cred") {
|
||||
return {
|
||||
val: value
|
||||
};
|
||||
}
|
||||
var val = evaluateNodeProperty(value, type, node, null, null);
|
||||
return {
|
||||
val: val
|
||||
};
|
||||
}
|
||||
catch (e) {
|
||||
this.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
name = name.substring(8);
|
||||
}
|
||||
if (group.g && flow) {
|
||||
const parent = flow.getGroupNode(group.g);
|
||||
return getGroupSetting(node, parent, flow, name);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/*!
|
||||
* Get value of environment variable.
|
||||
* @param {Node} node - accessing node
|
||||
* @param {String} name - name of variable
|
||||
* @return {String} value of env var
|
||||
*/
|
||||
function getSetting(node, name) {
|
||||
if (node && node._flow) {
|
||||
var flow = node._flow;
|
||||
if (flow) {
|
||||
return flow.getSetting(name);
|
||||
function getSetting(node, name, flow_) {
|
||||
var flow = (flow_ ? flow_ : (node ? node._flow : null));
|
||||
if (flow) {
|
||||
if (node && node.g) {
|
||||
const group = flow.getGroupNode(node.g);
|
||||
const result = getGroupSetting(node, group, flow, name);
|
||||
if (result) {
|
||||
return result.val;
|
||||
}
|
||||
}
|
||||
return flow.getSetting(name);
|
||||
}
|
||||
return process.env[name];
|
||||
}
|
||||
@@ -544,18 +609,19 @@ function getSetting(node, name) {
|
||||
* @memberof @node-red/util_util
|
||||
*/
|
||||
function evaluateEnvProperty(value, node) {
|
||||
var flow = (node && node.hasOwnProperty("_flow")) ? node._flow : null;
|
||||
var result;
|
||||
if (/^\${[^}]+}$/.test(value)) {
|
||||
// ${ENV_VAR}
|
||||
var name = value.substring(2,value.length-1);
|
||||
result = getSetting(node, name);
|
||||
result = getSetting(node, name, flow);
|
||||
} else if (!/\${\S+}/.test(value)) {
|
||||
// ENV_VAR
|
||||
result = getSetting(node, value);
|
||||
result = getSetting(node, value, flow);
|
||||
} else {
|
||||
// FOO${ENV_VAR}BAR
|
||||
return value.replace(/\${([^}]+)}/g, function(match, name) {
|
||||
var val = getSetting(node, name);
|
||||
var val = getSetting(node, name, flow);
|
||||
return (val === undefined)?"":val;
|
||||
});
|
||||
}
|
||||
@@ -668,7 +734,7 @@ function prepareJSONataExpression(value,node) {
|
||||
return node.context().global.get(val, store);
|
||||
});
|
||||
expr.assign('env', function(name) {
|
||||
var val = getSetting(node, name);
|
||||
var val = getSetting(node, name, node._flow);
|
||||
if (typeof val !== 'undefined') {
|
||||
return val;
|
||||
} else {
|
||||
@@ -976,5 +1042,6 @@ module.exports = {
|
||||
normaliseNodeTypeName: normaliseNodeTypeName,
|
||||
prepareJSONataExpression: prepareJSONataExpression,
|
||||
evaluateJSONataExpression: evaluateJSONataExpression,
|
||||
parseContextStore: parseContextStore
|
||||
parseContextStore: parseContextStore,
|
||||
getSetting: getSetting
|
||||
};
|
||||
|
Reference in New Issue
Block a user