Merge pull request #49 from mhart/nested-env-configs
Add support for nested configs via env
This commit is contained in:
commit
6dd2351991
6 changed files with 55 additions and 10 deletions
16
README.md
16
README.md
|
@ -159,6 +159,22 @@ Responsible for loading the values parsed from `process.env` into the configurat
|
|||
// Can optionally also be an Array of values to limit process.env to.
|
||||
//
|
||||
nconf.env(['only', 'load', 'these', 'values', 'from', 'process.env']);
|
||||
|
||||
//
|
||||
// Can also specify a separator for nested keys (instead of the default ':')
|
||||
//
|
||||
nconf.env('__');
|
||||
// Get the value of the env variable 'database__host'
|
||||
var dbHost = nconf.get('database:host');
|
||||
|
||||
//
|
||||
// Or use both options
|
||||
//
|
||||
nconf.env({
|
||||
separator: '__',
|
||||
whitelist: ['database__host', 'only', 'load', 'these', 'values']
|
||||
});
|
||||
var dbHost = nconf.get('database:host');
|
||||
```
|
||||
|
||||
### Literal
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
var util = require('util'),
|
||||
common = require('../common'),
|
||||
Memory = require('./memory').Memory;
|
||||
|
||||
//
|
||||
|
@ -17,9 +18,17 @@ var util = require('util'),
|
|||
var Env = exports.Env = function (options) {
|
||||
Memory.call(this, options);
|
||||
|
||||
this.type = 'env';
|
||||
this.readOnly = true;
|
||||
this.options = options || [];
|
||||
options = options || {};
|
||||
this.type = 'env';
|
||||
this.readOnly = true;
|
||||
this.whitelist = options.whitelist || [];
|
||||
this.separator = options.separator || '';
|
||||
if (options instanceof Array) {
|
||||
this.whitelist = options;
|
||||
}
|
||||
if (typeof(options) === 'string') {
|
||||
this.separator = options;
|
||||
}
|
||||
};
|
||||
|
||||
// Inherit from the Memory store
|
||||
|
@ -43,9 +52,13 @@ Env.prototype.loadEnv = function () {
|
|||
|
||||
this.readOnly = false;
|
||||
Object.keys(process.env).filter(function (key) {
|
||||
return !self.options.length || self.options.indexOf(key) !== -1;
|
||||
return !self.whitelist.length || self.whitelist.indexOf(key) !== -1;
|
||||
}).forEach(function (key) {
|
||||
self.set(key, process.env[key]);
|
||||
if (self.separator) {
|
||||
self.set(common.key.apply(common, key.split(self.separator)), process.env[key]);
|
||||
} else {
|
||||
self.set(key, process.env[key]);
|
||||
}
|
||||
});
|
||||
|
||||
this.readOnly = true;
|
||||
|
|
|
@ -183,7 +183,7 @@ Memory.prototype.merge = function (key, value) {
|
|||
}
|
||||
|
||||
return Object.keys(value).every(function (nested) {
|
||||
return self.merge(fullKey + ':' + nested, value[nested]);
|
||||
return self.merge(common.key(fullKey, nested), value[nested]);
|
||||
});
|
||||
};
|
||||
|
||||
|
|
11
test/fixtures/scripts/nconf-nested-env.js
vendored
Normal file
11
test/fixtures/scripts/nconf-nested-env.js
vendored
Normal file
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* nconf-nested-env.js: Test fixture for env with nested keys.
|
||||
*
|
||||
* (C) 2012, Nodejitsu Inc.
|
||||
* (C) 2012, Michael Hart
|
||||
*
|
||||
*/
|
||||
|
||||
var nconf = require('../../../lib/nconf').env('_');
|
||||
|
||||
process.stdout.write(nconf.get('SOME:THING'));
|
|
@ -60,6 +60,10 @@ vows.describe('nconf/provider').addBatch({
|
|||
script: path.join(fixturesDir, 'scripts', 'nconf-hierarchical-file-argv.js'),
|
||||
argv: ['--something', 'foobar'],
|
||||
env: { SOMETHING: true }
|
||||
}),
|
||||
"when 'env' is set to true with a nested separator": helpers.assertSystemConf({
|
||||
script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'),
|
||||
env: { SOME_THING: 'foobar' }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,8 +16,9 @@ vows.describe('nconf/stores/env').addBatch({
|
|||
"should have the correct methods defined": function (env) {
|
||||
assert.isFunction(env.loadSync);
|
||||
assert.isFunction(env.loadEnv);
|
||||
assert.isArray(env.options);
|
||||
assert.lengthOf(env.options, 0);
|
||||
assert.isArray(env.whitelist);
|
||||
assert.lengthOf(env.whitelist, 0);
|
||||
assert.equal(env.separator, '');
|
||||
}
|
||||
}
|
||||
}).export(module);
|
Loading…
Reference in a new issue