diff --git a/bin/mpw b/bin/mpw index 6d92958..0663c6a 100755 --- a/bin/mpw +++ b/bin/mpw @@ -92,6 +92,10 @@ OptionParser.new do |opts| options[:import] = file end + opts.on('-k', '--key KEY', I18n.t('option.import')) do |key| + options[:key] = key + end + opts.on('-N', '--no-sync', I18n.t('option.no_sync')) do options[:sync] = false end @@ -150,9 +154,13 @@ elsif not options[:update].nil? and not options[:id].nil? cli.update(options[:id]) # Add a new item -elsif not options[:add].nil? +elsif not options[:add].nil? and options[:key].nil? cli.add +# Add a new public key in wallet +elsif not options[:add].nil? and not options[:key].nil? + cli.add_key(options[:key]) + # Export elsif not options[:export].nil? cli.export(options[:export]) diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index 230ff53..127da7f 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -112,7 +112,7 @@ class MPW @keys.each do |id, key| tar.add_file_simple("wallet/keys/#{id}.pub", 0400, key.length) do |io| - io.write(password) + io.write(key) end end end @@ -131,6 +131,18 @@ class MPW @passwords[id] = encrypt(password) end + # Add public key + # args: key -> new public key + def add_key(key) + data = GPGME::Key.export(key).read + + if data.to_s.empty? + raise I18n.t('error.export_key') + end + + @keys[key] = data + end + # TODO def check_config if false @@ -150,7 +162,6 @@ class MPW end end - # Search in some csv data # @args: options -> a hash with paramaters # @rtrn: a list with the resultat of the search diff --git a/lib/mpw/ui/cli.rb b/lib/mpw/ui/cli.rb index 57063de..4d36cd8 100644 --- a/lib/mpw/ui/cli.rb +++ b/lib/mpw/ui/cli.rb @@ -157,7 +157,6 @@ class Cli def get_wallet(wallet=nil) if wallet.to_s.empty? wallets = Dir.glob("#{@config.wallet_dir}/*.mpw") - puts wallets case wallets.length when 0 @@ -186,6 +185,14 @@ class Cli end end + # Add new public key + def add_key(key) + @mpw.add_key(key) + @mpw.write_data + rescue Exception => e + puts "#{I18n.t('display.error')} #13: #{e}".red + end + # Form to add a new item def add options = {}