2017-11-03 02:09:34 +02:00
const { BaseCommand } = require ( './base-command.js' ) ;
const { _ , setLocale } = require ( 'lib/locale.js' ) ;
const { app } = require ( './app.js' ) ;
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 ) => {
const value = Setting . value ( name ) ;
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
}
2017-10-17 19:18:31 +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 ) ) ;
2017-10-17 19:18:31 +02:00
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 ;