Skip to content

Commit

Permalink
Humanize config vars that are loaded from the environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Oct 4, 2016
1 parent 13163b0 commit 7eb6af6
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions lib/app/configuration/rc.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,83 @@
/**
* Module dependencies
*/

var _ = require('lodash');
var rc = require('rc');

var rttc = require('rttc');

/**
* Locate and load a .sailsrc file if one exists.
* Locate and load .sailsrc files if they exist.
*
* NOTE: this occurs almost immediately when sails is required,
* and since `rc` is synchronous, the examination of env variables,
* cmdline opts, and .sailsrc files is immediate, and happens only once.
*
* @type {Object}
*/
module.exports = rc('sails');
var conf = rc('sails');

// Load in overrides from the environment, using `rttc.parseHuman` to
// guesstimate the types.
// NOTE -- the code below is lifted from the `rc` module, and modified to:
// 1. Pass JSHint
// 2. Run parseHuman on values
// If at some point `rc` exposes metadata about which configs came from
// the environment, we can simplify our code by just running `parseHuman`
// on those values instead of doing the work to pluck them from the env.
var env = (function () {
var env = process.env;
var prefix = 'sails_';
var obj = {};
var l = prefix.length;
_.each(_.keys(env), function(k) {
if((k.indexOf(prefix)) === 0) {

var keypath = k.substring(l).split('__');

// Trim empty strings from keypath array
var _emptyStringIndex;
while ((_emptyStringIndex=keypath.indexOf('')) > -1) {
keypath.splice(_emptyStringIndex, 1);
}

var cursor = obj;
_.each(keypath, function _buildSubObj(_subkey,i){

// (check for _subkey first so we ignore empty strings)
if (!_subkey) {
return;
}

// If this is the last key, just stuff the value in there
// Assigns actual value from env variable to final key
// (unless it's just an empty string- in that case use the last valid key)
if (i === keypath.length-1){
try {
cursor[_subkey] = rttc.parseHuman(env[k], 'json');
} catch(e) {
cursor[_subkey] = rttc.parseHuman(env[k]);
}
}


// Build sub-object if nothing already exists at the keypath
if (cursor[_subkey] === undefined){
cursor[_subkey] = {};
}

// Increment cursor used to track the object at the current depth
cursor = cursor[_subkey];

});

}

});
return obj;
})();

// Merge the humanized environment vars into the config we loaded via `rc`.
conf = _.merge(conf, env);

// Expose the final conf object to the world.
module.exports = conf;

0 comments on commit 7eb6af6

Please sign in to comment.