This repository was archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
54 lines (43 loc) · 1.49 KB
/
index.js
File metadata and controls
54 lines (43 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
let _ = require('lodash'),
async = require('async'),
argv = require('minimist')(process.argv.slice(2)),
host = argv.host || '127.0.0.1',
port = argv.port || '8500',
inputFile = argv.input,
flatten = require('./flatten'),
consul = require('consul')({ host, port });
let debug = (argv.debug)? console.log.bind(console) : function(){};
let assert = (condition, failMessage, err) => {
if (!condition){
console.error(`Error: ${failMessage}`);
if (err) console.error(err.stack);
process.exit(-1);
}
};
assert(inputFile, 'Please specify the bootstrap file with the --input flag.');
let object = null;
try {
object = require(inputFile);
}
catch (e){
assert(!e, `Could not load input file: ${inputFile}.`, e)
}
assert(_.isObject(object), 'Input is not an object.');
let flattenedObject = flatten(object);
let insertKeyValuePair = (key) => {
return (next) => {
let value = flattenedObject[key];
let serialized = (_.isString(value))? value : JSON.stringify(value, null, 2);
consul.kv.set(key, serialized, (err, results) => {
if (err) console.error(`ERROR: setting key ${key}: ${err}`);
else debug(`DEBUG: Successfully set '${key}' with value: ${value}`);
return next(err, results);
});
};
};
let insertTasks = _.keys(flattenedObject).map(insertKeyValuePair);
async.parallel(insertTasks, (err) => {
assert(!err, 'Error inserting key/value pairs', err);
console.log('Key-Value pairs uploaded to Consul successfully.');
});