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

Allow providing a default value when an environment variable is used. #715

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 16 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ function walkConfig (level) {
for (var configEntry in level) {
if (level[configEntry] && level[configEntry].ENV) {
if (!process.env[level[configEntry].ENV]) {
log.verbose(
'Environment variable ' + level[configEntry].ENV + ' is empty!'
);
if (level[configEntry].default !== undefined) {
log.verbose('Environment variable ' + level[configEntry].ENV + ' is empty, used provided default value.');
level[configEntry] = level[configEntry].default;
} else {
log.verbose(
'Environment variable ' + level[configEntry].ENV + ' is empty!'
);
}
} else {
level[configEntry] = process.env[level[configEntry].ENV];
}

level[configEntry] = process.env[level[configEntry].ENV];
} else if (level[configEntry] && typeof level[configEntry] === 'object') {
level[configEntry] = walkConfig(level[configEntry]);
}
Expand All @@ -120,7 +125,12 @@ exports.loadObject = function (_config, currentEnv) {
for (var env in config) {
if (config[env].ENV) {
if (!process.env[config[env].ENV]) {
log.verbose('Environment variable ' + config[env].ENV + ' is empty!');
if (config[env].default !== undefined) {
log.verbose('Environment variable ' + config[env].ENV + ' is empty, used provided default value.');
out[env] = parseDatabaseUrl(config[env].default);
} else {
log.verbose('Environment variable ' + config[env].ENV + ' is empty!');
}
} else {
out[env] = parseDatabaseUrl(process.env[config[env].ENV]);
}
Expand Down
32 changes: 32 additions & 0 deletions test/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ lab.experiment('config', function () {
}
);

lab.experiment(
'loading from a file with ENV vars that have defaults set',
function () {
var _config = config.load(path.join(__dirname, 'database_with_env_using_default.json'), 'prod');
lab.test(
'should load the default value specified in the config', () => {
Code.expect(_config.prod.username).to.equal('defaultUser');
}
);
}
);

lab.experiment(
'loading from a file with ENV URL',

Expand All @@ -122,6 +134,26 @@ lab.experiment('config', function () {
}
);

lab.experiment(
'loading from a file with ENV URL default',

function () {
var configPath = path.join(__dirname, 'database_with_env_url_default.json');
var _config = config.load(configPath, 'prod');

lab.test(
'should load a value from the environments', () => {
var current = _config.getCurrent();
Code.expect(current.settings.driver).to.equal('postgres');
Code.expect(current.settings.user).to.equal('uname');
Code.expect(current.settings.password).to.equal('pw');
Code.expect(current.settings.host).to.equal('server.com');
Code.expect(current.settings.database).to.equal('dbname');
}
);
}
);

lab.experiment('loading from an URL', function () {
var databaseUrl = 'postgres://uname:[email protected]/dbname';
var _config = config.loadUrl(databaseUrl, 'dev');
Expand Down
3 changes: 3 additions & 0 deletions test/database_with_env_url_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prod": {"ENV": "BOGUS_ENV_VAR", "default": "postgres://uname:[email protected]/dbname" }
}
7 changes: 7 additions & 0 deletions test/database_with_env_using_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"prod": {
"driver": "sqlite3",
"filename": "prod.db",
"username": {"ENV": "BOGUS_ENV_VAR", "default": "defaultUser" }
}
}