From 1c9fd14650d03feade049a01b73414ae14cbc4a1 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 11 Nov 2020 17:14:27 -0700 Subject: [PATCH 1/4] Allow providing a default value when an environment variable is used. Signed-off-by: Robert Kempton --- lib/config.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index f27e641a..0fab44d5 100644 --- a/lib/config.js +++ b/lib/config.js @@ -120,7 +120,11 @@ 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(process.env[config[env].default]){ + log.verbose('Environment variable ' + config[env].ENV + ' is empty, used provided default value.'); + } else { + log.verbose('Environment variable ' + config[env].ENV + ' is empty!'); + } } else { out[env] = parseDatabaseUrl(process.env[config[env].ENV]); } From 6766e169c791d731523e220c471d3c91d2b32e0f Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 11 Nov 2020 17:26:16 -0700 Subject: [PATCH 2/4] specifically check for undefined since someone could set null, 0, etc. Signed-off-by: Robert Kempton --- lib/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index 0fab44d5..c593ad4d 100644 --- a/lib/config.js +++ b/lib/config.js @@ -120,7 +120,7 @@ exports.loadObject = function (_config, currentEnv) { for (var env in config) { if (config[env].ENV) { if (!process.env[config[env].ENV]) { - if(process.env[config[env].default]){ + if(process.env[config[env].default] !== undefined){ log.verbose('Environment variable ' + config[env].ENV + ' is empty, used provided default value.'); } else { log.verbose('Environment variable ' + config[env].ENV + ' is empty!'); From 193f32a43c90141243b536f692893bc37a8fc192 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 11 Nov 2020 17:28:04 -0700 Subject: [PATCH 3/4] actually set the var and actually read the right variable :| Signed-off-by: Robert Kempton --- lib/config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/config.js b/lib/config.js index c593ad4d..972f74af 100644 --- a/lib/config.js +++ b/lib/config.js @@ -120,8 +120,9 @@ exports.loadObject = function (_config, currentEnv) { for (var env in config) { if (config[env].ENV) { if (!process.env[config[env].ENV]) { - if(process.env[config[env].default] !== undefined){ + 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!'); } From 34ef05c0259a825de435a75cc6ba8064f53f7b72 Mon Sep 17 00:00:00 2001 From: Robert Kempton Date: Wed, 11 Nov 2020 22:00:31 -0700 Subject: [PATCH 4/4] add tests fix non url case Signed-off-by: Robert Kempton --- lib/config.js | 17 +++++++----- test/config_test.js | 32 +++++++++++++++++++++++ test/database_with_env_url_default.json | 3 +++ test/database_with_env_using_default.json | 7 +++++ 4 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 test/database_with_env_url_default.json create mode 100644 test/database_with_env_using_default.json diff --git a/lib/config.js b/lib/config.js index 972f74af..b6b463af 100644 --- a/lib/config.js +++ b/lib/config.js @@ -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]); } @@ -120,7 +125,7 @@ exports.loadObject = function (_config, currentEnv) { for (var env in config) { if (config[env].ENV) { if (!process.env[config[env].ENV]) { - if(config[env].default !== undefined){ + 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 { diff --git a/test/config_test.js b/test/config_test.js index a9b5f9ee..a3cd2273 100644 --- a/test/config_test.js +++ b/test/config_test.js @@ -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', @@ -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:pw@server.com/dbname'; var _config = config.loadUrl(databaseUrl, 'dev'); diff --git a/test/database_with_env_url_default.json b/test/database_with_env_url_default.json new file mode 100644 index 00000000..8636e5aa --- /dev/null +++ b/test/database_with_env_url_default.json @@ -0,0 +1,3 @@ +{ + "prod": {"ENV": "BOGUS_ENV_VAR", "default": "postgres://uname:pw@server.com/dbname" } +} diff --git a/test/database_with_env_using_default.json b/test/database_with_env_using_default.json new file mode 100644 index 00000000..70dc8e10 --- /dev/null +++ b/test/database_with_env_using_default.json @@ -0,0 +1,7 @@ +{ + "prod": { + "driver": "sqlite3", + "filename": "prod.db", + "username": {"ENV": "BOGUS_ENV_VAR", "default": "defaultUser" } + } +}