/
config.js
90 lines (78 loc) · 1.8 KB
/
config.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* config.js: Configuration for police cli.
*
* Copyright © 2011 Pavan Kumar Sunkara. All rights reserved
*/
var config = module.exports;
/*
* Requiring modules
*/
var nconf = require('nconf')
, winston = require('winston')
, fs = require('fs')
, path = require('path');
config.load = function () {
/*
* Load options from argv
*/
nconf.argv();
/*
* Now load options from file /home/user/.policeconf
* or the file specified in env or argv
*/
var conf = config.path();
if (!fs.existsSync(conf)) {
winston.silly('Initalizing configuration file'.cyan);
try {
fs.writeFileSync(conf, '{}');
fs.chmodSync(conf, 0600);
} catch (err) {
winston.warn('Cannot intialize configuration file.'.red.bold);
throw err;
}
}
nconf.file(conf);
nconf = nconf.stores.file;
return this;
}
/*
* Get github authenticated token
*/
config.token = function (police) {
if (!config.cached_token) {
config.cached_token = nconf.get('token') || process.env.GITHUB_TOKEN;
if (!config.cached_token) {
police.winston.warn('You are not authenticated to github'.red.bold);
police.help.show(1);
}
}
return config.cached_token;
}
/*
* Define get function which passes args to nconf.get()
*/
config.get = function (key) {
return nconf.get(key);
}
/*
* Define set function which passes args to nconf.set()
*/
config.set = function (key, value) {
nconf.set(key, value);
}
/*
* Define save function which saves the config to file
*/
config.save = function (police) {
nconf.save(function (err) {
if (err) police.exit(err);
winston.silly('Saving configuration file.'.cyan);
police.exit();
});
}
/*
* Get configuration file path
*/
config.path = function () {
return nconf.get('conf') || path.join(process.env.HOME, '.policeconf');
}