From c75f169b0ca7d28691ac73e7db30d47a3c3347e9 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Mon, 17 Oct 2016 20:48:20 +0200 Subject: [PATCH] new binary for update --- bin/mpw-update | 63 ++++++++++++++++++++++++++++++ lib/mpw/cli.rb | 81 ++++++++++++++------------------------- templates/update_form.erb | 14 +++---- 3 files changed, 97 insertions(+), 61 deletions(-) create mode 100644 bin/mpw-update diff --git a/bin/mpw-update b/bin/mpw-update new file mode 100644 index 0000000..e521ffc --- /dev/null +++ b/bin/mpw-update @@ -0,0 +1,63 @@ +#!/usr/bin/ruby +# MPW is a software to crypt and manage your passwords +# Copyright (C) 2016 Adrien Waksberg +# +# 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) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 5f356ad..67bf9a2 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -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 diff --git a/templates/update_form.erb b/templates/update_form.erb index 57178cc..f4a380b 100644 --- a/templates/update_form.erb +++ b/templates/update_form.erb @@ -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: