From 6d2a45217c8aeda5594065daaf1d36d6c1bb52dc Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 23 Oct 2016 21:54:46 +0200 Subject: [PATCH] fix load config --- bin/mpw-add | 1 + bin/mpw-config | 1 + bin/mpw-copy | 1 + bin/mpw-delete | 1 + bin/mpw-export | 1 + bin/mpw-import | 1 + bin/mpw-list | 1 + bin/mpw-update | 1 + bin/mpw-wallet | 2 ++ i18n/en.yml | 2 +- i18n/fr.yml | 2 +- lib/mpw/cli.rb | 19 ++++++++++--------- lib/mpw/config.rb | 27 ++++++++++++--------------- 13 files changed, 34 insertions(+), 26 deletions(-) diff --git a/bin/mpw-add b/bin/mpw-add index 542b884..a0e450f 100644 --- a/bin/mpw-add +++ b/bin/mpw-add @@ -50,6 +50,7 @@ end.parse! config = MPW::Config.new(options[:config]) cli = MPW::Cli.new(config, options[:sync]) +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.add(options[:password]) diff --git a/bin/mpw-config b/bin/mpw-config index b0a1863..133f3c4 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -65,6 +65,7 @@ config = MPW::Config.new(options[:config]) cli = MPW::Cli.new(config, nil) if not options[:init].nil? + cli.load_config cli.get_wallet cli.setup(values) cli.setup_gpg_key(options[:init]) diff --git a/bin/mpw-copy b/bin/mpw-copy index b4d770c..e9ae66b 100644 --- a/bin/mpw-copy +++ b/bin/mpw-copy @@ -63,6 +63,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.copy(options[:clipboard], opts) diff --git a/bin/mpw-delete b/bin/mpw-delete index 3dc0cd7..e2d1195 100644 --- a/bin/mpw-delete +++ b/bin/mpw-delete @@ -58,6 +58,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.delete(opts) diff --git a/bin/mpw-export b/bin/mpw-export index 4a174aa..be15bb5 100644 --- a/bin/mpw-export +++ b/bin/mpw-export @@ -62,6 +62,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.export(options[:file], opts) diff --git a/bin/mpw-import b/bin/mpw-import index 8881441..ad52f57 100644 --- a/bin/mpw-import +++ b/bin/mpw-import @@ -54,6 +54,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.import(options[:file]) diff --git a/bin/mpw-list b/bin/mpw-list index 2b503cf..1c729cc 100644 --- a/bin/mpw-list +++ b/bin/mpw-list @@ -58,6 +58,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.list(opts) diff --git a/bin/mpw-update b/bin/mpw-update index 7e6519d..cdd3af1 100644 --- a/bin/mpw-update +++ b/bin/mpw-update @@ -58,6 +58,7 @@ opts = { search: options[:pattern], group: options[:group], } +cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt cli.update(opts) diff --git a/bin/mpw-wallet b/bin/mpw-wallet index 0184d85..8e33426 100644 --- a/bin/mpw-wallet +++ b/bin/mpw-wallet @@ -75,6 +75,8 @@ end.parse! config = MPW::Config.new(options[:config]) cli = MPW::Cli.new(config, options[:sync]) +cli.load_config + if not options[:list].nil? cli.list_wallet else diff --git a/i18n/en.yml b/i18n/en.yml index e88f360..9a964d2 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -3,7 +3,7 @@ en: error: config: write: "Can't write the config file!" - check: "Checkconfig failed!" + load: "Checkconfig failed!" key_bad_format: "The key string isn't in good format!" no_key_public: "You haven't the public key of %{key}!" genkey_gpg: diff --git a/i18n/fr.yml b/i18n/fr.yml index 903cba6..d2cae5b 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -3,7 +3,7 @@ fr: error: config: write: "Impossible d'écrire le fichier de configuration!" - check: "Le fichier de configuration est invalide!" + load: "Le fichier de configuration est invalide!" key_bad_format: "La clé GPG est invalide!" no_key_public: "Vous ne possédez pas la clé publique de %{key}!" genkey_gpg: diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index b0161a1..b25cd18 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -40,8 +40,6 @@ class Cli # Change a parameter int the config after init # @args: options -> param to change def set_config(options) - raise I18n.t('error.config.check') if not @config.is_valid? - gpg_key = options[:gpg_key] || @config.key lang = options[:lang] || @config.lang wallet_dir = options[:wallet_dir] || @config.wallet_dir @@ -56,15 +54,13 @@ class Cli # Create a new config file # @args: options -> set param def setup(options) - @config.is_valid? - lang = options[:lang] || Locale::Tag.parse(ENV['LANG']).to_simple.to_s[0..1] I18n.locale = lang.to_sym @config.setup(options[:gpg_key], lang, options[:wallet_dir], options[:gpg_exe]) - raise I18n.t('error.config.check') if not @config.is_valid? + load_config puts "#{I18n.t('form.setup_config.valid')}".green rescue Exception => e @@ -116,6 +112,15 @@ class Cli exit 2 end + # Load config + def load_config + @config.load_config + + rescue Exception => e + puts "#{I18n.t('display.error')} #10: #{e}".red + exit 2 + end + # Request the GPG password and decrypt the file def decrypt if not defined?(@mpw) @@ -305,8 +310,6 @@ class Cli # List all wallets def list_wallet - @config.is_valid? - wallets = Dir.glob("#{@config.wallet_dir}/*.mpw") wallets.each do |wallet| @@ -317,8 +320,6 @@ class Cli # Display the wallet # @args: wallet -> the wallet name def get_wallet(wallet=nil) - @config.is_valid? - if wallet.to_s.empty? wallets = Dir.glob("#{@config.wallet_dir}/*.mpw") diff --git a/lib/mpw/config.rb b/lib/mpw/config.rb index 148ecf1..2c1c496 100644 --- a/lib/mpw/config.rb +++ b/lib/mpw/config.rb @@ -65,11 +65,10 @@ class Config wallet_dir = "#{@config_dir}/wallets" end - config = {'config' => {'key' => key, - 'lang' => lang, - 'wallet_dir' => wallet_dir, - 'gpg_exe' => gpg_exe, - } + config = { 'key' => key, + 'lang' => lang, + 'wallet_dir' => wallet_dir, + 'gpg_exe' => gpg_exe, } FileUtils.mkdir_p(wallet_dir, mode: 0700) @@ -113,22 +112,20 @@ class Config raise "#{I18n.t('error.config.genkey_gpg.exception')}\n#{e}" end - # Check the config file - # @rtrn: true if the config file is correct - def is_valid? + # Load the config file + def load_config config = YAML::load_file(@config_file) - @key = config['config']['key'] - @lang = config['config']['lang'] - @wallet_dir = config['config']['wallet_dir'] - @gpg_exe = config['config']['gpg_exe'] + @key = config['key'] + @lang = config['lang'] + @wallet_dir = config['wallet_dir'] + @gpg_exe = config['gpg_exe'] raise if @key.empty? or @wallet_dir.empty? I18n.locale = @lang.to_sym - return true - rescue - return false + rescue Exception => e + raise "#{I18n.t('error.config.load')}\n#{e}" end # Check if private key exist