mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-20 01:50:04 +00:00
new binary for export
This commit is contained in:
parent
985de310fb
commit
2f686ca163
3 changed files with 92 additions and 30 deletions
67
bin/mpw-export
Normal file
67
bin/mpw-export
Normal file
|
@ -0,0 +1,67 @@
|
|||
#!/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 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)
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue