Skip to content

Commit 39b6b3d

Browse files
committed
Merge pull request breser#1 from kalistace/master
Add support for .properties for expand_key mode
2 parents 71124ec + 10faf51 commit 39b6b3d

File tree

6 files changed

+465
-66
lines changed

6 files changed

+465
-66
lines changed

lib/consul/index.js

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var _ = require('underscore');
22
var fs = require('fs');
33
var path = require('path');
4+
var utils = require('../utils.js');
45

56
var logger = require('../logging.js');
67

@@ -21,7 +22,7 @@ exports.setToken = function(tok) {
2122

2223
var write_content_to_consul = function(key_name, content, cb) {
2324
logger.trace('Adding key %s, value:\n%s', key_name, content);
24-
consul.kv.set({'key': key_name, value: content, token: token}, function(err) {
25+
consul.kv.set({'key': key_name, value: content !== null ? String(content) : null, token: token}, function(err) {
2526
if (err) {
2627
return cb('Failed to write key ' + key_name + ' due to ' + err);
2728
}
@@ -54,7 +55,7 @@ var create_key_name = function(branch, file, ref) {
5455
/**
5556
* Given an obj, recurse into it, populating the parts array with all of the key->value
5657
* relationships, prefixed by parent objs.
57-
*
58+
*
5859
* For example, the obj { 'first': { 'second': { 'third' : 'whee' }}} should yield a
5960
* parts array with a single entry: 'first/second/third' with value 'whee'.
6061
*/
@@ -74,7 +75,7 @@ var render_obj = function(parts, prefix, obj) {
7475
* as parents of subtrees. Arrays are ignored since they add annoying problems (multiple array
7576
* entries can have the same value, so what do we do then?).
7677
*/
77-
var populate_kvs_from_json = function(branch, prefix, obj, cb) {
78+
var populate_kvs_from_object = function(branch, prefix, obj, cb) {
7879

7980
var writes = [];
8081

@@ -89,41 +90,94 @@ var populate_kvs_from_json = function(branch, prefix, obj, cb) {
8990
});
9091
};
9192

93+
9294
/**
9395
* If a file was modified, read its new value and update consul's KV store.
9496
*/
9597
var file_modified = function(branch, file, cb) {
9698

97-
var fqf = branch.branch_directory + path.sep + file;
98-
99-
logger.trace('Attempting to read "%s"', fqf);
100-
101-
if (branch.expand_keys && file.endsWith('.json')) {
102-
// Delete current tree. Yeah, I know this is kinda the coward's way out, but it's a hell of a lot
103-
// easier to get provably correct than diffing the file against the contents of the KV store.
104-
file_deleted(branch, file, function(err) {
105-
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err);
106-
107-
fs.readFile(fqf, {encoding:'utf8'}, function(err, body) {
99+
var handle_json_kv_file = function(file_path, cb) {
100+
fs.readFile(file_path, {encoding: 'utf8'}, function (err, body) {
108101
/* istanbul ignore if */
109-
if (err) return cb('Failed to read key ' + fqf + ' due to ' + err);
102+
if (err) return cb('Failed to read key ' + file_path + ' due to ' + err);
110103
var body = body ? body.trim() : '';
111104
try {
112105
var obj = JSON.parse(body);
113-
populate_kvs_from_json(branch, create_key_name(branch, file), obj, cb);
114-
} catch(e) {
106+
populate_kvs_from_object(branch, create_key_name(branch, file), obj, cb);
107+
} catch (e) {
115108
logger.warn("Failed to parse .json file. Using body string as a KV.");
116109
write_content_to_consul(create_key_name(branch, file), body, cb);
117110
}
118111
});
112+
};
113+
114+
var handle_properties_kv_file = function(file_path, common_properties_relative_path, cb) {
115+
116+
function extract_and_populate_properties(file_body, common_body, cb) {
117+
utils.load_properties(file_body, common_body, function (error, obj) {
118+
if (error) {
119+
logger.warn('Failed to load properties for : ' + file + ' due to : ' + error + '.' + ' Using body string as a KV.');
120+
handle_as_flat_file(file_path, branch, file, cb);
121+
} else {
122+
populate_kvs_from_object(branch, create_key_name(branch, file), obj, cb);
123+
}
124+
});
125+
}
126+
127+
fs.readFile(file_path, {encoding: 'utf8'}, function (err, file_body) {
128+
/* istanbul ignore if */
129+
if (err) return cb('Failed to read key ' + file_path + ' due to ' + err);
130+
131+
if(common_properties_relative_path) {
132+
var path_to_common_properties_file = branch.branch_directory + path.sep + common_properties_relative_path;
133+
fs.readFile(path_to_common_properties_file, {encoding: 'utf8'}, function (err, common_body) {
134+
if (err) {
135+
logger.warn('Failed to read common variables for ' + path_to_common_properties_file + ' due to ' + err);
136+
common_body = '';
137+
}
138+
extract_and_populate_properties(file_body, common_body, cb);
139+
});
140+
}
141+
else {
142+
extract_and_populate_properties(file_body, '', cb);
143+
}
119144
});
120-
} else {
121-
fs.readFile(fqf, {encoding:'utf8'}, function(err, body) {
145+
};
146+
147+
var handle_as_flat_file = function(fqf, branch, file, cb) {
148+
fs.readFile(fqf, {encoding: 'utf8'}, function (err, body) {
122149
/* istanbul ignore if */
123150
if (err) return cb('Failed to read key ' + fqf + ' due to ' + err);
124151
var body = body ? body.trim() : '';
125152
write_content_to_consul(create_key_name(branch, file), body, cb);
126153
});
154+
};
155+
156+
var handle_expended_keys_with_different_file_types = function() {
157+
if (file.endsWith('.json')) {
158+
// Delete current tree. Yeah, I know this is kinda the coward's way out, but it's a hell of a lot
159+
// easier to get provably correct than diffing the file against the contents of the KV store.
160+
file_deleted(branch, file, function (err) {
161+
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err);
162+
handle_json_kv_file(fqf, cb);
163+
});
164+
} else if (file.endsWith('.properties')) {
165+
file_deleted(branch, file, function (err) {
166+
if (err) return cb('Failed to delete key ' + key_name + ' due to ' + err);
167+
handle_properties_kv_file(fqf, branch.common_properties, cb);
168+
});
169+
} else {
170+
handle_as_flat_file(fqf, branch, file, cb);
171+
}
172+
};
173+
174+
var fqf = branch.branch_directory + path.sep + file;
175+
logger.trace('Attempting to read "%s"', fqf);
176+
177+
if (branch.expand_keys) {
178+
handle_expended_keys_with_different_file_types();
179+
} else {
180+
handle_as_flat_file(fqf, branch, file, cb);
127181
}
128182
};
129183

lib/git/branch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ function Branch(repo_config, name) {
1616
Object.defineProperty(this, 'branch_parent', {value: repo_config.local_store + path.sep + repo_config.name});
1717
Object.defineProperty(this, 'branch_directory', {value: this.branch_parent + path.sep + name});
1818
Object.defineProperty(this, 'expand_keys', { value: repo_config['expand_keys'] === true });
19+
Object.defineProperty(this, 'common_properties', { value: repo_config['common_properties']});
1920
Object.defineProperty(this, 'include_branch_name', {
2021
// If include_branch_name is not set, assume true. Otherwise, identity check the value against true.
2122
value: repo_config['include_branch_name'] == undefined || repo_config['include_branch_name'] === true

lib/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require('fs');
2+
var properties = require ("properties");
23

34
/**
45
* Check to make sure the provided path is a writeable directory. Throw an exception if not.
@@ -23,3 +24,19 @@ module.exports.validate_writeable_directory = function(dir) {
2324
throw new Error(dir + ' is not a directory');
2425
}
2526
};
27+
28+
var options = {
29+
path: false,
30+
variables: true
31+
};
32+
33+
module.exports.load_properties = function (specific_file, common_file, cb){
34+
properties.parse (common_file, options,
35+
function (error, env){
36+
if (error) return cb (error);
37+
//Pass the common properties as external variables
38+
options.vars = env;
39+
40+
properties.parse (specific_file, options, cb);
41+
});
42+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"express": "~4.6.1",
2121
"mkdirp": "0.5.0",
2222
"rimraf": "2.2.8",
23-
"underscore": "^1.8.0"
23+
"underscore": "^1.8.0",
24+
"properties": "1.2.1"
2425
},
2526
"devDependencies": {
2627
"istanbul": "0.2.11",

0 commit comments

Comments
 (0)