Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano-pogliani committed Jan 26, 2016
0 parents commit 5e64ae0
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.swp

node_modules/
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright (c) 2016 Arts Alliance Media

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
77 changes: 77 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
MkConf
======
Generate configuration files dynamically.

`MkConf` is a tool to generate configuration files out of templates
and environment variables.


Quick start
-----------
```bash
npm install -g github:artsalliancemedia/mk-config
mkconf --help
# Usage: node mkconf
#
# -h, --help display this help
# -e, --env-map=FILE JSON map from environment variables to template variables
# -d, --defaults=FILE JSON file with the default data to pass to the template
# -i, --input=FILE Read the template from FILE, defaults to stdin
# -o, --output=FILE Write the rendered data to FILE, defaults to stdout

mkconf --defaults=config.json --env-map=overrides.json --input=config.template > out.config
mkconf --defaults=config.json --env-map=overrides.json --input=config.template --output=out.config
```

### Examples
The following examples are based on the input files listed here:

* `config.json`:
```json
{
"db-name": "myapp",
"bind-ip": "127.0.0.1"
}
```

* `overrides.json`:
```json
{
"db-name": "DB_NAME"
}
```

* `config.template`:
```text
[app]
bind = {{ bind-ip }}

[db]
name = {{ db-name }}
```

#### Clean environment
In this example the environment variable `DB_NAME` is not set.
```bash
mkconf --defaults=config.json --env-map=overrides.json --input=config.template --output=out.config
cat out.config
[app]
bind = 127.0.0.1

[db]
name = myapp
```

#### Environment override
If the environment variable `DB_NAME` is instead set,
the value in it will be used instead of the default:
```bash
export DB_NAME="mytest"
mkconf --defaults=config.json --env-map=overrides.json --input=config.template --output=out.config
cat out.config
[app]
bind = 127.0.0.1

[db]
name = mytest
```
42 changes: 42 additions & 0 deletions bin/mkconf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/env node

// Build CLI parser.
var getopt = require("node-getopt");
var parser = getopt.create([
["h", "help", "display this help"],

["e", "env-map=FILE", "JSON map from environment variables to template variables"],
["d", "defaults=FILE", "JSON file with the default data to pass to the template"],

["i", "input=FILE", "Read the template from FILE, defaults to stdin"],
["o", "output=FILE", "Write the rendered data to FILE, defaults to stdout"]

]).bindHelp();


// Parse options.
var opts = parser.parseSystem();
var options = {
"env-map": opts.options["env-map"],
"defaults": opts.options.defaults,
"input": opts.options.input,

"output": opts.options.output || "-"
}

if (!options.defaults) {
console.error("Missing required --defaults option.");
process.exit(1)
}
if (!options["env-map"]) {
console.error("Missing required --env-map option.");
process.exit(1)
}
if (!options.input) {
console.error("Missing required --input option.");
process.exit(1)
}

var MkConf = require("../lib/mkconf");
var mkconf = new MkConf(options);
mkconf.make();
69 changes: 69 additions & 0 deletions lib/mkconf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
var fs = require("fs");
var Handlebars = require("handlebars");


// Create a new template processor.
var MkConf = module.exports = function MkConf(options) {
this._options = options;
this._engine = Handlebars.create();
};


// Read and parse JSON defaults.
MkConf.prototype._getDefaults = function _getDefaults() {
var raw = fs.readFileSync(this._options.defaults);
return JSON.parse(raw);
};

// Read and parse JSON env-map, then update template data.
MkConf.prototype._getEnvironment = function _getEnvironment(data) {
var raw = fs.readFileSync(this._options["env-map"]);
var env = JSON.parse(raw);

Object.keys(env).forEach(function(data_name) {
var env_name = env[data_name];
if (process.env.hasOwnProperty(env_name)) {
data[data_name] = process.env[env_name];
}
});

return data;
};


// Read the template from file or stdin.
MkConf.prototype._getTemplate = function _getTemplate(callback) {
return fs.readFileSync(this._options.input, { encoding: "utf-8" });
};

// Compile the template and render it with the data.
MkConf.prototype._render = function _render(source, data) {
var template = this._engine.compile(source, { noEscape: true });
return template(data);
};


// Write the output to stdout/stderr.
MkConf.prototype._write = function _write(data) {
if (this._options.output === "-") {
process.stdout.write(data);

} else {
fs.writeFileSync(this._options.output, data);
}
};


// Load template and render it.
MkConf.prototype.make = function make() {
// Load data.
var data = this._getDefaults();
this._getEnvironment(data);

// Render template.
var template = this._getTemplate();
var output = this._render(template, data);

// Write output.
this._write(output);
};
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "mkconf",
"version": "0.0.0",
"description": "Generate configuration files dynamically",
"author": "Stefano Pogliani <[email protected]",

"bin": {
"mkconf": "./bin/mkconf"
},

"main": "lib/mkconf.js",
"repository": {
"type": "git",
"url": "https://github.com/artsalliancemedia/mk-config.git"
},

"keywords": [
"configuration",
"handlebars",
"template"
],

"dependencies": {
"handlebars": "~4.0.5",
"node-getopt": "~0.2.3"
},

"license": "MIT"
}

0 comments on commit 5e64ae0

Please sign in to comment.