From c841123ac0549f0ab6310da55623dd470a48944f Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Wed, 22 Feb 2017 21:51:06 +0100 Subject: [PATCH] feat: add option to set default wallet --- bin/mpw-config | 7 ++++++- i18n/en.yml | 3 +++ i18n/fr.yml | 3 +++ lib/mpw/cli.rb | 19 +++++++++++++------ lib/mpw/config.rb | 41 +++++++++++++++++++---------------------- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/bin/mpw-config b/bin/mpw-config index fb6b372..74309ce 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -34,6 +34,10 @@ OptionParser.new do |opts| options[:config] = config end + opts.on('-d', '--default-wallet NAME', I18n.t('option.default_wallet')) do |default_wallet| + values[:default_wallet] = default_wallet + end + opts.on('-g', '--gpg-exe PATH', I18n.t('option.gpg_exe')) do |gpg_exe| values[:gpg_exe] = gpg_exe end @@ -64,12 +68,13 @@ end.parse! config = MPW::Config.new(options[:config]) cli = MPW::Cli.new(config, nil) -if not options[:init].nil? +if options.has_key?(:init) cli.setup(values) cli.load_config cli.get_wallet cli.setup_gpg_key(values[:gpg_key]) cli.setup_wallet_config else + cli.load_config cli.set_config(values) end diff --git a/i18n/en.yml b/i18n/en.yml index 536a5e4..6dafacf 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -54,6 +54,7 @@ en: alpha: "Use letter to generate a password" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" + default_wallet: "Specify the default wallet to use" delete_gpg_key: "Delete the wallet's share with an other GPG key" export: "Export a wallet in an yaml file" file_export: "Specify the file where export data" @@ -128,6 +129,8 @@ en: file_not_exist: "The import file doesn't exist!" valid: "The import is succesfull!" not_valid: "No data to import!" + set_config: + valid: "The config file has been edited!" setup_config: title: "Setup a new config file" lang: "Choose your language (en, fr, ...) [default=%{lang}]: " diff --git a/i18n/fr.yml b/i18n/fr.yml index 09f060a..a20e58f 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -54,6 +54,7 @@ fr: alpha: "Utilise des lettres dans la génération d'un mot de passe" config: "Spécifie le fichier de configuration à utiliser" clipboard: "Désactive la fonction presse papier" + default_wallet: "Spécifie le porte-feuille à utiliser par défaut" delete_gpg_key: "Supprime le partage le portefeuille avec une autre clé GPG" export: "Exporte un portefeuille dans un fichier yaml" file_export: "Spécifie le fichier où exporter les données" @@ -128,6 +129,8 @@ fr: file_not_exist: "Le fichier d'import n'existe pas" valid: "L'import est un succès!" not_valid: "Aucune donnée à importer!" + set_config: + valid: "Le fichier de configuration a bien été modifié!" setup_config: title: "Création d'un nouveau fichier de configuration" lang: "Choisissez votre langue (en, fr, ...) [défaut=%{lang}]: " diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 94fa9c3..0a5dcde 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -40,12 +40,15 @@ class Cli # Change a parameter int the config after init # @args: options -> param to change def set_config(options) - gpg_key = options[:gpg_key] || @config.key - lang = options[:lang] || @config.lang - wallet_dir = options[:wallet_dir] || @config.wallet_dir - gpg_exe = options[:gpg_exe] || @config.gpg_exe + gpg_key = options[:gpg_key] || @config.key + lang = options[:lang] || @config.lang + wallet_dir = options[:wallet_dir] || @config.wallet_dir + default_wallet = options[:default_wallet] || @config.default_wallet + gpg_exe = options[:gpg_exe] || @config.gpg_exe - @config.setup(gpg_key, lang, wallet_dir, gpg_exe) + @config.setup(gpg_key, lang, wallet_dir, default_wallet, gpg_exe) + + puts "#{I18n.t('form.set_config.valid')}".green rescue Exception => e puts "#{I18n.t('display.error')} #15: #{e}".red exit 2 @@ -341,7 +344,9 @@ class Cli def list_wallet wallets = [] Dir.glob("#{@config.wallet_dir}/*.mpw").each do |f| - wallets << File.basename(f, '.mpw') + wallet = File.basename(f, '.mpw') + wallet += ' *'.green if wallet == @config.default_wallet + wallets << wallet end table_list('wallets', wallets) @@ -355,6 +360,8 @@ class Cli if wallets.length == 1 @wallet_file = wallets[0] + elsif not @config.default_wallet.to_s.empty? + @wallet_file = "#{@config.wallet_dir}/#{@config.default_wallet}.mpw" else @wallet_file = "#{@config.wallet_dir}/default.mpw" end diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index 867ff35..c1e883c 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -29,6 +29,7 @@ class Config attr_accessor :key attr_accessor :lang attr_accessor :config_dir + attr_accessor :default_wallet attr_accessor :wallet_dir attr_accessor :gpg_exe @@ -45,31 +46,28 @@ class Config @config_dir = "#{Dir.home}/.config/mpw" end - if @config_file.nil? or @config_file.empty? - @config_file = "#{@config_dir}/mpw.cfg" - end + @config_file = "#{@config_dir}/mpw.cfg" if @config_file.nil? or @config_file.empty? end # Create a new config file # @args: key -> the gpg key to encrypt # lang -> the software language - # wallet_dir -> the directory where are the wallets password - # gpg_exe -> the path of gpg executable + # wallet_dir -> the directory where are the wallets password + # default_wallet -> the default wallet + # gpg_exe -> the path of gpg executable # @rtrn: true if le config file is create - def setup(key, lang, wallet_dir, gpg_exe) + def setup(key, lang, wallet_dir, default_wallet, gpg_exe) if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ raise I18n.t('error.config.key_bad_format') end - if wallet_dir.to_s.empty? - wallet_dir = "#{@config_dir}/wallets" - end - - config = { 'key' => key, - 'lang' => lang, - 'wallet_dir' => wallet_dir, - 'gpg_exe' => gpg_exe, - } + wallet_dir = "#{@config_dir}/wallets" if wallet_dir.to_s.empty? + config = { 'key' => key, + 'lang' => lang, + 'wallet_dir' => wallet_dir, + 'default_wallet' => default_wallet, + 'gpg_exe' => gpg_exe, + } FileUtils.mkdir_p(@config_dir, mode: 0700) FileUtils.mkdir_p(wallet_dir, mode: 0700) @@ -77,7 +75,6 @@ class Config File.open(@config_file, 'w') do |file| file << config.to_yaml end - rescue Exception => e raise "#{I18n.t('error.config.write')}\n#{e}" end @@ -116,16 +113,16 @@ class Config # Load the config file def load_config - config = YAML::load_file(@config_file) - @key = config['key'] - @lang = config['lang'] - @wallet_dir = config['wallet_dir'] - @gpg_exe = config['gpg_exe'] + config = YAML::load_file(@config_file) + @key = config['key'] + @lang = config['lang'] + @wallet_dir = config['wallet_dir'] + @default_wallet = config['default_wallet'] + @gpg_exe = config['gpg_exe'] raise if @key.empty? or @wallet_dir.empty? I18n.locale = @lang.to_sym - rescue Exception => e raise "#{I18n.t('error.config.load')}\n#{e}" end