From e45c528a3fd8a0b0c279885776e57dbf7c2e57ca Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Fri, 14 Oct 2016 23:20:55 +0200 Subject: [PATCH] init: iniatilize a default wallet --- bin/mpw-config | 3 ++- lib/mpw/cli.rb | 38 ++++++++++++++++++++++---------------- lib/mpw/mpw.rb | 9 ++++++--- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/bin/mpw-config b/bin/mpw-config index 41223c6..d29a93c 100644 --- a/bin/mpw-config +++ b/bin/mpw-config @@ -60,11 +60,12 @@ OptionParser.new do |opts| end.parse! config = MPW::Config.new(options[:config]) -cli = MPW::Cli.new(config, options[:clipboard], options[:sync], options[:otp]) +cli = MPW::Cli.new(config, nil, nil, nil) if not options[:init].nil? cli.setup(options) cli.setup_gpg_key(options[:gpg_key]) if not config.check_gpg_key? + cli.setup_wallet_config exit 0 end diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 0547ac4..2f9ad31 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -89,9 +89,11 @@ class Cli raise I18n.t('form.setup_gpg_key.error_password') end + @password = password.to_s + puts I18n.t('form.setup_gpg_key.wait') - @config.setup_gpg_key(password.to_s, gpg_key) + @config.setup_gpg_key(@password, gpg_key) puts "#{I18n.t('form.setup_gpg_key.valid')}".green rescue Exception => e @@ -100,23 +102,27 @@ class Cli end # Setup wallet config for sync - def setup_wallet_config - config = {} - config['sync'] = {} + # @args: wallet -> the wallet name + def setup_wallet_config(wallet = nil) + #config = {} + #config['sync'] = {} - puts I18n.t('form.setup_wallet.title') - puts '--------------------' - config['sync']['type'] = ask(I18n.t('form.setup_wallet.sync_type')).to_s + #puts '--------------------' + #config['sync']['type'] = ask(I18n.t('form.setup_wallet.sync_type')).to_s - if ['ftp', 'ssh'].include?(config['sync']['type'].downcase) - config['sync']['host'] = ask(I18n.t('form.setup_wallet.sync_host')).to_s - config['sync']['port'] = ask(I18n.t('form.setup_wallet.sync_port')).to_s - config['sync']['user'] = ask(I18n.t('form.setup_wallet.sync_user')).to_s - config['sync']['password'] = ask(I18n.t('form.setup_wallet.sync_pwd')).to_s - config['sync']['path'] = ask(I18n.t('form.setup_wallet.sync_path')).to_s - end + #if ['ftp', 'ssh'].include?(config['sync']['type'].downcase) + # config['sync']['host'] = ask(I18n.t('form.setup_wallet.sync_host')).to_s + # config['sync']['port'] = ask(I18n.t('form.setup_wallet.sync_port')).to_s + # config['sync']['user'] = ask(I18n.t('form.setup_wallet.sync_user')).to_s + # config['sync']['password'] = ask(I18n.t('form.setup_wallet.sync_pwd')).to_s + # config['sync']['path'] = ask(I18n.t('form.setup_wallet.sync_path')).to_s + #end - @mpw.set_config(config) + wallet_file = wallet.nil? ? "#{@config.wallet_dir}/default.mpw" : "#{@config.wallet_dir}/#{wallet}.mpw" + + @mpw = MPW.new(@config.key, wallet_file, @password, @config.gpg_exe) + @mpw.read_data + @mpw.set_config @mpw.write_data puts "#{I18n.t('form.setup_wallet.valid')}".green @@ -384,7 +390,7 @@ class Cli @mpw.sync(true) if @sync puts "#{I18n.t('form.add_item.valid')}".green - rescue Exception => e + #rescue Exception => e puts "#{I18n.t('display.error')} #13: #{e}".red end diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 6c3fc00..f4e8ce3 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -112,7 +112,7 @@ class MPW data.merge!(item.id => {'id' => item.id, 'name' => item.name, - 'group' => item.group, + 'group' => item.group, 'host' => item.host, 'protocol' => item.protocol, 'user' => item.user, @@ -158,7 +158,7 @@ class MPW File.rename(tmp_file, @wallet_file) rescue Exception => e - File.unlink(tmp_file) + File.unlink(tmp_file) if File.exist?(tmp_file) raise "#{I18n.t('error.mpw_file.write_data')}\n#{e}" end @@ -211,9 +211,12 @@ class MPW # Set config # args: config -> a hash with config options - def set_config(config) + def set_config(config=nil) + @config = {} if @config.nil? @config['sync'] = {} if @config['sync'].nil? + return if config.to_s.empty? + @config['sync']['type'] = config['sync']['type'] @config['sync']['host'] = config['sync']['host'] @config['sync']['port'] = config['sync']['port']