1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-10-27 02:33:19 +00:00

feat: add options to edit or update an item

This commit is contained in:
Adrien Waksberg 2017-04-13 22:35:57 +02:00
parent 77bca426bc
commit 0fdefdf433
7 changed files with 120 additions and 32 deletions

View file

@ -24,7 +24,9 @@ require 'mpw/cli'
# Options # Options
# --------------------------------------------------------- # # --------------------------------------------------------- #
values = {}
options = {} options = {}
options[:text_editor] = true
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw add [options]" opts.banner = "#{I18n.t('option.usage')}: mpw add [options]"
@ -33,15 +35,47 @@ OptionParser.new do |opts|
options[:config] = config options[:config] = config
end 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 opts.on('-h', '--help', I18n.t('option.help')) do
puts opts puts opts
exit 0 exit 0
end 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 opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true options[:password] = true
end 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| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
@ -53,4 +87,4 @@ cli = MPW::Cli.new(config)
cli.load_config cli.load_config
cli.get_wallet(options[:wallet]) cli.get_wallet(options[:wallet])
cli.decrypt cli.decrypt
cli.add(options[:password]) cli.add(options[:password], options[:text_editor], values)

View file

@ -24,8 +24,10 @@ require 'mpw/cli'
# Options # Options
# --------------------------------------------------------- # # --------------------------------------------------------- #
options = {}
values = {} values = {}
search = {}
options = {}
options[:text_editor] = false
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw update [options]" opts.banner = "#{I18n.t('option.usage')}: mpw update [options]"
@ -34,7 +36,15 @@ OptionParser.new do |opts|
options[:config] = config options[:config] = config
end 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| 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 values[:group] = group
end end
@ -43,14 +53,38 @@ OptionParser.new do |opts|
exit 0 exit 0
end 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| 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 end
opts.on('-r', '--random', I18n.t('option.random_password')) do opts.on('-r', '--random', I18n.t('option.random_password')) do
options[:password] = true options[:password] = true
end 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| opts.on('-w', '--wallet NAME', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet options[:wallet] = wallet
end end
@ -62,4 +96,4 @@ cli = MPW::Cli.new(config)
cli.load_config cli.load_config
cli.get_wallet(options[:wallet]) cli.get_wallet(options[:wallet])
cli.decrypt cli.decrypt
cli.update(options[:password], values) cli.update(options[:password], options[:text_editor], search, values)

View file

@ -43,6 +43,7 @@ en:
add: "Add an item or key" add: "Add an item or key"
add_gpg_key: "Share the wallet with an other GPG key" add_gpg_key: "Share the wallet with an other GPG key"
alpha: "Use letter to generate a password" alpha: "Use letter to generate a password"
comment: "Specify a comment"
config: "Specify the configuration file to use" config: "Specify the configuration file to use"
clipboard: "Disable the clipboard feature" clipboard: "Disable the clipboard feature"
default_path: "Move the wallet in default directory" default_path: "Move the wallet in default directory"
@ -61,6 +62,7 @@ en:
gpg_key: "Specify a GPG key (ex: user@example.com)" gpg_key: "Specify a GPG key (ex: user@example.com)"
group: "Search the items with specified group" group: "Search the items with specified group"
help: "Show this help message" help: "Show this help message"
host: "Specify a host or ip"
init: "Initialize mpw" init: "Initialize mpw"
import: "Import item since a yaml file" import: "Import item since a yaml file"
key: "Specify the key name" key: "Specify the key name"
@ -68,17 +70,23 @@ en:
length: "Size of the password" length: "Size of the password"
list: "List the wallets" list: "List the wallets"
list_keys: "List the GPG keys in wallet" list_keys: "List the GPG keys in wallet"
new_group: "Specify the group for the item"
numeric: "Use number to generate a password" numeric: "Use number to generate a password"
otp_code: "Set an otp key"
path: "Move the wallet in new specify directory" path: "Move the wallet in new specify directory"
pattern: "Given search pattern" pattern: "Given search pattern"
pinmode: "Enable pinentry mode (available with gpg >= 2.1)" 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" random_password: "Generate a random password"
setup: "Create a new configuration file" setup: "Create a new configuration file"
setup_wallet: "Create a new configuration file for a wallet" setup_wallet: "Create a new configuration file for a wallet"
special_chars: "Use special char to generate a password" special_chars: "Use special char to generate a password"
show: "Search and show the items" show: "Search and show the items"
show_all: "List all items" show_all: "List all items"
text_editor: "Use text editor to edit the item"
usage: "Usage" usage: "Usage"
user: "Set an user"
wallet: "Specify a wallet to use" wallet: "Specify a wallet to use"
wallet_dir: "Set the wallets folder" wallet_dir: "Set the wallets folder"

View file

@ -44,6 +44,7 @@ fr:
add_gpg_key: "Partage le portefeuille avec une autre clé GPG" 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" alpha: "Utilise des lettres dans la génération d'un mot de passe"
config: "Spécifie le fichier de configuration à utiliser" config: "Spécifie le fichier de configuration à utiliser"
comment: "Spécifie un commentaire"
clipboard: "Désactive la fonction presse papier" clipboard: "Désactive la fonction presse papier"
default_path: "Déplace le portefeuille dans le dossier par défaut" default_path: "Déplace le portefeuille dans le dossier par défaut"
default_wallet: "Spécifie le porte-feuille à utiliser 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)" gpg_key: "Spécifie une clé GPG (ex: user@example.com)"
group: "Recherche les éléments appartenant au groupe spécifié" group: "Recherche les éléments appartenant au groupe spécifié"
help: "Affiche ce message d'aide" 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" import: "Importe des éléments depuis un fichier yaml"
init: "Initialise mpw" init: "Initialise mpw"
key: "Spécifie le nom d'une clé" key: "Spécifie le nom d'une clé"
@ -68,17 +70,23 @@ fr:
length: "Taille du mot de passe" length: "Taille du mot de passe"
list: "Liste les portefeuilles" list: "Liste les portefeuilles"
list_keys: "Liste les clés GPG dans le portefeuille" 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" 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" path: "Déplace le portefeuille dans un nouveau dossier"
pattern: "Motif de donnée à chercher" pattern: "Motif de donnée à chercher"
pinmode: "Active le mode pinentry (valable avec gpg >= 2.1)" 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" random_password: "Génére un mot de passe aléatoire"
setup: "Création d'un nouveau fichier de configuration" setup: "Création d'un nouveau fichier de configuration"
setup_wallet: "Création d'un nouveau fichier de configuration pour un portefeuille" 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" 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: "Recherche et affiche les éléments"
show_all: "Liste tous les éléments" show_all: "Liste tous les éléments"
text_editor: "Active l'édition avec un éditeur de texte"
usage: "Utilisation" usage: "Utilisation"
user: "Spécifie un utilisateur"
wallet: "Spécifie le portefeuille à utiliser" wallet: "Spécifie le portefeuille à utiliser"
wallet_dir: "Spécifie le répertoire des portefeuilles" wallet_dir: "Spécifie le répertoire des portefeuilles"

View file

@ -400,9 +400,9 @@ module MPW
# @args: template -> template name # @args: template -> template name
# item -> the item to edit # item -> the item to edit
# password -> disable field password # 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' editor = ENV['EDITOR'] || 'nano'
options = {}
opts = {} opts = {}
template_file = "#{File.expand_path('../../../templates', __FILE__)}/#{template_name}.erb" template_file = "#{File.expand_path('../../../templates', __FILE__)}/#{template_name}.erb"
template = ERB.new(IO.read(template_file)) template = ERB.new(IO.read(template_file))
@ -430,8 +430,10 @@ module MPW
# Form to add a new item # Form to add a new item
# @args: password -> generate a random password # @args: password -> generate a random password
def add(password = false) # text_editor -> enable text editor mode
options = text_editor('add_form', nil, password) # 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) item = Item.new(options)
options[:password] = MPW.password(@config.password) if password options[:password] = MPW.password(@config.password) if password
@ -447,8 +449,10 @@ module MPW
# Update an item # Update an item
# @args: password -> generate a random password # @args: password -> generate a random password
# text_editor -> enable text editor mode
# options -> the option to search # 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) items = @mpw.list(options)
if items.empty? if items.empty?
@ -457,12 +461,12 @@ module MPW
table_items(items) if items.length > 1 table_items(items) if items.length > 1
item = get_item(items) item = get_item(items)
options = text_editor('update_form', item, password) values = text_editor('update_form', password, item, values) if text_editor
options[:password] = MPW.password(@config.password) if password values[:password] = MPW.password(@config.password) if password
item.update(options) item.update(values)
@mpw.set_password(item.id, options[:password]) if options.key?(:password) @mpw.set_password(item.id, values[:password]) if values.key?(:password)
@mpw.set_otp_key(item.id, options[:otp_key]) if options.key?(:otp_key) @mpw.set_otp_key(item.id, values[:otp_key]) if values.key?(:otp_key)
@mpw.write_data @mpw.write_data
puts I18n.t('form.update_item.valid').to_s.green puts I18n.t('form.update_item.valid').to_s.green

View file

@ -1,9 +1,9 @@
--- ---
host: # <%= I18n.t('form.add_item.host') %> host: <%= options[:host] %> # <%= I18n.t('form.add_item.host') %>
user: # <%= I18n.t('form.add_item.login') %> user: <%= options[:user] %> # <%= I18n.t('form.add_item.login') %>
group: # <%= I18n.t('form.add_item.group') %> group: <%= options[:group] %> # <%= I18n.t('form.add_item.group') %>
protocol: # <%= I18n.t('form.add_item.protocol') %><% unless password %> protocol: <%= options[:protocol] %> # <%= I18n.t('form.add_item.protocol') %><% unless password %>
password: # <%= I18n.t('form.add_item.password') %><% end %> password: # <%= I18n.t('form.add_item.password') %><% end %>
port: # <%= I18n.t('form.add_item.port') %> port: <%= options[:port] %> # <%= I18n.t('form.add_item.port') %>
comment: # <%= I18n.t('form.add_item.comment') %> comment: <%= options[:comment] %> # <%= I18n.t('form.add_item.comment') %>
otp_key: # <%= I18n.t('form.add_item.otp_key') %> otp_key: <%= options[:otp] %> # <%= I18n.t('form.add_item.otp_key') %>

View file

@ -1,17 +1,17 @@
--- ---
# <%= I18n.t('form.update_item.host') %> # <%= 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') %> # <%= 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') %> # <%= I18n.t('form.update_item.password') %>
password: <% end %> password: <% end %>
# <%= I18n.t('form.update_item.group') %> # <%= 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') %> # <%= 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') %> # <%= 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') %> # <%= 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') %> # <%= I18n.t('form.update_item.comment') %>
comment: <%= item.comment %> comment: <% if options[:comment] %><%= options[:comment] %><% else %><%= item.comment %><% end %>