Merge pull request #98 from joaoafrmartins/master

filter process.env by regexp
This commit is contained in:
Charlie Robbins 2014-11-26 01:06:11 -05:00
commit d5bd26c0b6
2 changed files with 11 additions and 3 deletions

View file

@ -191,11 +191,12 @@ Responsible for loading the values parsed from `process.env` into the configurat
var dbHost = nconf.get('database:host'); var dbHost = nconf.get('database:host');
// //
// Or use both options // Or use all options
// //
nconf.env({ nconf.env({
separator: '__', separator: '__',
whitelist: ['database__host', 'only', 'load', 'these', 'values'] match: /^whatever_matches_this_will_be_whitelisted/
whitelist: ['database__host', 'only', 'load', 'these', 'values', 'if', 'whatever_doesnt_match_but_is_whitelisted_gets_loaded_too']
}); });
var dbHost = nconf.get('database:host'); var dbHost = nconf.get('database:host');
``` ```

View file

@ -21,6 +21,7 @@ var Env = exports.Env = function (options) {
options = options || {}; options = options || {};
this.type = 'env'; this.type = 'env';
this.readOnly = true; this.readOnly = true;
this.match = options.match
this.whitelist = options.whitelist || []; this.whitelist = options.whitelist || [];
this.separator = options.separator || ''; this.separator = options.separator || '';
if (options instanceof Array) { if (options instanceof Array) {
@ -52,7 +53,13 @@ Env.prototype.loadEnv = function () {
this.readOnly = false; this.readOnly = false;
Object.keys(process.env).filter(function (key) { Object.keys(process.env).filter(function (key) {
return !self.whitelist.length || self.whitelist.indexOf(key) !== -1; if(self.match && self.whitelist.length) {
return key.match(self.match) || self.whitelist.indexOf(key) !== -1
} else if (self.match) {
return key.match(self.match)
} else {
return !self.whitelist.length || self.whitelist.indexOf(key) !== -1
}
}).forEach(function (key) { }).forEach(function (key) {
if (self.separator) { if (self.separator) {
self.set(common.key.apply(common, key.split(self.separator)), process.env[key]); self.set(common.key.apply(common, key.split(self.separator)), process.env[key]);