Skip to content

Commit

Permalink
Merge pull request #65 from grawk/feature.env.ignore
Browse files Browse the repository at this point in the history
add feature for ignoring environment variables
  • Loading branch information
grawk authored Jan 17, 2017
2 parents c439495 + f8127c2 commit f28d39f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ These protocols will be used to process the config data prior to registration.
If using an array of handler implementations, each handler is run in series (see [`Multiple handlers` in the shortstop README](https://github.com/krakenjs/shortstop#multiple-handlers)).
* `defaults` (*String*) - the name of the file containing all default values.
Defaults to `config.json`.
* `envignore` (*Array*) - any properties found in `process.env` that should be ignored

```javascript
'use strict';
Expand Down
5 changes: 3 additions & 2 deletions lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const debug = debuglog('confit');

export default class Factory {

constructor({ basedir, protocols = {}, defaults = 'config.json' }) {
constructor({ basedir, protocols = {}, defaults = 'config.json', envignore = []}) {
this.envignore = envignore.push('env');
this.basedir = basedir;
this.protocols = protocols;
this.promise = Promise.resolve({})
Expand All @@ -42,7 +43,7 @@ export default class Factory {
return Handlers.resolveImport(loadJsonicSync(file), this.basedir)
.then(data => Common.merge(loadJsonicSync(file), store));
}))
.then(store => Common.merge(Provider.env(), store))
.then(store => Common.merge(Provider.env(envignore), store))
.then(store => Common.merge(Provider.argv(), store));
}

Expand Down
4 changes: 2 additions & 2 deletions lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ export default {
return result;
},

env() {
env(ignore) {
let result = {};

// process.env is not a normal object, so we
// need to map values.
for (let env of Object.keys(process.env)) {
//env:env is decided by process.env.NODE_ENV.
//Not allowing process.env.env to override the env:env value.
if (env !== 'env') {
if (ignore.indexOf(env) < 0) {
result[env] = process.env[env];
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/confit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,5 +533,28 @@ test('confit', function (t) {
t.end();
});
});
t.test('env ignore', function (t) {
var basedir, options;

var env = process.env = {
NODE_ENV: 'development',
fromlocal: 'config:local',
local: 'motion',
ignoreme: 'file:./path/to/mindyourbusiness'
};
basedir = path.join(__dirname, 'fixtures', 'defaults');
options = {
basedir: basedir,
envignore: ['ignoreme']
};

confit(options).create(function (err, config) {
t.error(err);
// Ensure env is read except for the desired ignored property
t.equal(config.get('fromlocal'), env.local);
t.equal(config.get('ignoreme'), undefined);
t.end();
});
});

});
2 changes: 1 addition & 1 deletion test/provider-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ test('env', function (t) {
env: 'development'
};

val = provider.env();
val = provider.env(['env']);
t.equal(val.foo, 'bar');
//env() provider ignores process.env.env
t.equal(val.env, undefined);
Expand Down

0 comments on commit f28d39f

Please sign in to comment.