Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a hierarchical config value to nconf on command line?

Colons separate nconf hierarchies, e.g.,

{
  "AUTH": {
    "ENABLED": true
  }
}

is accessed via:

nconf.get("AUTH:ENABLED");

I'd like to override this via environment and/or command line options under npm start, e.g.,

AUTH:ENABLED=false npm start

This fails under both bash and zsh because of the colon. Escaping the colon with \ doesn't help.

The following also fails under all circumstances (hierarchical or not):

npm start --AUTH:ENABLED=false

How can I pass a hierarchical config value through to nconf/optimist in a way that works?

like image 224
Dave Newton Avatar asked Jul 29 '14 14:07

Dave Newton


1 Answers

Easiest thing is to configure nconf to use a less-terrible separator character as per the docs:

//
// Or use both options
//
nconf.env({
  separator: '__',
  whitelist: ['database__host', 'only', 'load', 'these', 'values']
});
var dbHost = nconf.get('database:host');

My other suggestion is consider a less-bizarre configuration module. There are dozens on npm. Looking through the nconf github issues I see enough warning signs that this module is just doing too much with too many options. Configuration should be straightforward. If it isn't, walk away. My guess is while this might be appropriate if you are building a PaaS such as nodejitsu, most applications don't need an infinitely deep config hierarchy with a dozen different places to check for data.

like image 195
Peter Lyons Avatar answered Sep 26 '22 09:09

Peter Lyons