forked from shakefu/jetconfig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·159 lines (146 loc) · 4.08 KB
/
cli.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
var path = require('path');
var Config = require('./index.js');
var parser = require('nomnom');
var log_levels = ['silly', 'debug', 'info', 'warn', 'critical'];
var log_level = 4;
parser.script(path.basename(process.argv[1]));
parser.option('version', {
flag: true,
help: "Print version and exit",
callback: function () { console.log(require('./package').version); }
})
.option('noInherit', {
flag: true,
help: "Prevent configuration inheritance",
abbr: 'n',
full: 'no-inherit',
})
.option('verbose', {
abbr: 'v',
help: "Increase verbosity, can be used multiple times",
flag: true,
callback: function () {
if (log_level > 0) log_level -= 1;
}
});
var cmd = function cmd (func) {
var handler = function handler (params) {
var opts = {};
var conf;
var result;
opts.logLevel = log_levels[log_level];
opts.prefix = params.prefix;
opts.inherit = !params.noInherit;
conf = new Config(opts);
result = func(conf, params);
if (result !== undefined) console.log(JSON.stringify(result, null, 2));
};
return handler;
};
parser.command('dump')
.help("Dump the current etcd configuration")
.option('prefix', {
position: 1,
help: 'Etcd namespace (default: config/)',
default: 'config/',
required: true
})
.callback(cmd(function (conf) {
return conf.dump();
}));
parser.command('list')
.help("List all available etcd configurations")
.option('prefix', {
position: 1,
help: 'Etcd namespace (default: /)',
default: '/',
required: true
})
.callback(cmd(function (conf) {
return conf.list(undefined, {dirOnly: false});
}));
parser.command('clear')
.help("Clear the current etcd configuration")
.option('prefix', {
position: 1,
help: 'Etcd namespace',
required: true
})
.option('extra', {
position: 2,
help: 'Don\'t forget to include a / between env and key',
required: false,
})
.callback(cmd(function (conf, params) {
if (params.extra) return {error: "You forgot a slash didn't you?"};
conf.allowClear = true;
conf.clear();
}));
parser.command('load')
.help("Load a configuration from a file")
.option('prefix', {
position: 1,
help: 'Etcd namespace',
required: true
})
.option('file', {
position: 2,
help: 'Filename to load',
required: true,
})
.callback(cmd(function (conf, params) {
var filename = path.resolve(params.file);
var new_conf;
try {
new_conf = require(filename);
} catch (err) {
if (String(err).match(/Cannot find module/)) {
console.log("No such file:", params.file);
}
else {
console.log('' + err);
}
return process.exit(1);
}
conf.log.silly("Read config", new_conf);
conf.load(new_conf, {cacheOnly: false});
}));
parser.command('get')
.help("Get a configuration key")
.option('prefix', {
position: 1,
help: 'Etcd namespace',
required: true
})
.option('key', {
position: 2,
help: 'Configuration key',
required: true
})
.callback(cmd(function (conf, params) {
var value = conf.get(params.key, params.value);
if (value === undefined) process.exit(1);
return value;
}));
parser.command('set')
.help("Set a configuration key to a value")
.option('prefix', {
position: 1,
help: 'Etcd namespace',
required: true
})
.option('key', {
position: 2,
help: 'Configuration key',
required: true
})
.option('value', {
position: 3,
help: 'Configuration value',
required: true
})
.callback(cmd(function (conf, params) {
conf.set(params.key, params.value);
}));
parser.parse();