1 | 'use strict';
|
2 | const _ = require('lodash'),
|
3 | chalk = require('chalk'),
|
4 | options = require('./cli-options'),
|
5 | reporter = require('../lib/reporters'),
|
6 | config = require('../lib/cmd/config');
|
7 |
|
8 | function builder(yargs) {
|
9 | return yargs
|
10 | .usage('Usage: $0 config [value]')
|
11 | .example('$0 config --key local', 'View local api key')
|
12 | .example('$0 config --key prod as8d7s9d', 'Set prod api key')
|
13 | .example('$0 config --url some-article', 'Get url')
|
14 | .example('$0 config --url mysite domain.com', 'Set url')
|
15 | .option('k', options.key)
|
16 | .option('u', options.url)
|
17 | .option('r', options.reporter);
|
18 | }
|
19 |
|
20 | function set(argv) {
|
21 | if (argv.key) {
|
22 | config.set('key', argv.key, argv.value);
|
23 | reporter.logSummary(argv.reporter, 'config', () => ({ success: true, message: `set ${argv.key} ${argv.value}` }))([]);
|
24 | } else if (argv.url) {
|
25 | config.set('url', argv.url, argv.value);
|
26 | reporter.logSummary(argv.reporter, 'config', () => ({ success: true, message: `set ${argv.url} ${argv.value}` }))([]);
|
27 | } else {
|
28 | reporter.logSummary(argv.reporter, 'config', () => ({ success: false, message: 'Please provide either --key or --url' }))([]);
|
29 | process.exit(1);
|
30 | }
|
31 | }
|
32 |
|
33 | function get(argv) {
|
34 | let type, key, val;
|
35 |
|
36 | if (argv.key) {
|
37 | type = 'key';
|
38 | key = argv.key;
|
39 | val = config.get('key', argv.key);
|
40 | } else if (argv.url) {
|
41 | type = 'url';
|
42 | key = argv.url;
|
43 | val = config.get('url', argv.url);
|
44 | } else if (argv.reporter !== 'json') {
|
45 |
|
46 | reporter.logSummary(argv.reporter, 'config', () => ({ success: true, message: `Raw Config Values:\n${_.reduce(config.getAll(), (str, items, type) => {
|
47 | if (type === '__filename') {
|
48 | return str;
|
49 | }
|
50 |
|
51 | return str += `${chalk.blue(type)}:\n${_.reduce(items, (itemsStr, itemVal, itemKey) => {
|
52 | return itemsStr += `${chalk.bold(itemKey)} = ${itemVal}\n`;
|
53 | }, '')}\n`;
|
54 | }, '')}`}))([]);
|
55 | process.exit(0);
|
56 | } else {
|
57 |
|
58 | reporter.logSummary(argv.reporter, 'config', () => ({ success: true, message: `Raw Config Values:\n${JSON.stringify(config.getAll())}` }))([]);
|
59 | process.exit(0);
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | if (!_.includes([key, `http://${key}`, process.env.CLAYCLI_DEFAULT_KEY, process.env.CLAYCLI_DEFAULT_URL], val)) {
|
65 | reporter.logSummary(argv.reporter, 'config', () => ({ success: true, message: val }))([]);
|
66 | } else {
|
67 | reporter.logSummary(argv.reporter, 'config', () => ({ success: false, message: `${type}: ${key} not found` }))([]);
|
68 | }
|
69 | }
|
70 |
|
71 | function handler(argv) {
|
72 | if (argv.value) {
|
73 |
|
74 | set(argv);
|
75 | } else {
|
76 | get(argv);
|
77 | }
|
78 | }
|
79 |
|
80 | module.exports = {
|
81 | command: 'config [value]',
|
82 | describe: 'View or set config variables',
|
83 | aliases: ['configure', 'cfg'],
|
84 | builder,
|
85 | handler
|
86 | };
|