diff --git a/bin/mpw b/bin/mpw index cb3bdf4..a185b49 100755 --- a/bin/mpw +++ b/bin/mpw @@ -66,7 +66,7 @@ OptionParser.new do |opts| options[:export] = true end - opts.on('-f', '--file', I18n.t('option.file')) do |file| + opts.on('-f', '--file FILE', I18n.t('option.file')) do |file| options[:file] = file end @@ -163,7 +163,7 @@ elsif not options[:add].nil? and options[:key].nil? # Add a new public key in wallet elsif not options[:add].nil? and not options[:key].nil? - cli.add_key(options[:key]) + cli.add_key(options[:key], options[:file]) # Delete a public key in wallet elsif not options[:delete].nil? and not options[:key].nil? diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index c66fa3b..2c988c5 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -138,8 +138,15 @@ class MPW # Add a public key # args: key -> new public key - def add_key(key) - data = GPGME::Key.export(key, armor: true).read + # file -> public gpg file to import + def add_key(key, file=nil) + if not file.nil? and File.exists?(file) + data = File.open(file).read + GPGME::Key.import(data, armor: true) + puts GPGME::Key.find(key)[0].trust + else + data = GPGME::Key.export(key, armor: true).read + end if data.to_s.empty? raise I18n.t('error.export_key') @@ -298,7 +305,7 @@ class MPW private def encrypt(data) recipients = [] - crypto = GPGME::Crypto.new(armor: true) + crypto = GPGME::Crypto.new(armor: true, always_trust: true) @keys.each_key do |key| recipients.push(key) diff --git a/lib/mpw/ui/cli.rb b/lib/mpw/ui/cli.rb index 682747f..a3b846b 100644 --- a/lib/mpw/ui/cli.rb +++ b/lib/mpw/ui/cli.rb @@ -188,8 +188,9 @@ class Cli # Add a new public key # args: key -> the key name to add - def add_key(key) - @mpw.add_key(key) + # file -> gpg public file to import + def add_key(key, file=nil) + @mpw.add_key(key, file) @mpw.write_data puts "#{I18n.t('key.add.valid')}".green