1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-03-20 21:34:35 +00:00

new binary for update

This commit is contained in:
Adrien Waksberg 2016-10-17 20:48:20 +02:00
parent d49b305f69
commit c75f169b0c
3 changed files with 97 additions and 61 deletions

63
bin/mpw-update Normal file
View file

@ -0,0 +1,63 @@
#!/usr/bin/ruby
# MPW is a software to crypt and manage your passwords
# Copyright (C) 2016 Adrien Waksberg <mpw@yae.im>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'optparse'
require 'mpw/config'
require 'mpw/cli'
# --------------------------------------------------------- #
# Options
# --------------------------------------------------------- #
options = {}
OptionParser.new do |opts|
opts.banner = "#{I18n.t('option.usage')}: mpw [options]"
opts.on('-c', '--config CONFIG', I18n.t('option.config')) do |config|
options[:config] = config
end
opts.on('-g', '--group GROUP', I18n.t('option.config')) do |group|
options[:group] = group
end
opts.on('-h', '--help', I18n.t('option.help')) do
puts opts
exit 0
end
opts.on('-p', '--pattern PATTERN', I18n.t('option.config')) do |pattern|
options[:pattern] = pattern
end
opts.on('-w', '--wallet WALLET', I18n.t('option.wallet')) do |wallet|
options[:wallet] = wallet
end
end.parse!
config = MPW::Config.new(options[:config])
cli = MPW::Cli.new(config, nil, options[:sync], nil)
opts = { search: options[:pattern],
group: options[:group],
}
cli.get_wallet(options[:wallet])
cli.decrypt
cli.update(opts)

View file

@ -228,53 +228,19 @@ class Cli
end
end
# Display the query's result
# @args: search -> the string to search
# protocol -> search from a particular protocol
def display(options={})
result = @mpw.list(options)
# Get an item when multiple choice
# @args: items -> array of items
# @rtrn: item
def get_item(items)
return items[0] if items.length == 1
case result.length
when 0
puts I18n.t('display.nothing')
when 1
display_item(result.first)
items.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase }
choice = ask(I18n.t('form.select')).to_i
if choice >= 1 and choice <= items.length
return items[choice-1]
else
group = nil
i = 1
result.sort! { |a,b| a.group.to_s.downcase <=> b.group.to_s.downcase }
result.each do |item|
if group != item.group
group = item.group
if group.empty?
puts I18n.t('display.no_group').yellow
else
puts "\n#{group}".yellow
end
end
print " |_ ".yellow
print "#{i}: ".cyan
print item.name
print " -> #{item.comment}".magenta if not item.comment.to_s.empty?
print "\n"
i += 1
end
print "\n"
choice = ask(I18n.t('form.select')).to_i
if choice >= 1 and choice < i
display_item(result[choice-1])
else
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
end
return nil
end
end
@ -480,17 +446,26 @@ class Cli
end
# Update an item
# @args: id -> the item's id
def update(item)
options = text_editor('update_form', item)
# @args: options -> the option to search
def update(options={})
items = @mpw.list(options)
if items.length == 0
puts "#{I18n.t('display.warning')}: #{I18n.t('warning.select')}".yellow
else
table(items) if items.length > 1
item.update(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
item = get_item(items)
options = text_editor('update_form', item)
puts "#{I18n.t('form.update_item.valid')}".green
item.update(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.valid')}".green
end
rescue Exception => e
puts "#{I18n.t('display.error')} #14: #{e}".red
end

View file

@ -1,19 +1,17 @@
---
# <%= I18n.t('form.update_item.name') %>
name: <%= item.name %>
# <%= I18n.t('form.update_item.group') %>
group: <%= item.group %>
# <%= I18n.t('form.update_item.host') %>
host: <%= item.host %>
# <%= I18n.t('form.update_item.protocol') %>
protocol: <%= item.protocol %>
# <%= I18n.t('form.update_item.login') %>
user: <%= item.user %>
# <%= I18n.t('form.update_item.password') %>
password:
# <%= I18n.t('form.update_item.group') %>
group: <%= item.group %>
# <%= I18n.t('form.update_item.protocol') %>
protocol: <%= item.protocol %>
# <%= I18n.t('form.update_item.port') %>
port: <%= item.port %>
# <%= I18n.t('form.update_item.otp_key') %>
opt_key:
# <%= I18n.t('form.update_item.comment') %>
comment: <%= item.comment %>
# <%= I18n.t('form.update_item.otp_key') %>
opt_code: