From e068a2758c867e7e634fe21a67887bf5279592bd Mon Sep 17 00:00:00 2001 From: nishiki Date: Sat, 9 Jul 2016 14:06:49 +0200 Subject: [PATCH] add login and password in clipboard --- Gemfile | 1 + README.md | 1 + bin/mpw | 19 ++++++++++++------- i18n/en.yml | 5 +++++ i18n/fr.yml | 5 +++++ lib/mpw/ui/cli.rb | 33 +++++++++++++++++++++++++++++---- mpw.gemspec | 1 + 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index ef153b8..b897ea2 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,4 @@ gem 'gpgme' gem 'colorize' gem 'net-ssh' gem 'net-scp' +gem 'clipboard' diff --git a/README.md b/README.md index 6532a53..e3ca84c 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ MPW can synchronize your password with SSH or FTP. This program work with ruby >= 2.0 * install ruby and rubygems on your computer +* install xclip * gem install mpw # How to use diff --git a/bin/mpw b/bin/mpw index ce73305..52033a3 100755 --- a/bin/mpw +++ b/bin/mpw @@ -48,12 +48,13 @@ I18n.locale = lang.to_sym # Options # --------------------------------------------------------- # -options = {} -options[:force] = false -options[:sync] = true -options[:group] = nil -options[:config] = nil -options[:wallet] = nil +options = {} +options[:force] = false +options[:sync] = true +options[:clipboard] = true +options[:group] = nil +options[:config] = nil +options[:wallet] = nil OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw [options]" @@ -71,6 +72,10 @@ OptionParser.new do |opts| options[:config] = config end + opts.on('-C', '--no-clipboard', I18n.t('option.clipboard')) do + options[:clipboard] = false + end + opts.on('-d', '--delete', I18n.t('option.remove')) do options[:delete] = true end @@ -144,7 +149,7 @@ end.parse! begin config = MPW::Config.new(options[:config]) - cli = MPW::Cli.new(config, options[:sync]) + cli = MPW::Cli.new(config, options[:clipboard], options[:sync]) # Setup a new config if not options[:setup].nil? diff --git a/i18n/en.yml b/i18n/en.yml index e6b7a78..5426128 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -38,6 +38,7 @@ en: option: add: "Add an item or key" config: "Specify the configuration file to use" + clipboard: "Disable the clipboard feature" export: "Export a wallet in an yaml file" file: "Specify a file, to use with the options [--import | --export | --add]" force: "No ask to confirm when you delete an item" @@ -72,6 +73,10 @@ en: port: "Enter the connection port (optional): " comment: "Enter a comment (optional): " valid: "Item has been added!" + clipboard: + clean: "The clipboard has been cleaned." + login: "The login has been copied in clipboard, press any key to continue." + password: "The password has been copied in clipboard for 30s!" delete_key: valid: "Key has been deleted!" delete_item: diff --git a/i18n/fr.yml b/i18n/fr.yml index 155a177..f371eef 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -38,6 +38,7 @@ fr: option: add: "Ajoute un élément ou une clé" config: "Spécifie le fichier de configuration à utiliser" + clipboard: "Désactive la fonction presse papier" export: "Exporte un portefeuille dans un fichier yaml" file: "Spécifie un fichier, à utiliser avec les options [--import | --export | --add]" force: "Ne demande pas de confirmation pour la suppression d'un élément" @@ -72,6 +73,10 @@ fr: port: "Entrez le port de connexion (optionnel): " comment: "Entrez un commentaire (optionnel): " valid: "L'élément a bien été ajouté!" + clipboard: + clean: "Le presse papier a été nettoyé." + login: "L'identifiant a été copié dans le presse papier, pressez n'importe quelle touche pour continuer." + password: "Le mot de passe a été copié dans le presse papier pour 30s!" delete_key: valid: "La clé a bien été supprimée!" delete_item: diff --git a/lib/mpw/ui/cli.rb b/lib/mpw/ui/cli.rb index bed2d31..d2980cf 100644 --- a/lib/mpw/ui/cli.rb +++ b/lib/mpw/ui/cli.rb @@ -20,6 +20,7 @@ require 'readline' require 'i18n' require 'colorize' require 'highline/import' +require 'clipboard' #TODO require "#{APP_ROOT}/../lib/mpw/item.rb" @@ -31,9 +32,10 @@ class Cli # Constructor # @args: config -> the config # sync -> boolean for sync or not - def initialize(config, sync=true) - @config = config - @sync = sync + def initialize(config, clipboard=true, sync=true) + @config = config + @clipboard = clipboard + @sync = sync end # Create a new config file @@ -184,11 +186,34 @@ class Cli print "#{I18n.t('display.login')}: ".cyan puts item.user print "#{I18n.t('display.password')}: ".cyan - puts @mpw.get_password(item.id) + if @clipboard + puts '***********' + else + puts @mpw.get_password(item.id) + end print "#{I18n.t('display.port')}: ".cyan puts item.port print "#{I18n.t('display.comment')}: ".cyan puts item.comment + + clipboard(item) if @clipboard + end + + # Copy in clipboard the login and password + def clipboard(item) + Clipboard.copy(item.user) + print "\n#{I18n.t('form.clipboard.login')}".green + gets + + Clipboard.copy(@mpw.get_password(item.id)) + puts I18n.t('form.clipboard.password').yellow + + sleep(30) + + Clipboard.clear + puts I18n.t('form.clipboard.clean').green + rescue SystemExit, Interrupt + Clipboard.clear end # Display the wallet diff --git a/mpw.gemspec b/mpw.gemspec index 31b851f..bf6f3db 100644 --- a/mpw.gemspec +++ b/mpw.gemspec @@ -24,4 +24,5 @@ Gem::Specification.new do |spec| spec.add_dependency "colorize" spec.add_dependency "net-ssh" spec.add_dependency "net-scp" + spec.add_dependency "clipboard" end