CC Configuration
[TOC]
Intro
This library is designed to standardize the way Concorde deals with configurations in Node releases. This library does not implement or enforce a particular configuration for any system. What it does is standardize where the config is found and how it is populated. It is intended to capture some practices from others, but it does not require you only use those practices. Instead it says if you do it this way, it will make your results predictable.
There are three ways to get configuration: command line, configuration file, and environment. 12 Factor apps specifies only using the environemnt as a configuration store. This is a handy idea, but doesn't scale to complex configurations (12 Factor says that that means the library is too complex - I doubt they are right). But, but, but for the things that differ from dev
to production
and the environments in between, the environment is ideal for configuration as a docker-compose
file can pull those values in easily. So the only difference from one environment to another is which docker-compose
file you use.
Configuration files should be used for defaults that rarely change. We use YAML files over JSON because they are easier to edit, support comments and are just less idiosyncratic. Command line (CLI) options should handle things that may change from run to run. They are also handy for quickly and easily changing logging metric options on the fly while debugging.
This library supports all three types, but won't enforce semantic meaning to where you get your configuration from. This is based on the confab configuration tool and yargs command line interpreter and inspired by this blog article.
The order in which things are loaded are config files -> enviroment variables -> CLI. Subsequent values with the same name are overwritten left to right.
#Installation
npm install @concorde2k/ccconfig
#Usage
Well, first you have to load it up. Then use it. Duh.
Let's assume you want to load environment variables that determine what database server to use, call them CC_SERVER
and CC_PORT
. We also have a rarely changed set of values that we want to store in a config file called ./config.yml
(note that ./
is the main module's path) and it looks like this:
alertColor: yellow
clientName: Bob's House of Pandas
This is how it would work:
const {config, environment, configFiles, defaults} = require("@concorde2k/ccconfig");
environment.CC_SERVER = "dbServer";
environment.CC_PORT = "dbPort";
defaults.dbServer = "CC-DEV-DB";
defaults.dbPort = 3363;
configFiles.push("./config/index.yml");
console.log(config.dbPort);
console.log(config.alertColor);
What if I want to add a configuration option after I have loaded the config?
const {config, load} = require("@concorde2k/ccconfig");
console.log(config.alertColor);
load({someNewOption: "red", alertColor: "purple:"});
console.log(config.alertColor);
console.log(config.someNewOption);
Command line options are handled pretty much the same way, except that all of yargs
syntax is supported.
const {config, cli} = require("@concorde2k/ccconfig");
cli.option(
"t",
{
alias : "target",
demandOption: true,
default : "AssetTracker",
describe : "The database on the target server that will be transferred",
type : "string"
}).option(
"s",
{
alias : "source",
demandOption: true,
default : "AssetTracker",
describe : "The database on the source server that will be transferred",
type : "string"
})
);
console.log(config.t)
And calling the program from the command line:
node myapp -t Terry
node myapp --target Terry
#Building
ccconfig
is built in Typescript and uses good ol' make
to build. You will need these tools:
npm install --global typescript
npm install --global typedoc typedoc-plugin-markdown
npm install --global typedoc-plugin-external-module-name
You will, of course, also need make
. Please use the Cygwin or Swan versions and make sure that the tools are in your PATH
.
make build
make clean
make dist-clean
make techdocs
make build-all
make patch-up
make minor-up
make publish