From 2bda7b6216362b554729d04900bab6365f7d3ffa Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 13 Sep 2011 07:38:41 -0400 Subject: [PATCH] [api] Added `nconf.stores.System` --- lib/nconf/provider.js | 6 ++-- lib/nconf/stores/system.js | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 lib/nconf/stores/system.js diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index c214ba6..f4a4076 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -17,10 +17,10 @@ var async = require('async'), // var Provider = exports.Provider = function (options) { options = options || {}; - this.overrides = options.overrides || null + this.overrides = options.overrides || null; this.useArgv = options.useArgv || false; - - this.store = stores.create(options.type || 'memory', options); + this.useEnv = options.useEnv || false; + this.store = stores.create(options.type || 'memory', options); }; // diff --git a/lib/nconf/stores/system.js b/lib/nconf/stores/system.js new file mode 100644 index 0000000..e2f599e --- /dev/null +++ b/lib/nconf/stores/system.js @@ -0,0 +1,72 @@ +/* + * system.js: Simple memory-based store for process environment variables and + * command-line arguments. + * + * (C) 2011, Charlie Robbins + * + */ + +var util = require('util'), + Memory = require('./memory').Memory; + +// +// ### function System (options) +// #### @options {Object} Options for this instance. +// Constructor function for the System nconf store, a simple abstraction +// around the Memory store that can read process environment variables +// and command-line arguments. +// +var System = exports.System = function (options) { + Memory.call(this, options); + + this.type = 'system'; + this.only = options.only || []; + this.argv = options.argv || {}; +}; + +// Inherit from the Memory store +util.inherits(System, Memory); + +// +// ### function loadSync () +// Loads the data passed in from `process.env` into this instance. +// +System.prototype.loadSync = function () { + this.loadEnv(); + this.loadArgv(); + + return this.store; +}; + +// +// ### function loadEnv () +// Loads the data passed in from `process.env` into this instance. +// +System.prototype.loadEnv = function () { + var self = this; + + Object.keys(process.env).filter(function (key) { + return !self.only.length || self.only.indexOf(key) !== -1; + }).forEach(function (key) { + self.set(key, process.env[key]); + }); + + return this.store; +}; + +// +// ### function loadSync () +// Loads the data passed in from the command-line arguments +// into this instance. +// +Argv.prototype.loadArgv = function () { + var self = this, argv = Object.keys(this.argv) > 0 + ? require('optimist').options(this.argv).argv + : require('optimist').argv; + + Object.keys(argv).forEach(function (key) { + self.set(key, argv[key]) + }); + + return this.store; +}; \ No newline at end of file