Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a8ec3d

Browse files
[Config] add config command.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 33f6808 commit 9a8ec3d

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

‎docs/advanced.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The local cache folder (`.lc/`) is in your home directory, e.g.
4848
problems.json # problems list
4949
1.two-sum.algorithms.json # specific problem info
5050

51-
Normally you don't need dig into the folder to manipulate those files. Use [cache command](https://skygragon.github.io/leetcode-cli/commands#cache) instead.
51+
**NOTE: Normally you don't need dig into the folder to manipulate those files. Use [cache command](https://skygragon.github.io/leetcode-cli/commands#cache) instead.**
5252

5353
# Configuration
5454

@@ -87,6 +87,8 @@ Here are some useful settings:
8787
* `icon:theme` to set icon them used in output.
8888
* `plugins` to config each installed plugins, see [Plugins](#plugins).
8989

90+
**NOTE: Normally you don't need dig into the folder to manipulate those files. Use [config command](https://skygragon.github.io/leetcode-cli/commands#config) instead.**
91+
9092
*Example*
9193

9294
Config for `github.js` and `cpp.lint.js` plugins:

‎docs/commands.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ title: Commands Help
55

66
* [help](#help)
77
* [cache](#cache)
8+
* [config](#config)
89
* [list](#list)
910
* [plugin](#plugin)
1011
* [show](#show)
@@ -73,6 +74,31 @@ Delete cache for problem 537:
7374

7475
$ leetcode cache -d 537
7576

77+
# config
78+
79+
Manage user config (~/.lcconfig).
80+
81+
* `leetcode config` to show all user modified configs.
82+
* `-a` to show all user configs (includes default ones).
83+
* `leetcode config <key>` to show config item by key.
84+
* `leetcode config <key> <value>` to update config by key.
85+
* `-d` to delete config item by key.
86+
87+
*Examples*
88+
89+
Set config item:
90+
91+
$ leetcode config color:enable false
92+
93+
**NOTE: the key is using colon ":" as the separator, not dot "."!**
94+
95+
Show config item:
96+
97+
$ leetcode config color
98+
{
99+
"enable": true
100+
}
101+
76102
# list
77103

78104
Navigate the problems.

‎lib/commands/config.js‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var _ = require('underscore');
2+
var nconf = require('nconf');
3+
4+
var h = require('../helper');
5+
var config = require('../config');
6+
var log = require('../log');
7+
var session = require('../session');
8+
9+
var cmd = {
10+
command: 'config [key] [value]',
11+
desc: 'show or set configurations',
12+
builder: {
13+
all: {
14+
alias: 'a',
15+
type: 'boolean',
16+
describe: 'Show all user configuration',
17+
default: false
18+
},
19+
delete: {
20+
alias: 'd',
21+
type: 'boolean',
22+
describe: 'Delete configuration',
23+
default: false
24+
}
25+
}
26+
};
27+
28+
function prettyConfig(cfg) {
29+
return JSON.stringify(cfg, null, 2);
30+
}
31+
32+
function loadConfig(showall) {
33+
var cfg = showall ? config.getAll(true) : nconf.get();
34+
return _.omit(cfg, 'type');
35+
}
36+
37+
function saveConfig() {
38+
require('fs').writeFileSync(h.getConfigFile(), prettyConfig(loadConfig(false)));
39+
}
40+
41+
cmd.handler = function(argv) {
42+
session.argv = argv;
43+
nconf.file('local', h.getConfigFile());
44+
45+
// show all
46+
if (argv.key === undefined)
47+
return log.info(prettyConfig(loadConfig(argv.all)));
48+
49+
var v = nconf.get(argv.key);
50+
51+
// delete
52+
if (argv.delete) {
53+
if (v === undefined) return log.error('Key not found: ' + argv.key);
54+
nconf.clear(argv.key);
55+
return saveConfig();
56+
}
57+
58+
// show
59+
if (argv.value === undefined) {
60+
if (v === undefined) return log.error('Key not found: ' + argv.key);
61+
return log.info(prettyConfig(v));
62+
}
63+
64+
// set
65+
if (argv.value === 'true') argv.value = true;
66+
if (argv.value === 'false') argv.value = false;
67+
nconf.set(argv.key, argv.value);
68+
return saveConfig();
69+
};
70+
71+
module.exports = cmd;

‎lib/config.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ var DEFAULT_CONFIG = {
6868
function Config() {}
6969

7070
Config.prototype.init = function() {
71-
nconf.file(h.getConfigFile())
72-
.defaults(DEFAULT_CONFIG);
71+
nconf.file('local', h.getConfigFile())
72+
.add('global', {type: 'literal', store: DEFAULT_CONFIG})
73+
.defaults({});
7374

7475
var cfg = nconf.get();
76+
nconf.remove('local');
77+
nconf.remove('global');
78+
7579
// HACK: remove old style configs
7680
for (var x in cfg) {
7781
if (x === x.toUpperCase()) delete cfg[x];

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /