Configuration support module that manages configuration commandlineargs/files/options/environment
Notes
- We use nconf module for holding all configuration data; our functions just offer ways to use that more flexibly
- We use yargs to handle commandline processing; our functions just offer ways to use that more flexibly
- Source:
- Copyright:
- 5/9/19
Methods
(private, inner) addConfigFile(filepath, flagErrorOnFileNotExist)
- Source:
Helper function to add a file to our queued list of config files (which will be processed in order)
Parameters:
Name | Type | Description |
---|---|---|
filepath |
* | |
flagErrorOnFileNotExist |
* | if set then an exception will be thrown if file does not exist; otherwise nothing happens |
Throws:
error if file is not found and flagErrorOnFileNotExist is true
Returns:
true if file found and queued for loading
(private, inner) addConfigFilesCli()
- Source:
Get a list of config files specified on the commandline via nconf, and load those in
(inner) configAutoConverTypeVal(val)
- Source:
Convert boolean "strings" to boolean values
Parameters:
Name | Type | Description |
---|---|---|
val |
* |
Returns:
val converted to bool if its "true" or "false"
(inner) discoverConfigFiles()
- Source:
Add all the config files we can find from default source config dir, and local dir, plus maybe some cli passed ones, PLUS some special overrides that can be used for testing
(private, inner) doNconfFile(rootTag, filepath)
- Source:
Invoke the nconf.file function but check file extension and support yaml files
Parameters:
Name | Type | Description |
---|---|---|
rootTag |
* | tag for nconf |
filepath |
* | filepath for nconf |
(inner) findQueuedCommand(cmd, flagPop)
- Source:
Find a queud command by name and return it. This is useful as an alternative way of finding commands explicitly
Parameters:
Name | Type | Description |
---|---|---|
cmd |
string | |
flagPop |
boolean | if true the found command will be popped off the queue |
Returns:
undefined if not found, or queued command in format {command: commandName, argv, callback}
(private, inner) fixConfigFilePathName(filepath)
- Source:
Fixup config file name by adding base directory, doing any %SPECIAL% substrings and adding .json after it if its not explicitly provided
Parameters:
Name | Type | Description |
---|---|---|
filepath |
string |
Returns:
filepath with base directory and extension (yml) added
(inner) getDebugFiles()
- Source:
Function for diagnostics/debugging.
Returns:
an object that contains a list of the config files in order of priority
(inner) getDebugHierarchy()
- Source:
Function for diagnostics/debugging.
Returns:
an object that contains a list of the config files in order of priority
(inner) getDebugOptions()
- Source:
Function for diagnostics/debugging.
Returns:
an object that contains the merged set of options, so that overridden options are respected
(private, inner) getVal(variadic)
- Source:
Just pass along a get request to the underlying nconf module. Use getValDefault if you don't want exception error thrown on missing variable
Example
getVal("DEBUG") - returns the value of the DEBUG config variable
Parameters:
Name | Type | Description |
---|---|---|
variadic |
* | args but normally just a single string with the variable name |
Throws:
error if variable not found
Returns:
value
(private, inner) getValDefault(arg, defaultVal)
- Source:
Get the value of a variable; if its undefined then return defaultVal
Parameters:
Name | Type | Description |
---|---|---|
arg |
string | variable name to find |
defaultVal |
* |
Returns:
the config value for variable specified, or defaultVal if not found
(inner) ipStringToSafeFilenameString(val)
- Source:
Replace filename illegal characters (:) from ip string with underscores
Parameters:
Name | Type | Description |
---|---|---|
val |
* | ip string with colons |
Returns:
ip string with colons replaced with underscores
(private, inner) nconfMergeConfigFile(filepath, flagErrorOnFileNotExist)
- Source:
Merge in a config file to nconf configuration settings using nconf.file command
Parameters:
Name | Type | Description |
---|---|---|
filepath |
string | |
flagErrorOnFileNotExist |
boolean | throw an error if file does not exist? |
Throws:
exception if file is missing and flagErrorOnFileNotExist is true
Returns:
true if file found and loaded
(inner) parse()
- Source:
One time call to process options that have been set through other functions first
Throws:
error if you try to run this twice
(inner) popQueuedCommand()
- Source:
An alternate way to manually go through queue of commands
Returns:
the next queued command, in format {command: commandName, argv, callback}
(inner) queueYargsCommand(commandName, argv, callback)
- Source:
Queue a command with arguments, for the yargs commandline processing library
Parameters:
Name | Type | Description |
---|---|---|
commandName |
string | |
argv |
* | |
callback |
function |
(inner) runQueuedCommands(flagErrorIfNone)
- Source:
Run a set of queued yarg commandline options/commands.
Parameters:
Name | Type | Description |
---|---|---|
flagErrorIfNone |
boolean |
(inner) setConfigDirs(val)
- Source:
Set the base directory for config files and find a list of all valid config files in this subdir
Notes
- Normally you would call this with the actual directory containing the config files, but if you call it with the parent directory, it will look for a "config/" subdir and use that instead.
- This function then looks for a set of specific filenames in the config directory, in a specificy priority order, and queues the ones it finds
Parameters:
Name | Type | Description |
---|---|---|
val |
string | base directory where config files are stored |
(inner) setDefaultOptions(val)
- Source:
Set an object with default config options, which will be overridden by options found in config files
Parameters:
Name | Type | Description |
---|---|---|
val |
object | configuration object with properties that will be used if no config file sets them |
(inner) setEnvList(val)
- Source:
Sets the list of environment variable key names that will be loaded into the config environment (if not overridden in config files)
Parameters:
Name | Type | Description |
---|---|---|
val |
array | list of strings that identify which variables from environment to load |
(inner) setOverrideOptions(val)
- Source:
Set an object containing override options that will take precedence over any options found in any config files
Parameters:
Name | Type | Description |
---|---|---|
val |
object | configuration object with properties that will override any config file |
(private, inner) setServerFilenamePrefixFromServerIp(val)
- Source:
Caller should call this in order for us to discover and load SERVERIP prefixed config files automatically
Parameters:
Name | Type | Description |
---|---|---|
val |
string | server ip as string (we will convert : to _) |
(inner) setYargs(val)
- Source:
The caller should pass us the yargs commandline processor module object to use
Parameters:
Name | Type | Description |
---|---|---|
val |
object |