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:
parent
110cbe1510
commit
2f627f271c
6 changed files with 70 additions and 98 deletions
22
bin/mpw
22
bin/mpw
|
@ -75,10 +75,6 @@ OptionParser.new do |opts|
|
||||||
options[:clipboard] = false
|
options[:clipboard] = false
|
||||||
end
|
end
|
||||||
|
|
||||||
opts.on('-d', '--delete', I18n.t('option.remove')) do
|
|
||||||
options[:delete] = true
|
|
||||||
end
|
|
||||||
|
|
||||||
opts.on('-e', '--export', I18n.t('option.export')) do
|
opts.on('-e', '--export', I18n.t('option.export')) do
|
||||||
options[:export] = true
|
options[:export] = true
|
||||||
end
|
end
|
||||||
|
@ -104,10 +100,6 @@ OptionParser.new do |opts|
|
||||||
exit 0
|
exit 0
|
||||||
end
|
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
|
opts.on('-I', '--import', I18n.t('option.import')) do
|
||||||
options[:import] = true
|
options[:import] = true
|
||||||
end
|
end
|
||||||
|
@ -136,10 +128,6 @@ OptionParser.new do |opts|
|
||||||
options[:setup] = true
|
options[:setup] = true
|
||||||
end
|
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|
|
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
|
||||||
options[:wallet] = wallet
|
options[:wallet] = wallet
|
||||||
end
|
end
|
||||||
|
@ -191,14 +179,6 @@ begin
|
||||||
|
|
||||||
cli.display(opts)
|
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
|
# Add a new item
|
||||||
elsif not options[:add].nil? and options[:key].nil?
|
elsif not options[:add].nil? and options[:key].nil?
|
||||||
cli.add
|
cli.add
|
||||||
|
@ -215,7 +195,7 @@ begin
|
||||||
elsif not options[:export].nil? and not options[:file].nil?
|
elsif not options[:export].nil? and not options[:file].nil?
|
||||||
cli.export(options[:file])
|
cli.export(options[:file])
|
||||||
|
|
||||||
# Add a new item
|
# Import
|
||||||
elsif not options[:import].nil? and not options[:file].nil?
|
elsif not options[:import].nil? and not options[:file].nil?
|
||||||
cli.import(options[:file])
|
cli.import(options[:file])
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,6 @@ en:
|
||||||
exception: "Can't create the GPG key!"
|
exception: "Can't create the GPG key!"
|
||||||
name: "You must define a name for your GPG key!"
|
name: "You must define a name for your GPG key!"
|
||||||
password: "You must define a password 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}!"
|
export: "Can't export, unable to write in %{file}!"
|
||||||
gpg_file:
|
gpg_file:
|
||||||
decrypt: "Can't decrypt file!"
|
decrypt: "Can't decrypt file!"
|
||||||
|
@ -46,7 +44,6 @@ en:
|
||||||
generate_password: "Generate a random password (default 8 characters)"
|
generate_password: "Generate a random password (default 8 characters)"
|
||||||
group: "Search the items with specified group"
|
group: "Search the items with specified group"
|
||||||
help: "Show this help message"
|
help: "Show this help message"
|
||||||
id: "Specify an id, to use with the options [--delete | --update]"
|
|
||||||
import: "Import item since a yaml file"
|
import: "Import item since a yaml file"
|
||||||
key: "Specify the key name, to use with the options [--add | --delete | --update]"
|
key: "Specify the key name, to use with the options [--add | --delete | --update]"
|
||||||
no_sync: "Disable synchronization with the server"
|
no_sync: "Disable synchronization with the server"
|
||||||
|
@ -56,8 +53,6 @@ en:
|
||||||
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"
|
||||||
remove: "Delete an item"
|
|
||||||
update: "Update an item"
|
|
||||||
usage: "Usage"
|
usage: "Usage"
|
||||||
wallet: "Specify a wallet to use"
|
wallet: "Specify a wallet to use"
|
||||||
|
|
||||||
|
@ -93,7 +88,6 @@ en:
|
||||||
delete_item:
|
delete_item:
|
||||||
ask: "Are you sure you want to remove the item %{id} ?"
|
ask: "Are you sure you want to remove the item %{id} ?"
|
||||||
valid: "The item %{id} has been removed!"
|
valid: "The item %{id} has been removed!"
|
||||||
not_valid: "The item %{id} hasn't been removed, because it doesn't exist!"
|
|
||||||
import:
|
import:
|
||||||
ask: "Are you sure you want to import this file %{file} ?"
|
ask: "Are you sure you want to import this file %{file} ?"
|
||||||
valid: "The import is succesfull!"
|
valid: "The import is succesfull!"
|
||||||
|
|
|
@ -10,8 +10,6 @@ fr:
|
||||||
exception: "La création de la clé GPG n'a pas pu aboutir!"
|
exception: "La création de la clé GPG n'a pas pu aboutir!"
|
||||||
name: "Vous devez définir un nom pour votre clé GPG!"
|
name: "Vous devez définir un nom pour votre clé GPG!"
|
||||||
password: "Vous devez définir un mot de passe 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}!"
|
export: "Impossible d'exporter les données dans le fichier %{file}!"
|
||||||
gpg_file:
|
gpg_file:
|
||||||
decrypt: "Impossible de déchiffrer le fichier GPG!"
|
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)"
|
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é"
|
group: "Recherche les éléments appartenant au groupe spécifié"
|
||||||
help: "Affiche ce message d'aide"
|
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"
|
import: "Importe des éléments depuis un fichier yaml"
|
||||||
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
|
key: "Spécifie le nom d'une clé, à utiliser avec les options [--add | --delete | --update]"
|
||||||
no_sync: "Désactive la synchronisation avec le serveur"
|
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"
|
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"
|
||||||
remove: "Supprime un élément"
|
|
||||||
update: "Met à jour un élément"
|
|
||||||
usage: "Utilisation"
|
usage: "Utilisation"
|
||||||
wallet: "Spécifie le portefeuille à utiliser"
|
wallet: "Spécifie le portefeuille à utiliser"
|
||||||
|
|
||||||
|
@ -93,7 +88,6 @@ fr:
|
||||||
delete_item:
|
delete_item:
|
||||||
ask: "Êtes vous sûre de vouloir supprimer l'élément %{id} ?"
|
ask: "Êtes vous sûre de vouloir supprimer l'élément %{id} ?"
|
||||||
valid: "L'élément %{id} a bien été supprimé!"
|
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:
|
import:
|
||||||
ask: "Êtes vous sûre de vouloir importer le fichier %{file} ?"
|
ask: "Êtes vous sûre de vouloir importer le fichier %{file} ?"
|
||||||
valid: "L'import est un succès!"
|
valid: "L'import est un succès!"
|
||||||
|
|
114
lib/mpw/cli.rb
114
lib/mpw/cli.rb
|
@ -21,6 +21,7 @@ require 'i18n'
|
||||||
require 'colorize'
|
require 'colorize'
|
||||||
require 'highline/import'
|
require 'highline/import'
|
||||||
require 'clipboard'
|
require 'clipboard'
|
||||||
|
require 'tmpdir'
|
||||||
require 'mpw/item'
|
require 'mpw/item'
|
||||||
require 'mpw/mpw'
|
require 'mpw/mpw'
|
||||||
|
|
||||||
|
@ -156,7 +157,7 @@ class Cli
|
||||||
group = nil
|
group = nil
|
||||||
i = 1
|
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|
|
result.each do |item|
|
||||||
if group != item.group
|
if group != item.group
|
||||||
|
@ -263,6 +264,12 @@ class Cli
|
||||||
Clipboard.copy(@mpw.get_otp_code(item.id))
|
Clipboard.copy(@mpw.get_otp_code(item.id))
|
||||||
puts I18n.t('form.clipboard.otp', time: @mpw.get_otp_remaining_time).yellow
|
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
|
else
|
||||||
puts "----- #{I18n.t('form.clipboard.help.name')} -----".cyan
|
puts "----- #{I18n.t('form.clipboard.help.name')} -----".cyan
|
||||||
puts I18n.t('form.clipboard.help.login')
|
puts I18n.t('form.clipboard.help.login')
|
||||||
|
@ -336,30 +343,41 @@ class Cli
|
||||||
puts "#{I18n.t('display.error')} #15: #{e}".red
|
puts "#{I18n.t('display.error')} #15: #{e}".red
|
||||||
end
|
end
|
||||||
|
|
||||||
# Form to add a new item
|
def text_editor(template_name, item=nil)
|
||||||
def add
|
options = {}
|
||||||
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')
|
Dir.mktmpdir do |dir|
|
||||||
puts '--------------------'
|
tmp_file = "#{dir}/#{template_name}.yml"
|
||||||
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
|
|
||||||
|
|
||||||
if @otp
|
File.open(tmp_file, 'w') do |f|
|
||||||
otp_key = ask(I18n.t('form.add_item.otp_key')).to_s
|
f << template.result(binding)
|
||||||
|
end
|
||||||
|
|
||||||
|
system("vim #{tmp_file}")
|
||||||
|
|
||||||
|
opts = YAML::load_file(tmp_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
item = Item.new(options)
|
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.add(item)
|
||||||
@mpw.set_password(item.id, password)
|
@mpw.set_password(item.id, options[:password]) if options.has_key?(:password)
|
||||||
@mpw.set_otp_key(item.id, otp_key)
|
@mpw.set_otp_key(item.id, options[:otp_key]) if options.has_key?(:otp_key)
|
||||||
@mpw.write_data
|
@mpw.write_data
|
||||||
@mpw.sync(true) if @sync
|
@mpw.sync(true) if @sync
|
||||||
|
|
||||||
|
@ -368,39 +386,16 @@ class Cli
|
||||||
|
|
||||||
# Update an item
|
# Update an item
|
||||||
# @args: id -> the item's id
|
# @args: id -> the item's id
|
||||||
def update(id)
|
def update(item)
|
||||||
item = @mpw.search_by_id(id)
|
options = text_editor('update_form', item)
|
||||||
|
|
||||||
if not item.nil?
|
item.update(options)
|
||||||
options = {}
|
@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.title')
|
puts "#{I18n.t('form.update_item.valid')}".green
|
||||||
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? }
|
|
||||||
|
|
||||||
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.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
|
rescue Exception => e
|
||||||
puts "#{I18n.t('display.error')} #14: #{e}".red
|
puts "#{I18n.t('display.error')} #14: #{e}".red
|
||||||
end
|
end
|
||||||
|
@ -408,29 +403,18 @@ class Cli
|
||||||
# Remove an item
|
# Remove an item
|
||||||
# @args: id -> the item's id
|
# @args: id -> the item's id
|
||||||
# force -> no resquest a validation
|
# force -> no resquest a validation
|
||||||
def delete(id, force=false)
|
def delete(item)
|
||||||
@clipboard = false
|
confirm = ask("#{I18n.t('form.delete_item.ask')} (y/N) ").to_s
|
||||||
item = @mpw.search_by_id(id)
|
|
||||||
|
|
||||||
if item.nil?
|
if not confirm =~ /^(y|yes|YES|Yes|Y)$/
|
||||||
puts I18n.t('form.delete_item.not_valid', id: id)
|
|
||||||
return
|
return
|
||||||
end
|
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
|
item.delete
|
||||||
@mpw.write_data
|
@mpw.write_data
|
||||||
@mpw.sync(true) if @sync
|
@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
|
rescue Exception => e
|
||||||
puts "#{I18n.t('display.error')} #16: #{e}".red
|
puts "#{I18n.t('display.error')} #16: #{e}".red
|
||||||
end
|
end
|
||||||
|
|
10
templates/add_form.erb
Normal file
10
templates/add_form.erb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
name:
|
||||||
|
group:
|
||||||
|
host:
|
||||||
|
protocol:
|
||||||
|
user:
|
||||||
|
password:
|
||||||
|
port:
|
||||||
|
comment:
|
||||||
|
otp_secret:
|
10
templates/update_form.erb
Normal file
10
templates/update_form.erb
Normal 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:
|
Loading…
Reference in a new issue