-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathenvLoader.js
71 lines (61 loc) · 1.51 KB
/
envLoader.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
fs = require('fs');
module.exports.loadFromFileSync = function(filename) {
if (!fs.existsSync('env.data')){
console.log('env.data file not found');
} else {
var vars = fs.readFileSync('env.data').toString().split('\n');
var count = 0;
vars.forEach(function(line){
var key = line.split('=')[0];
var value = line.slice(key.length+1);
key = key.trim();
if (key) {
value = value.trim();
process.env[key] = value;
count++;
}
});
console.log(count+' key(s) loaded from env.data');
}
}
module.exports.loadFromRedis = function(options, callback){
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
var redis = require('redis');
client = redis.createClient(options.port, options.host, options);
client.on("error", function (err) {
console.log("Error " + err);
});
var pwd;
if (fs.existsSync('./redis-auth.tmp')) {
pwd = fs.readFileSync('./redis-auth.tmp').toString();
}
var getData = function(){
client.hgetall('hubot:env', function(err, reply){
var count = 0;
if (!err && reply) {
for (var key in reply) {
if (reply.hasOwnProperty(key)){
process.env[key] = reply[key];
count++;
}
}
}
console.log(count+' key(s) loaded from redis');
client.quit();
if (typeof callback === 'function') {
callback(err);
}
});
};
if (pwd) {
client.auth(pwd, function(){
getData();
});
} else {
getData();
}
}