1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 05:47:53 +00:00

add and update with vim editor

This commit is contained in:
nishiki 2016-08-09 22:06:47 +02:00
parent 110cbe1510
commit 2f627f271c
6 changed files with 70 additions and 98 deletions

22
bin/mpw
View file

@ -75,10 +75,6 @@ OptionParser.new do |opts|
options[:clipboard] = false
end
opts.on('-d', '--delete', I18n.t('option.remove')) do
options[:delete] = true
end
opts.on('-e', '--export', I18n.t('option.export')) do
options[:export] = true
end
@ -104,10 +100,6 @@ OptionParser.new do |opts|
exit 0
end
opts.on('-i', '--id ID', I18n.t('option.id')) do |id|
options[:id] = id
end
opts.on('-I', '--import', I18n.t('option.import')) do
options[:import] = true
end
@ -136,10 +128,6 @@ OptionParser.new do |opts|
options[:setup] = true
end
opts.on('-u', '--update', I18n.t('option.update')) do
options[:update] = true
end
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet
end
@ -191,14 +179,6 @@ begin
cli.display(opts)
# Remove an item
elsif not options[:delete].nil? and not options[:id].nil?
cli.delete(options[:id], options[:force])
# Update an item
elsif not options[:update].nil? and not options[:id].nil?
cli.update(options[:id])
# Add a new item
elsif not options[:add].nil? and options[:key].nil?
cli.add
@ -215,7 +195,7 @@ begin
elsif not options[:export].nil? and not options[:file].nil?
cli.export(options[:file])
# Add a new item
# Import
elsif not options[:import].nil? and not options[:file].nil?
cli.import(options[:file])

View file

@ -10,8 +10,6 @@ en:
exception: "Can't create the GPG key!"
name: "You must define a name for your GPG key!"
password: "You must define a password for your GPG key!"
delete:
id_no_exist: "Can't delete the item %{id}, it doesn't exist!"
export: "Can't export, unable to write in %{file}!"
gpg_file:
decrypt: "Can't decrypt file!"
@ -46,7 +44,6 @@ en:
generate_password: "Generate a random password (default 8 characters)"
group: "Search the items with specified group"
help: "Show this help message"
id: "Specify an id, to use with the options [--delete | --update]"
import: "Import item since a yaml file"
key: "Specify the key name, to use with the options [--add | --delete | --update]"
no_sync: "Disable synchronization with the server"
@ -56,8 +53,6 @@ en:
special_chars: "Use special char to generate a password"
show: "Search and show the items"
show_all: "List all items"
remove: "Delete an item"
update: "Update an item"
usage: "Usage"
wallet: "Specify a wallet to use"
@ -93,7 +88,6 @@ en:
delete_item:
ask: "Are you sure you want to remove the item %{id} ?"
valid: "The item %{id} has been removed!"
not_valid: "The item %{id} hasn't been removed, because it doesn't exist!"
import:
ask: "Are you sure you want to import this file %{file} ?"
valid: "The import is succesfull!"

View file

@ -10,8 +10,6 @@ fr:
exception: "La création de la clé GPG n'a pas pu aboutir!"
name: "Vous devez définir un nom pour votre clé GPG!"
password: "Vous devez définir un mot de passe pour votre clé GPG!"
delete:
id_no_exist: "Impossible de supprimer l'élément %{id}, car il n'existe pas!"
export: "Impossible d'exporter les données dans le fichier %{file}!"
gpg_file:
decrypt: "Impossible de déchiffrer le fichier GPG!"
@ -46,7 +44,6 @@ fr:
generate_password: "Génére un mot de passe aléatoire (défaut 8 caractères)"
group: "Recherche les éléments appartenant au groupe spécifié"
help: "Affiche ce message d'aide"
id: "Spécifie un identifiant, à utiliser avec les options [--delete | --update]"
import: "Importe des éléments depuis un fichier yaml"
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
no_sync: "Désactive la synchronisation avec le serveur"
@ -56,8 +53,6 @@ fr:
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"
remove: "Supprime un élément"
update: "Met à jour un élément"
usage: "Utilisation"
wallet: "Spécifie le portefeuille à utiliser"
@ -93,7 +88,6 @@ fr:
delete_item:
ask: "Êtes vous sûre de vouloir supprimer l'élément %{id} ?"
valid: "L'élément %{id} a bien été supprimé!"
not_valid: "L'élément %{id} n'a pu être supprimé, car il n'existe pas!"
import:
ask: "Êtes vous sûre de vouloir importer le fichier %{file} ?"
valid: "L'import est un succès!"

View file

@ -21,6 +21,7 @@ require 'i18n'
require 'colorize'
require 'highline/import'
require 'clipboard'
require 'tmpdir'
require 'mpw/item'
require 'mpw/mpw'
@ -156,7 +157,7 @@ class Cli
group = nil
i = 1
result.sort! { |a,b| a.group.downcase <=> b.group.downcase }
result.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase }
result.each do |item|
if group != item.group
@ -263,6 +264,12 @@ class Cli
Clipboard.copy(@mpw.get_otp_code(item.id))
puts I18n.t('form.clipboard.otp', time: @mpw.get_otp_remaining_time).yellow
when 'd', 'delete'
delete(item)
when 'u', 'update', 'e', 'edit'
update(item)
else
puts "----- #{I18n.t('form.clipboard.help.name')} -----".cyan
puts I18n.t('form.clipboard.help.login')
@ -336,30 +343,41 @@ class Cli
puts "#{I18n.t('display.error')} #15: #{e}".red
end
# Form to add a new item
def add
def text_editor(template_name, item=nil)
options = {}
opts = {}
template_file = "#{File.expand_path('../../../templates', __FILE__)}/#{template_name}.erb"
template = ERB.new(IO.read(template_file))
puts I18n.t('form.add_item.title')
puts '--------------------'
options[:name] = ask(I18n.t('form.add_item.name')).to_s
options[:group] = ask(I18n.t('form.add_item.group')).to_s
options[:host] = ask(I18n.t('form.add_item.server')).to_s
options[:protocol] = ask(I18n.t('form.add_item.protocol')).to_s
options[:user] = ask(I18n.t('form.add_item.login')).to_s
password = ask(I18n.t('form.add_item.password')).to_s
options[:port] = ask(I18n.t('form.add_item.port')).to_s
options[:comment] = ask(I18n.t('form.add_item.comment')).to_s
Dir.mktmpdir do |dir|
tmp_file = "#{dir}/#{template_name}.yml"
if @otp
otp_key = ask(I18n.t('form.add_item.otp_key')).to_s
File.open(tmp_file, 'w') do |f|
f << template.result(binding)
end
system("vim #{tmp_file}")
opts = YAML::load_file(tmp_file)
end
opts.delete_if { |k,v| v.to_s.empty? }
opts.each do |k,v|
options[k.to_sym] = v
end
return options
end
# Form to add a new item
def add
options = text_editor('add_form')
item = Item.new(options)
@mpw.add(item)
@mpw.set_password(item.id, password)
@mpw.set_otp_key(item.id, otp_key)
@mpw.set_password(item.id, options[:password]) if options.has_key?(:password)
@mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key)
@mpw.write_data
@mpw.sync(true) if @sync
@ -368,39 +386,16 @@ class Cli
# Update an item
# @args: id -> the item's id
def update(id)
item = @mpw.search_by_id(id)
if not item.nil?
options = {}
puts I18n.t('form.update_item.title')
puts '--------------------'
options[:name] = ask(I18n.t('form.update_item.name' , name: item.name)).to_s
options[:group] = ask(I18n.t('form.update_item.group' , group: item.group)).to_s
options[:host] = ask(I18n.t('form.update_item.server' , server: item.host)).to_s
options[:protocol] = ask(I18n.t('form.update_item.protocol', protocol: item.protocol)).to_s
options[:user] = ask(I18n.t('form.update_item.login' , login: item.user)).to_s
password = ask(I18n.t('form.update_item.password')).to_s
options[:port] = ask(I18n.t('form.update_item.port' , port: item.port)).to_s
options[:comment] = ask(I18n.t('form.update_item.comment' , comment: item.comment)).to_s
if @otp
otp_key = ask(I18n.t('form.update_item.otp_key')).to_s
end
options.delete_if { |k,v| v.empty? }
def update(item)
options = text_editor('update_form', item)
item.update(options)
@mpw.set_password(item.id, password) if not password.empty?
@mpw.set_otp_key(item.id, otp_key) if not otp_key.to_s.empty?
@mpw.set_password(item.id, options[:password]) if options.has_key?(:password)
@mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key)
@mpw.write_data
@mpw.sync(true) if @sync
puts "#{I18n.t('form.update_item.valid')}".green
else
puts I18n.t('display.nothing')
end
rescue Exception => e
puts "#{I18n.t('display.error')} #14: #{e}".red
end
@ -408,29 +403,18 @@ class Cli
# Remove an item
# @args: id -> the item's id
# force -> no resquest a validation
def delete(id, force=false)
@clipboard = false
item = @mpw.search_by_id(id)
def delete(item)
confirm = ask("#{I18n.t('form.delete_item.ask')} (y/N) ").to_s
if item.nil?
puts I18n.t('form.delete_item.not_valid', id: id)
return
end
if not force
display_item(item)
confirm = ask("#{I18n.t('form.delete_item.ask', id: id)} (y/N) ").to_s
if not confirm =~ /^(y|yes|YES|Yes|Y)$/
return
end
end
item.delete
@mpw.write_data
@mpw.sync(true) if @sync
puts "#{I18n.t('form.delete_item.valid', id: id)}".green
puts "#{I18n.t('form.delete_item.valid')}".green
rescue Exception => e
puts "#{I18n.t('display.error')} #16: #{e}".red
end

10
templates/add_form.erb Normal file
View file

@ -0,0 +1,10 @@
---
name:
group:
host:
protocol:
user:
password:
port:
comment:
otp_secret:

10
templates/update_form.erb Normal file
View file

@ -0,0 +1,10 @@
---
name: <%= item.name %>
group: <%= item.group %>
host: <%= item.host %>
protocol: <%= item.protocol %>
user: <%= item.user %>
password:
port: <%= item.port %>
comment: <%= item.comment %>
otp_secret: