joplin/CliClient/app/command-config.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

const { BaseCommand } = require('./base-command.js');
const { _, setLocale } = require('lib/locale.js');
const { app } = require('./app.js');
2017-12-14 18:12:14 +00:00
const Setting = require('lib/models/Setting.js');
2017-07-10 20:03:46 +00:00
class Command extends BaseCommand {
usage() {
2017-07-28 18:13:07 +00:00
return 'config [name] [value]';
2017-07-10 20:03:46 +00:00
}
description() {
2017-07-24 18:58:11 +00:00
return _("Gets or sets a config value. If [value] is not provided, it will show the value of [name]. If neither [name] nor [value] is provided, it will list the current configuration.");
2017-07-10 20:03:46 +00:00
}
2017-07-24 19:47:01 +00:00
options() {
return [
2017-08-22 17:57:35 +00:00
['-v, --verbose', _('Also displays unset and hidden config variables.')],
2017-07-24 19:47:01 +00:00
];
}
2017-07-10 20:03:46 +00:00
async action(args) {
2017-08-22 17:57:35 +00:00
const verbose = args.options.verbose;
2017-07-24 18:58:11 +00:00
const renderKeyValue = (name) => {
const value = Setting.value(name);
if (Setting.isEnum(name)) {
2017-08-22 17:57:35 +00:00
return _('%s = %s (%s)', name, value, Setting.enumOptionsDoc(name));
2017-07-24 18:58:11 +00:00
} else {
return _('%s = %s', name, value);
}
}
2017-07-10 20:03:46 +00:00
if (!args.name && !args.value) {
2017-08-22 17:57:35 +00:00
let keys = Setting.keys(!verbose, 'cli');
2017-11-12 00:44:26 +00:00
keys.sort();
2017-07-10 20:03:46 +00:00
for (let i = 0; i < keys.length; i++) {
2017-08-22 17:57:35 +00:00
const value = Setting.value(keys[i]);
if (!verbose && !value) continue;
2017-10-07 16:30:27 +00:00
this.stdout(renderKeyValue(keys[i]));
2017-07-10 20:03:46 +00:00
}
app().gui().showConsole();
app().gui().maximizeConsole();
2017-07-10 20:03:46 +00:00
return;
}
if (args.name && !args.value) {
2017-10-07 16:30:27 +00:00
this.stdout(renderKeyValue(args.name));
app().gui().showConsole();
app().gui().maximizeConsole();
2017-07-10 20:03:46 +00:00
return;
}
Setting.setValue(args.name, args.value);
2017-07-18 18:49:47 +00:00
if (args.name == 'locale') {
setLocale(Setting.value('locale'));
app().onLocaleChanged();
}
2017-07-10 20:03:46 +00:00
await Setting.saveAll();
}
}
module.exports = Command;