1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-02-20 01:50:04 +00:00

new binary for import

This commit is contained in:
Adrien Waksberg 2016-10-19 16:12:13 +02:00
parent 7c24853e5e
commit 92168dd654
3 changed files with 79 additions and 23 deletions

59
bin/mpw-import Normal file
View file

@ -0,0 +1,59 @@
#!/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('-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])

View file

@ -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

View file

@ -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