1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/CliClient/app/command-config.js

67 lines
1.7 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 20:12:14 +02:00
const Setting = require('lib/models/Setting.js');
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage() {
2017-07-28 20:13:07 +02:00
return 'config [name] [value]';
2017-07-10 22:03:46 +02:00
}
description() {
2017-07-24 20:58:11 +02: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 22:03:46 +02:00
}
2017-07-24 21:47:01 +02:00
options() {
return [
2017-08-22 19:57:35 +02:00
['-v, --verbose', _('Also displays unset and hidden config variables.')],
2017-07-24 21:47:01 +02:00
];
}
2017-07-10 22:03:46 +02:00
async action(args) {
2017-08-22 19:57:35 +02:00
const verbose = args.options.verbose;
2017-07-24 20:58:11 +02:00
const renderKeyValue = (name) => {
let value = Setting.value(name);
if (typeof value === 'object' || Array.isArray(value)) value = JSON.stringify(value);
2017-07-24 20:58:11 +02:00
if (Setting.isEnum(name)) {
2017-08-22 19:57:35 +02:00
return _('%s = %s (%s)', name, value, Setting.enumOptionsDoc(name));
2017-07-24 20:58:11 +02:00
} else {
return _('%s = %s', name, value);
}
}
2017-07-10 22:03:46 +02:00
if (!args.name && !args.value) {
2017-08-22 19:57:35 +02:00
let keys = Setting.keys(!verbose, 'cli');
2017-11-12 02:44:26 +02:00
keys.sort();
2017-07-10 22:03:46 +02:00
for (let i = 0; i < keys.length; i++) {
2017-08-22 19:57:35 +02:00
const value = Setting.value(keys[i]);
if (!verbose && !value) continue;
2017-10-07 18:30:27 +02:00
this.stdout(renderKeyValue(keys[i]));
2017-07-10 22:03:46 +02:00
}
app().gui().showConsole();
app().gui().maximizeConsole();
2017-07-10 22:03:46 +02:00
return;
}
if (args.name && !args.value) {
2017-10-07 18:30:27 +02:00
this.stdout(renderKeyValue(args.name));
app().gui().showConsole();
app().gui().maximizeConsole();
2017-07-10 22:03:46 +02:00
return;
}
Setting.setValue(args.name, args.value);
2017-07-18 20:49:47 +02:00
if (args.name == 'locale') {
setLocale(Setting.value('locale'));
app().onLocaleChanged();
}
2017-07-10 22:03:46 +02:00
await Setting.saveAll();
}
}
module.exports = Command;