Skip to content

Commit

Permalink
env: off/on are truthy sometimes
Browse files Browse the repository at this point in the history
Provide readENVTruthy, and mapTruthy, to explicitly handle
the cases where on/off or similar should result in a
Boolean value.  Here, they should be mapped correctly,
eliminating a problem @MilosKozak experienced when setting
AUTH_DEFAULT_ROLES=bad, there is an exception becaue the
value is a literal `true` instead of the string `"bad"`.

Intended to be a no-op, preserving the intended effects
from previous commit
dbe29bd from
@jasoncalabrese.
  • Loading branch information
bewest committed Aug 9, 2016
1 parent 60e82a0 commit 0f42e69
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 9 additions & 4 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ function updateSettings() {
//should always find extended settings last
env.extendedSettings = findExtendedSettings(process.env);

if (!readENV('TREATMENTS_AUTH', true)) {
env.settings.authDefaultRoles = env.settings.authDefaultRoles || [ ];
if (!readENVTruthy('TREATMENTS_AUTH', true)) {
env.settings.authDefaultRoles = env.settings.authDefaultRoles || "";
env.settings.authDefaultRoles += ' careportal';
}

Expand All @@ -151,12 +151,17 @@ function readENV(varName, defaultValue) {
|| process.env[varName]
|| process.env[varName.toLowerCase()];

if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }

return value != null ? value : defaultValue;
}

function readENVTruthy(varName, defaultValue) {
var value = readENV(varName, defaultValue);
if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }
return value;
}

function findExtendedSettings (envs) {
var extended = {};

Expand Down
15 changes: 14 additions & 1 deletion lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ function init ( ) {
};

var valueMappers = {
alarmUrgentHighMins: mapNumberArray
nightMode: mapTruthy
, alarmUrgentHigh: mapTruthy
, alarmUrgentHighMins: mapNumberArray
, alarmHigh: mapTruthy
, alarmHighMins: mapNumberArray
, alarmLow: mapTruthy
, alarmLowMins: mapNumberArray
, alarmUrgentLow: mapTruthy
, alarmUrgentLowMins: mapNumberArray
, alarmUrgentMins: mapNumberArray
, alarmTimeagoWarn: mapTruthy
, alarmTimeagoUrgent: mapTruthy
, alarmWarnMins: mapNumberArray
, timeFormat: mapNumber
};
Expand Down Expand Up @@ -80,6 +87,12 @@ function init ( ) {
}
}

function mapTruthy (value) {
if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }
return value;
}

//TODO: getting sent in status.json, shouldn't be
settings.DEFAULT_FEATURES = ['delta', 'direction', 'timeago', 'devicestatus', 'upbat', 'errorcodes', 'profile'];

Expand Down

0 comments on commit 0f42e69

Please sign in to comment.