From 0fdefdf4332d33e88bcfc1027addd159e70e4c45 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Thu, 13 Apr 2017 22:35:57 +0200 Subject: [PATCH] feat: add options to edit or update an item --- bin/mpw-add | 38 +++++++++++++++++++++++++++++++++-- bin/mpw-update | 42 +++++++++++++++++++++++++++++++++++---- i18n/en.yml | 8 ++++++++ i18n/fr.yml | 8 ++++++++ lib/mpw/cli.rb | 26 ++++++++++++++---------- templates/add_form.erb | 16 +++++++-------- templates/update_form.erb | 14 ++++++------- 7 files changed, 120 insertions(+), 32 deletions(-) diff --git a/bin/mpw-add b/bin/mpw-add index 6376326..20eaf7b 100644 --- a/bin/mpw-add +++ b/bin/mpw-add @@ -24,7 +24,9 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} +values = {} +options = {} +options[:text_editor] = true OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw add [options]" @@ -33,15 +35,47 @@ OptionParser.new do |opts| options[:config] = config end + opts.on('-C', '--comment COMMENT', I18n.t('option.comment')) do |comment| + values[:comment] = comment + end + + opts.on('-G', '--group NAME', I18n.t('option.new_group')) do |group| + values[:group] = group + end + opts.on('-h', '--help', I18n.t('option.help')) do puts opts exit 0 end + opts.on('-H', '--host HOST', I18n.t('option.host')) do |host| + values[:host] = host + end + + opts.on('-o', '--otp-code CODE', I18n.t('option.otp_code')) do |otp| + values[:otp_key] = otp + end + + opts.on('-O', '--protocol PROTOCOL', I18n.t('option.protocol')) do |protocol| + values[:protocol] = protocol + end + + opts.on('-P', '--port NUMBER', I18n.t('option.port')) do |port| + values[:port] = port + end + opts.on('-r', '--random', I18n.t('option.random_password')) do options[:password] = true end + opts.on('-t', '--text-editor', I18n.t('option.text_editor')) do + options[:text_editor] = true + end + + opts.on('-u', '--user USER', I18n.t('option.user')) do |user| + values[:user] = user + end + opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end @@ -53,4 +87,4 @@ cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt -cli.add(options[:password]) +cli.add(options[:password], options[:text_editor], values) diff --git a/bin/mpw-update b/bin/mpw-update index c258330..d887ce3 100644 --- a/bin/mpw-update +++ b/bin/mpw-update @@ -24,8 +24,10 @@ require 'mpw/cli' # Options # --------------------------------------------------------- # -options = {} -values = {} +values = {} +search = {} +options = {} +options[:text_editor] = false OptionParser.new do |opts| opts.banner = "#{I18n.t('option.usage')}: mpw update [options]" @@ -34,7 +36,15 @@ OptionParser.new do |opts| options[:config] = config end + opts.on('-C', '--comment COMMENT', I18n.t('option.comment')) do |comment| + values[:comment] = comment + end + opts.on('-g', '--group NAME', I18n.t('option.group')) do |group| + search[:group] = group + end + + opts.on('-G', '--new-group NAME', I18n.t('option.new_group')) do |group| values[:group] = group end @@ -43,14 +53,38 @@ OptionParser.new do |opts| exit 0 end + opts.on('-H', '--host HOST', I18n.t('option.host')) do |host| + values[:host] = host + end + + opts.on('-o', '--otp-code CODE', I18n.t('option.otp_code')) do |otp| + values[:otp_key] = otp + end + + opts.on('-O', '--protocol PROTOCOL', I18n.t('option.protocol')) do |protocol| + values[:protocol] = protocol + end + opts.on('-p', '--pattern PATTERN', I18n.t('option.pattern')) do |pattern| - values[:pattern] = pattern + search[:pattern] = pattern + end + + opts.on('-P', '--port NUMBER', I18n.t('option.port')) do |port| + values[:port] = port end opts.on('-r', '--random', I18n.t('option.random_password')) do options[:password] = true end + opts.on('-t', '--text-editor', I18n.t('option.text_editor')) do + options[:text_editor] = true + end + + opts.on('-u', '--user USER', I18n.t('option.user')) do |user| + values[:user] = user + end + opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet| options[:wallet] = wallet end @@ -62,4 +96,4 @@ cli = MPW::Cli.new(config) cli.load_config cli.get_wallet(options[:wallet]) cli.decrypt -cli.update(options[:password], values) +cli.update(options[:password], options[:text_editor], search, values) diff --git a/i18n/en.yml b/i18n/en.yml index 5ed50c7..402facd 100644 --- a/i18n/en.yml +++ b/i18n/en.yml @@ -43,6 +43,7 @@ en: add: "Add an item or key" add_gpg_key: "Share the wallet with an other GPG key" alpha: "Use letter to generate a password" + comment: "Specify a comment" config: "Specify the configuration file to use" clipboard: "Disable the clipboard feature" default_path: "Move the wallet in default directory" @@ -61,6 +62,7 @@ en: gpg_key: "Specify a GPG key (ex: user@example.com)" group: "Search the items with specified group" help: "Show this help message" + host: "Specify a host or ip" init: "Initialize mpw" import: "Import item since a yaml file" key: "Specify the key name" @@ -68,17 +70,23 @@ en: length: "Size of the password" list: "List the wallets" list_keys: "List the GPG keys in wallet" + new_group: "Specify the group for the item" numeric: "Use number to generate a password" + otp_code: "Set an otp key" path: "Move the wallet in new specify directory" pattern: "Given search pattern" pinmode: "Enable pinentry mode (available with gpg >= 2.1)" + port: "Set a port of connexion" + protocol: "Set a protocol of connexion" random_password: "Generate a random password" setup: "Create a new configuration file" setup_wallet: "Create a new configuration file for a wallet" special_chars: "Use special char to generate a password" show: "Search and show the items" show_all: "List all items" + text_editor: "Use text editor to edit the item" usage: "Usage" + user: "Set an user" wallet: "Specify a wallet to use" wallet_dir: "Set the wallets folder" diff --git a/i18n/fr.yml b/i18n/fr.yml index 13829a2..b1b4ecc 100644 --- a/i18n/fr.yml +++ b/i18n/fr.yml @@ -44,6 +44,7 @@ fr: add_gpg_key: "Partage le portefeuille avec une autre clé GPG" alpha: "Utilise des lettres dans la génération d'un mot de passe" config: "Spécifie le fichier de configuration à utiliser" + comment: "Spécifie un commentaire" clipboard: "Désactive la fonction presse papier" default_path: "Déplace le portefeuille dans le dossier par défaut" default_wallet: "Spécifie le porte-feuille à utiliser par défaut" @@ -61,6 +62,7 @@ fr: gpg_key: "Spécifie une clé GPG (ex: user@example.com)" group: "Recherche les éléments appartenant au groupe spécifié" help: "Affiche ce message d'aide" + host: "Spécifie le nom du serveur ou l'ip" import: "Importe des éléments depuis un fichier yaml" init: "Initialise mpw" key: "Spécifie le nom d'une clé" @@ -68,17 +70,23 @@ fr: length: "Taille du mot de passe" list: "Liste les portefeuilles" list_keys: "Liste les clés GPG dans le portefeuille" + new_group: "Spécifie le groupe de l'item" numeric: "Utilise des chiffre dans la génération d'un mot de passe" + otp_code: "Spécifie un code OTP" path: "Déplace le portefeuille dans un nouveau dossier" pattern: "Motif de donnée à chercher" pinmode: "Active le mode pinentry (valable avec gpg >= 2.1)" + port: "Spécifie un port de connexion" + protocol: "Spécifie un protocol de connexion" random_password: "Génére un mot de passe aléatoire" setup: "Création d'un nouveau fichier de configuration" setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille" special_chars: "Utilise des charactères speciaux dans la génération d'un mot de passe" show: "Recherche et affiche les éléments" show_all: "Liste tous les éléments" + text_editor: "Active l'édition avec un éditeur de texte" usage: "Utilisation" + user: "Spécifie un utilisateur" wallet: "Spécifie le portefeuille à utiliser" wallet_dir: "Spécifie le répertoire des portefeuilles" diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 6fd257f..7aad7f0 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -400,9 +400,9 @@ module MPW # @args: template -> template name # item -> the item to edit # password -> disable field password - def text_editor(template_name, item = nil, password = false) + # @rtrn: a hash with the value for an item + def text_editor(template_name, password = false, item = nil, **options) editor = ENV['EDITOR'] || 'nano' - options = {} opts = {} template_file = "#{File.expand_path('../../../templates', __FILE__)}/#{template_name}.erb" template = ERB.new(IO.read(template_file)) @@ -430,8 +430,10 @@ module MPW # Form to add a new item # @args: password -> generate a random password - def add(password = false) - options = text_editor('add_form', nil, password) + # text_editor -> enable text editor mode + # values -> hash with multiples value to set the item + def add(password = false, text_editor = false, **values) + options = text_editor('add_form', password, nil, values) if text_editor item = Item.new(options) options[:password] = MPW.password(@config.password) if password @@ -447,8 +449,10 @@ module MPW # Update an item # @args: password -> generate a random password + # text_editor -> enable text editor mode # options -> the option to search - def update(password = false, **options) + # values -> hash with multiples value to set the item + def update(password = false, text_editor = false, options = {}, **values) items = @mpw.list(options) if items.empty? @@ -456,13 +460,13 @@ module MPW else table_items(items) if items.length > 1 - item = get_item(items) - options = text_editor('update_form', item, password) - options[:password] = MPW.password(@config.password) if password + item = get_item(items) + values = text_editor('update_form', password, item, values) if text_editor + values[:password] = MPW.password(@config.password) if password - item.update(options) - @mpw.set_password(item.id, options[:password]) if options.key?(:password) - @mpw.set_otp_key(item.id, options[:otp_key]) if options.key?(:otp_key) + item.update(values) + @mpw.set_password(item.id, values[:password]) if values.key?(:password) + @mpw.set_otp_key(item.id, values[:otp_key]) if values.key?(:otp_key) @mpw.write_data puts I18n.t('form.update_item.valid').to_s.green diff --git a/templates/add_form.erb b/templates/add_form.erb index bb81787..d88874e 100644 --- a/templates/add_form.erb +++ b/templates/add_form.erb @@ -1,9 +1,9 @@ --- -host: # <%= I18n.t('form.add_item.host') %> -user: # <%= I18n.t('form.add_item.login') %> -group: # <%= I18n.t('form.add_item.group') %> -protocol: # <%= I18n.t('form.add_item.protocol') %><% unless password %> -password: # <%= I18n.t('form.add_item.password') %><% end %> -port: # <%= I18n.t('form.add_item.port') %> -comment: # <%= I18n.t('form.add_item.comment') %> -otp_key: # <%= I18n.t('form.add_item.otp_key') %> +host: <%= options[:host] %> # <%= I18n.t('form.add_item.host') %> +user: <%= options[:user] %> # <%= I18n.t('form.add_item.login') %> +group: <%= options[:group] %> # <%= I18n.t('form.add_item.group') %> +protocol: <%= options[:protocol] %> # <%= I18n.t('form.add_item.protocol') %><% unless password %> +password: # <%= I18n.t('form.add_item.password') %><% end %> +port: <%= options[:port] %> # <%= I18n.t('form.add_item.port') %> +comment: <%= options[:comment] %> # <%= I18n.t('form.add_item.comment') %> +otp_key: <%= options[:otp] %> # <%= I18n.t('form.add_item.otp_key') %> diff --git a/templates/update_form.erb b/templates/update_form.erb index f27da0b..5037568 100644 --- a/templates/update_form.erb +++ b/templates/update_form.erb @@ -1,17 +1,17 @@ --- # <%= I18n.t('form.update_item.host') %> -host: <%= item.host %> +host: <% if options[:host] %><%= options[:host] %><% else %><%= item.host %><% end %> # <%= I18n.t('form.update_item.login') %> -user: <%= item.user %><% unless password %> +user: <% if options[:user] %><%= options[:user] %><% else %><%= item.user %><% end %><% unless password %> # <%= I18n.t('form.update_item.password') %> password: <% end %> # <%= I18n.t('form.update_item.group') %> -group: <%= item.group %> +group: <% if options[:group] %><%= options[:group] %><% else %><%= item.group %><% end %> # <%= I18n.t('form.update_item.protocol') %> -protocol: <%= item.protocol %> +protocol: <% if options[:protocol] %><%= options[:protocol] %><% else %><%= item.protocol %><% end %> # <%= I18n.t('form.update_item.port') %> -port: <%= item.port %> +port: <% if options[:port] %><%= options[:port] %><% else %><%= item.port %><% end %> # <%= I18n.t('form.update_item.otp_key') %> -otp_key: +otp_key: <% if options[:otp_key] %><%= options[:otp_key] %><% end %> # <%= I18n.t('form.update_item.comment') %> -comment: <%= item.comment %> +comment: <% if options[:comment] %><%= options[:comment] %><% else %><%= item.comment %><% end %>