From 2f686ca1631219ff4aea958bb6969377241e9160 Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Wed, 19 Oct 2016 15:08:08 +0200 Subject: [PATCH] new binary for export --- bin/mpw-export | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/mpw/cli.rb | 27 ++++++++++++++++++-- lib/mpw/mpw.rb | 28 --------------------- 3 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 bin/mpw-export diff --git a/bin/mpw-export b/bin/mpw-export new file mode 100644 index 0000000..700d067 --- /dev/null +++ b/bin/mpw-export @@ -0,0 +1,67 @@ +#!/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('-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.export(options[:file], opts) diff --git a/lib/mpw/cli.rb b/lib/mpw/cli.rb index b92d5fb..daf8543 100644 --- a/lib/mpw/cli.rb +++ b/lib/mpw/cli.rb @@ -495,8 +495,31 @@ class Cli # Export the items in a CSV file # @args: file -> the destination file - def export(file) - @mpw.export(file) + # options -> option to search + def export(file, options) + file = 'export-mpw.yml' if file.to_s.empty? + items = @mpw.list(options) + data = {} + i = 1 + + items.each do |item| + data.merge!(i => { 'host' => item.host, + 'user' => item.user, + 'group' => item.group, + 'password' => @mpw.get_password(item.id), + 'protocol' => item.protocol, + 'port' => item.port, + 'otp_key' => @mpw.get_otp_code(item.id), + 'comment' => item.comment, + 'last_edit' => item.last_edit, + 'created' => item.created, + } + ) + + i += 1 + end + + File.open(file, 'w') {|f| f << data.to_yaml} puts "#{I18n.t('export.export.valid', file)}".green rescue Exception => e diff --git a/lib/mpw/mpw.rb b/lib/mpw/mpw.rb index f1ae35e..10c57d0 100644 --- a/lib/mpw/mpw.rb +++ b/lib/mpw/mpw.rb @@ -274,34 +274,6 @@ class MPW return nil end - # Export to yaml - # @args: file -> file where you export the data - def export(file) - data = {} - i = 1 - - @data.each do |item| - data.merge!(i => { 'group' => item.group, - 'host' => item.host, - 'protocol' => item.protocol, - 'user' => item.user, - 'password' => get_password(item.id), - 'port' => item.port, - 'comment' => item.comment, - 'otp_key' => get_otp_code(item.id), - 'last_edit' => item.last_edit, - 'created' => item.created, - } - ) - - i += 1 - end - - File.open(file, 'w') {|f| f << data.to_yaml} - rescue Exception => e - raise "#{I18n.t('error.export', file: file)}\n#{e}" - end - # Import to yaml # @args: file -> path to file import def import(file)