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

feat: Implemented default values for undefined env vars #766

Open
wants to merge 2 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
31 changes: 23 additions & 8 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,26 @@ exports.loadFile = function (fileName, currentEnv, plugins) {

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]) {
continue;
// If we're asking to get the value from an env var
} else if (level[configEntry].ENV) {
// If the env var is set, use it
if (typeof process.env[level[configEntry].ENV] !== 'undefined') {
level[configEntry] = process.env[level[configEntry].ENV];
} else {
// Otherwise, if a default is provided, use that
log.verbose('Environment variable ' + level[configEntry].ENV + ' is empty!');
if (typeof level[configEntry].default !== 'undefined') {
log.verbose('Using default value provided.');
level[configEntry] = level[configEntry].default;
} else {
// Otherwise, there's no var and there's no default - set it to undefined
level[configEntry] = undefined;
}
}

level[configEntry] = process.env[level[configEntry].ENV];
} else if (level[configEntry] && typeof level[configEntry] === 'object') {
} else if (typeof level[configEntry] === 'object') {
// If we're NOT asking to get the value from an env var and the value is an object, recurse
level[configEntry] = walkConfig(level[configEntry]);
}
}
Expand All @@ -121,6 +132,10 @@ exports.loadObject = function (_config, currentEnv) {
if (config[env].ENV) {
if (!process.env[config[env].ENV]) {
log.verbose('Environment variable ' + config[env].ENV + ' is empty!');
if (typeof config[env].default !== 'undefined') {
log.verbose('Using default value provided.');
out[env] = parseDatabaseUrl(config[env].default);
}
} else {
out[env] = parseDatabaseUrl(process.env[config[env].ENV]);
}
Expand Down
37 changes: 37 additions & 0 deletions test/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ lab.experiment('config', function () {

function () {
process.env.DB_MIGRATE_TEST_VAR = 'username_from_env';
process.env.EMPTY_VAR = '';
if (typeof process.env.NOT_SET !== 'undefined') {
delete process.env.NOT_SET;
}
var configPath = path.join(__dirname, 'database_with_env.json');
var _config = config.load(configPath, 'prod');

Expand All @@ -98,6 +102,16 @@ lab.experiment('config', function () {
Code.expect(_config.prod.username).to.equal('username_from_env');
}
);
lab.test(
'should load default value from not set env var', () => {
Code.expect(_config.prod.password).to.equal('my-password');
}
);
lab.test(
'should use value from env var even when empty, rather than default', () => {
Code.expect(_config.prod.host).to.equal('');
}
);
}
);

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

lab.experiment(
'loading from a file from default when ENV URL is not set',

function () {
if (typeof process.env.DB_MIGRATE_TEST_VAR !== "undefined") {
delete process.env.DB_MIGRATE_TEST_VAR;
}
var configPath = path.join(__dirname, 'database_with_env_url.json');
var _config = config.load(configPath, 'prod');

lab.test(
'should load the url from default when env var not set', () => {
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
4 changes: 3 additions & 1 deletion test/database_with_env.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"prod": {
"driver": "sqlite3",
"filename": "prod.db",
"username": {"ENV": "DB_MIGRATE_TEST_VAR"}
"username": {"ENV": "DB_MIGRATE_TEST_VAR"},
"password": {"ENV": "NOT_SET", "default": "my-password"},
"host": {"ENV": "EMPTY_VAR", "default": "my-host"}
}
}
2 changes: 1 addition & 1 deletion test/database_with_env_url.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"prod": {"ENV": "DB_MIGRATE_TEST_VAR"}
"prod": {"ENV": "DB_MIGRATE_TEST_VAR", "default": "postgres://uname:[email protected]/dbname"}
}