Skip to content

Commit 3cb7bdf

Browse files
committed
Feat: Implemented default values for undefined env vars
closes #765 Signed-off-by: Kael Shipman <[email protected]>
1 parent 67b6a4b commit 3cb7bdf

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

lib/config.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,26 @@ exports.loadFile = function (fileName, currentEnv, plugins) {
9696

9797
function walkConfig (level) {
9898
for (var configEntry in level) {
99-
if (level[configEntry] && level[configEntry].ENV) {
100-
if (!process.env[level[configEntry].ENV]) {
101-
log.verbose(
102-
'Environment variable ' + level[configEntry].ENV + ' is empty!'
103-
);
99+
if (!level[configEntry]) {
100+
continue;
101+
// If we're asking to get the value from an env var
102+
} else if (level[configEntry].ENV) {
103+
// If the env var is set, use it
104+
if (typeof process.env[level[configEntry].ENV] !== 'undefined') {
105+
level[configEntry] = process.env[level[configEntry].ENV];
106+
} else {
107+
// Otherwise, if a default is provided, use that
108+
log.verbose('Environment variable ' + level[configEntry].ENV + ' is empty!');
109+
if (typeof level[configEntry].default !== 'undefined') {
110+
log.verbose('Using default value provided.');
111+
level[configEntry] = level[configEntry].default;
112+
} else {
113+
// Otherwise, there's no var and there's no default - set it to undefined
114+
level[configEntry] = undefined;
115+
}
104116
}
105-
106-
level[configEntry] = process.env[level[configEntry].ENV];
107-
} else if (level[configEntry] && typeof level[configEntry] === 'object') {
117+
} else if (typeof level[configEntry] === 'object') {
118+
// If we're NOT asking to get the value from an env var and the value is an object, recurse
108119
level[configEntry] = walkConfig(level[configEntry]);
109120
}
110121
}
@@ -121,6 +132,10 @@ exports.loadObject = function (_config, currentEnv) {
121132
if (config[env].ENV) {
122133
if (!process.env[config[env].ENV]) {
123134
log.verbose('Environment variable ' + config[env].ENV + ' is empty!');
135+
if (typeof config[env].default !== 'undefined') {
136+
log.verbose('Using default value provided.');
137+
out[env] = parseDatabaseUrl(config[env].default);
138+
}
124139
} else {
125140
out[env] = parseDatabaseUrl(process.env[config[env].ENV]);
126141
}

test/config_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ lab.experiment('config', function () {
9090

9191
function () {
9292
process.env.DB_MIGRATE_TEST_VAR = 'username_from_env';
93+
process.env.EMPTY_VAR = '';
94+
if (typeof process.env.NOT_SET !== 'undefined') {
95+
delete process.env.NOT_SET;
96+
}
9397
var configPath = path.join(__dirname, 'database_with_env.json');
9498
var _config = config.load(configPath, 'prod');
9599

@@ -98,6 +102,16 @@ lab.experiment('config', function () {
98102
Code.expect(_config.prod.username).to.equal('username_from_env');
99103
}
100104
);
105+
lab.test(
106+
'should load default value from not set env var', () => {
107+
Code.expect(_config.prod.password).to.equal('my-password');
108+
}
109+
);
110+
lab.test(
111+
'should use value from env var even when empty, rather than default', () => {
112+
Code.expect(_config.prod.host).to.equal('');
113+
}
114+
);
101115
}
102116
);
103117

@@ -122,6 +136,29 @@ lab.experiment('config', function () {
122136
}
123137
);
124138

139+
lab.experiment(
140+
'loading from a file from default when ENV URL is not set',
141+
142+
function () {
143+
if (typeof process.env.DB_MIGRATE_TEST_VAR !== "undefined") {
144+
delete process.env.DB_MIGRATE_TEST_VAR;
145+
}
146+
var configPath = path.join(__dirname, 'database_with_env_url.json');
147+
var _config = config.load(configPath, 'prod');
148+
149+
lab.test(
150+
'should load the url from default when env var not set', () => {
151+
var current = _config.getCurrent();
152+
Code.expect(current.settings.driver).to.equal('postgres');
153+
Code.expect(current.settings.user).to.equal('uname');
154+
Code.expect(current.settings.password).to.equal('pw');
155+
Code.expect(current.settings.host).to.equal('server.com');
156+
Code.expect(current.settings.database).to.equal('dbname');
157+
}
158+
);
159+
}
160+
);
161+
125162
lab.experiment('loading from an URL', function () {
126163
var databaseUrl = 'postgres://uname:[email protected]/dbname';
127164
var _config = config.loadUrl(databaseUrl, 'dev');

test/database_with_env.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"prod": {
33
"driver": "sqlite3",
44
"filename": "prod.db",
5-
"username": {"ENV": "DB_MIGRATE_TEST_VAR"}
5+
"username": {"ENV": "DB_MIGRATE_TEST_VAR"},
6+
"password": {"ENV": "NOT_SET", "default": "my-password"},
7+
"host": {"ENV": "EMPTY_VAR", "default": "my-host"}
68
}
79
}

test/database_with_env_url.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"prod": {"ENV": "DB_MIGRATE_TEST_VAR"}
2+
"prod": {"ENV": "DB_MIGRATE_TEST_VAR", "default": "postgres://uname:[email protected]/dbname"}
33
}

0 commit comments

Comments
 (0)