Skip to content

Commit

Permalink
added yaml support to expend_keys, breser#54
Browse files Browse the repository at this point in the history
  • Loading branch information
hexelon committed Aug 2, 2016
1 parent 9f6c586 commit 193b907
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
32 changes: 30 additions & 2 deletions lib/consul/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var _ = require('underscore');
var fs = require('fs');
var path = require('path');
var yaml = require('js-yaml');
var eachAsync = require('each-async');
var utils = require('../utils.js');

var logger = require('../logging.js');
Expand Down Expand Up @@ -104,7 +106,7 @@ var file_modified = function(branch, file, cb) {
fs.readFile(file_path, {encoding: 'utf8'}, function (err, body) {
/* istanbul ignore if */
if (err) return cb('Failed to read key ' + file_path + ' due to ' + err);
var body = body ? body.trim() : '';
body ? body.trim() : '';
try {
var obj = JSON.parse(body);
populate_kvs_from_object(branch, create_key_name(branch, file), obj, cb);
Expand Down Expand Up @@ -148,11 +150,32 @@ var file_modified = function(branch, file, cb) {
});
};

var handle_yaml_kv_file = function(file_path, cb) {
fs.readFile(file_path, {encoding: 'utf8'}, function (err, body) {
/* istanbul ignore if */
if (err) return cb('Failed to read key ' + file_path + ' due to ' + err);
body ? body.trim() : '';
var docs = [];
try {
yaml.loadAll(body, function(doc) {
docs.push(doc);
});

eachAsync(docs, function(doc,index,cb) {
populate_kvs_from_object(branch, create_key_name(branch, file), doc, cb);
}, cb);
} catch (e) {
logger.warn("Failed to parse .yaml file " + file + ". Using body string as a KV.",e);
write_content_to_consul(create_key_name(branch, file), body, cb);
}
});
};

var handle_as_flat_file = function(fqf, branch, file, cb) {
fs.readFile(fqf, {encoding: 'utf8'}, function (err, body) {
/* istanbul ignore if */
if (err) return cb('Failed to read key ' + fqf + ' due to ' + err);
var body = body ? body.trim() : '';
body ? body.trim() : '';
write_content_to_consul(create_key_name(branch, file), body, cb);
});
};
Expand All @@ -170,6 +193,11 @@ var file_modified = function(branch, file, cb) {
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err);
handle_properties_kv_file(fqf, branch.common_properties, cb);
});
} else if (file.endsWith('.yaml') || file.endsWith('.yml')) {
file_deleted(branch, file, function (err) {
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err);
handle_yaml_kv_file(fqf, branch.common_properties, cb);
});
} else {
handle_as_flat_file(fqf, branch, file, cb);
}
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"mkdirp": "0.5.0",
"rimraf": "2.2.8",
"underscore": "^1.8.0",
"properties": "1.2.1"
"properties": "1.2.1",
"js-yaml": "^3.6.1",
"each-async": "^1.1.1"
},
"devDependencies": {
"grunt": "^0.4.5",
Expand Down

0 comments on commit 193b907

Please sign in to comment.