From 92168dd654dfc55b9ec5380f8d8a4fbb15e2c5f3 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Wed, 19 Oct 2016 16:12:13 +0200 Subject: [PATCH] new binary for import --- bin/mpw-import | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/mpw/cli.rb | 21 +++++++++++++++++- lib/mpw/mpw.rb | 22 ------------------- 3 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 bin/mpw-import diff --git a/bin/mpw-import b/bin/mpw-import new file mode 100644 index 0000000..61b5d8c --- /dev/null +++ b/bin/mpw-import @@ -0,0 +1,59 @@ +#!/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 wallet [options]" + + opts.on('-c', '--config PATH', I18n.t('option.config')) do |config| + options[:config] = config + end + + opts.on('-f', '--file PATH', I18n.t('option.file')) do |file| + options[:file] = file + end + + opts.on('-h', '--help', I18n.t('option.help')) do + puts opts + exit 0 + 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.import(options[:file]) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index 8e95297..8fc109b 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -529,7 +529,26 @@ class Cli # Import items from a YAML file # @args: file -> the import file def import(file) - @mpw.import(file) + raise I18n.t('import.file_empty') if file.to_s.empty? + raise I18n.t('import.file_not_exist') if not File.exist?(file) + + YAML::load_file(file).each_value do |row| + + item = Item.new(group: row['group'], + host: row['host'], + protocol: row['protocol'], + user: row['user'], + port: row['port'], + comment: row['comment'], + ) + + next if item.empty? + + @mpw.add(item) + @mpw.set_password(item.id, row['password']) if not row['password'].to_s.empty? + @mpw.set_otp_key(item.id, row['otp_key']) if not row['otp_key'].to_s.empty? + end + @mpw.write_data puts "#{I18n.t('form.import.valid')}".green diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index c2befb1..a0c4c6e 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -274,28 +274,6 @@ class MPW return nil end - # Import to yaml - # @args: file -> path to file import - def import(file) - YAML::load_file(file).each_value do |row| - item = Item.new(group: row['group'], - host: row['host'], - protocol: row['protocol'], - user: row['user'], - port: row['port'], - comment: row['comment'], - ) - - raise 'Item is empty' if item.empty? - - @data.push(item) - set_password(item.id, row['password']) if not row['password'].to_s.empty? - set_otp_code(item.id, row['otp_key']) if not row['otp_key'].to_s.empty? - end - rescue Exception => e - raise "#{I18n.t('error.import', file: file)}\n#{e}" - end - # Get last sync def get_last_sync return @config['sync']['last_sync'].to_i