Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·317 lines (288 loc) · 8.77 KB
/
Copy pathcli.js
File metadata and controls
executable file
·317 lines (288 loc) · 8.77 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env node
var path = require('path');
var Config = require('./index.js');
var parseArgs = require('minimist');
var log_levels = ['silly', 'debug', 'info', 'warn', 'critical'];
var log_level = 4;
var script = path.basename(process.argv[1]);
var commands; // Command definitions, assigned below.
// The global options. The `help` strings are what nomnom printed, and the
// usage generators below reproduce nomnom's help layout.
var global_options = [
{
name: "version",
help: "Print version and exit"
},
{
name: "noInherit",
abbr: "n",
full: "no-inherit",
help: "Prevent configuration inheritance"
},
{
name: "verbose",
abbr: "v",
help: "Increase verbosity, can be used multiple times"
}
];
var pad = function pad (str, width) {
while (str.length < width) str += " ";
return str;
};
var option_labels = function option_labels () {
return global_options.map(function (opt) {
var label = opt.abbr ? "-" + opt.abbr + ", " : "";
return label + "--" + (opt.full || opt.name);
});
};
var options_help = function options_help (lines) {
var labels = option_labels();
var width = Math.max.apply(null, labels.map(function (label) {
return label.length;
})) + 3;
lines.push("Options:");
global_options.forEach(function (opt, i) {
lines.push(" " + pad(labels[i], width) + opt.help);
});
};
var usage = function usage () {
var lines = [""];
var names = Object.keys(commands);
var width = Math.max.apply(null, names.map(function (name) {
return name.length;
})) + 5;
lines.push("Usage: " + script + " <command> [options]");
lines.push("");
lines.push(pad("command", width + 2));
names.forEach(function (name) {
lines.push(" " + pad(name, width) + commands[name].help);
});
lines.push("");
options_help(lines);
lines.push("");
return lines.join("\n");
};
var command_usage = function command_usage (name) {
var command = commands[name];
var lines = [""];
var optnames = Object.keys(command.options);
var args = optnames.map(function (optname) {
var required = command.options[optname].required;
return required ? "<" + optname + ">" : "[" + optname + "]";
});
var width = Math.max.apply(null, optnames.map(function (optname) {
return optname.length;
})) + 5;
lines.push("Usage: " + script + " " + name + " " + args.join(" ") +
" [options]");
lines.push("");
optnames.forEach(function (optname) {
lines.push(pad(optname, width) + command.options[optname].help);
});
lines.push("");
options_help(lines);
lines.push("");
lines.push(command.help);
return lines.join("\n");
};
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;
};
commands = {
dump: {
help: "Dump the current etcd configuration",
options: {
prefix: {
position: 1,
help: "Etcd namespace (default: config/)",
default: "config/",
required: true
}
},
callback: cmd(function (conf) {
return conf.dump();
})
},
list: {
help: "List all available etcd configurations",
options: {
prefix: {
position: 1,
help: "Etcd namespace (default: /)",
default: "/",
required: true
}
},
callback: cmd(function (conf) {
return conf.list(undefined, {dirOnly: false});
})
},
clear: {
help: "Clear the current etcd configuration",
options: {
prefix: {
position: 1,
help: "Etcd namespace",
required: true
},
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();
})
},
load: {
help: "Load a configuration from a file",
options: {
prefix: {
position: 1,
help: "Etcd namespace",
required: true
},
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});
})
},
get: {
help: "Get a configuration key",
options: {
prefix: {
position: 1,
help: "Etcd namespace",
required: true
},
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;
})
},
set: {
help: "Set a configuration key to a value",
options: {
prefix: {
position: 1,
help: "Etcd namespace",
required: true
},
key: {
position: 2,
help: "Configuration key",
required: true
},
value: {
position: 3,
help: "Configuration value",
required: true
}
},
callback: cmd(function (conf, params) {
conf.set(params.key, params.value);
})
}
};
var args = process.argv.slice(2);
var ddash = args.indexOf("--");
var head = ddash === -1 ? args : args.slice(0, ddash);
var tail = ddash === -1 ? [] : args.slice(ddash);
// minimist turns `--no-foo` into `foo: false` instead of setting a
// `no-foo` flag, so normalize the long spellings of the `-n` option to the
// short form before parsing. Anything after `--` is left untouched.
head = head.map(function (arg) {
if (arg === "--no-inherit" || arg === "--noInherit") return "-n";
return arg;
});
// minimist does not accumulate repeated boolean flags, but `-v` is meant
// to be usable multiple times, so count occurrences ourselves.
var verbosity = head.filter(function (arg) {
return arg === "-v" || arg === "--verbose";
}).length;
while (verbosity-- > 0 && log_level > 0) log_level -= 1;
var argv = parseArgs(head.concat(tail), {
string: ["_"],
boolean: ["version", "help", "h", "n"]
});
var name = argv._[0];
var command = commands[name];
if (command === undefined) {
if (name !== undefined) {
console.log(script + ": no such command '" + name + "'");
return process.exit(1);
}
if (argv.help || argv.h) {
console.log(usage());
return process.exit(0);
}
if (argv.version) {
console.log(require('./package').version);
}
console.log("\ncommand argument is required\n" + usage());
return process.exit(1);
}
if (argv.help || argv.h) {
console.log(command_usage(name));
return process.exit(0);
}
var params = {};
var positionals = argv._.slice(1);
Object.keys(command.options).forEach(function (optname) {
var opt = command.options[optname];
var value = positionals[opt.position - 1];
if (value === undefined && opt.default !== undefined) {
value = opt.default;
}
if (value === undefined && opt.required) {
console.log("\n" + optname + " argument is required\n" +
command_usage(name));
process.exit(1);
}
params[optname] = value;
});
if (argv.version) {
console.log(require('./package').version);
}
params.noInherit = argv.n === true;
command.callback(params);