Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mapping env vars to multiple settings #189

Merged
merged 1 commit into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/convict.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,12 @@ function normalizeSchema(name, node, props, fullName, env, argv, sensitive) {

let o = Object.create(node);
props[name] = o;

// associate this property with an environmental variable
if (o.env) {
if (env[o.env]) {
throw new Error("'" + fullName + "' reuses an env variable: " + o.env);
if (!env[o.env]) {
env[o.env] = [];
}
env[o.env] = fullName;
env[o.env].push(fullName);
}

// associate this property with a command-line argument
Expand Down Expand Up @@ -247,9 +246,11 @@ function normalizeSchema(name, node, props, fullName, env, argv, sensitive) {

function importEnvironment(o) {
Object.keys(o._env).forEach(function(envStr) {
let k = o._env[envStr];
if (process.env[envStr]) {
o.set(k, process.env[envStr]);
let ks = o._env[envStr];
ks.forEach(function(k) {
o.set(k, process.env[envStr]);
});
}
});
}
Expand Down
17 changes: 17 additions & 0 deletions test/cases/env_multi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

exports.conf = {
foo: {
default: 'a',
format: String,
env: 'FOO'
},
bar: {
default: 'a',
format: String,
env: 'FOO'
},
};
exports.env = {
FOO: 'b'
};
1 change: 1 addition & 0 deletions test/cases/env_multi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/cases/env_multi.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "foo": "b", "bar": "b" }
7 changes: 0 additions & 7 deletions test/schema-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@ describe('convict schema file', function() {
conf = convict(path.join(__dirname, 'cases/schema-built-in-formats.json'));
});

it('must throw when parsing a specification that reuses an env variable', function() {
(function() { convict({
foo: {default: 'a', env: 'BAZ'},
bar: {default: 'a', env: 'BAZ'}
})}).must.throw();
});

it('must throw when parsing a specification that reuses a command-line argument', function() {
(function() { convict({
foo: {default: 'a', arg: 'BAZ'},
Expand Down