UNPKG

2.06 kBJavaScriptView Raw
1const Table = require('easy-table');
2const { l10n, print, Config } = require('../..');
3
4exports.command = 'config [key] [value]';
5
6exports.aliases = ['cfg'];
7
8exports.desc = l10n('CMD_CFG_DESC');
9
10exports.builder = (yargs) => {
11 yargs
12 .usage(l10n('CMD_CFG_USAGE'))
13 .option('reset', {
14 describe: l10n('CMD_CFG_OPT_RESET'),
15 type: 'boolean',
16 })
17 .example('dmhy config', l10n('CMD_CFG_EXAMPLE1_DESC'))
18 .example('dmhy config downloader', l10n('CMD_CFG_EXAMPLE2_DESC'))
19 .example('dmhy config downloader deluge', l10n('CMD_CFG_EXAMPLE3_DESC'));
20};
21
22exports.handler = (argv) => {
23 // pkv := parameter key value
24 const config = new Config();
25 if (argv.key) {
26 let pkv = config.get(argv.key);
27 if (!pkv) {
28 print.error(l10n('CMD_CFG_UNKNOWN_KEY', { key: argv.key }));
29 process.exit(1);
30 }
31 if (argv.value) {
32 const validator = Config.VALIDATORS[pkv.key] || (() => ({ 'ok': true }));
33 const result = validator(argv.value);
34 if (!result.ok) {
35 print.warn(result.msg);
36 }
37 const spkv = config.set(argv.key, argv.value);
38 if (spkv) {
39 print.success(l10n('CMD_CFG_SET_SUCCESS'));
40 } else {
41 print.error(l10n('CMD_CFG_SET_FAILED'));
42 }
43
44 const printable = {};
45 printable[l10n('CMD_CFG_CELL_KEY')] = spkv.key;
46 printable[l10n('CMD_CFG_CELL_VALUE')] = spkv.value;
47 console.log(Table.print(printable));
48 } else {
49 if (argv.reset) {
50 config.reset(argv.key);
51 pkv = config.get(argv.key);
52 }
53 const printable = {};
54 printable[l10n('CMD_CFG_CELL_KEY')] = pkv.key;
55 printable[l10n('CMD_CFG_CELL_VALUE')] = pkv.value;
56 console.log(Table.print(printable));
57 }
58 } else {
59 if (argv.reset) {
60 config.reset();
61 }
62 const t = new Table();
63 Object.entries(config.parameters).forEach(([key, value]) => {
64 t.cell(l10n('CMD_CFG_CELL_KEY'), key);
65 t.cell(l10n('CMD_CFG_CELL_VALUE'), value);
66 t.newRow();
67 });
68 console.log(t.toString());
69 }
70 process.exit(0);
71};