1 | var Config = exports.Config = function(repo) {
|
2 | var _repo = repo, _data = null;
|
3 |
|
4 | Object.defineProperty(this, "repo", { get: function() { return _repo; }, set: function(value) { _id = value; }, enumerable: false});
|
5 | Object.defineProperty(this, "data", { get: function() {
|
6 | _data = lazy_reader(_repo, 'data', _data);
|
7 | return _data;
|
8 | }, set: function(value) { _data = value; }, enumerable: true});
|
9 | }
|
10 |
|
11 | var lazy_reader = function(repo, name, variable) {
|
12 | if(variable) return variable;
|
13 |
|
14 | var done = false;
|
15 | var hash = {};
|
16 |
|
17 | repo.git.config({list:true}, function(err, output) {
|
18 | var lines = output.split("\n");
|
19 |
|
20 | lines.forEach(function(line) {
|
21 | var parts = line.split(/=/);
|
22 | var key = parts.shift();
|
23 | hash[key] = parts.join("=");
|
24 | })
|
25 | done = true;
|
26 | })
|
27 |
|
28 | while(!done) {};
|
29 | return hash;
|
30 | }
|
31 |
|
32 | Config.prototype.fetch = function(key, default_value) {
|
33 | var value = this.data[key];
|
34 | if(!value) return default_value;
|
35 | return this.data[key];
|
36 | }
|
37 |
|
38 | Config.prototype.set = function(key, value, callback) {
|
39 | var self = this;
|
40 |
|
41 | this.repo.git.config({}, key, value, function(err, output) {
|
42 | if(err) return callback(err, output);
|
43 |
|
44 | self.data = null;
|
45 |
|
46 | callback(null, output);
|
47 | });
|
48 | } |
\ | No newline at end of file |