1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-03-20 21:34:35 +00:00

fix bug in preview import

This commit is contained in:
nishiki 2015-03-19 22:52:33 +01:00
parent 7f45037366
commit 4cc134afb4
2 changed files with 38 additions and 9 deletions

View file

@ -259,22 +259,49 @@ module MPW
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: a hash with the items to import, if there is an error return false # @rtrn: a hash with the items to import, if there is an error return false
def import_preview(file, type=:yaml) def import_preview(file, type=:yaml)
result = [] data = []
case type case type
when :csv when :csv
CSV.foreach(file, {headers: true}) do |row| CSV.foreach(file, {headers: true}) do |row|
result << row item = Item.new(name: row['name'],
group: row['group'],
host: row['host'],
protocol: row['protocol'],
user: row['user'],
password: row['password'],
port: row['port'],
comment: row['comment'],
)
return false if item.empty?
data.push(item)
end end
when :yaml when :yaml
YAML::load_file(file).each do |k, row| YAML::load_file(file).each_value do |row|
result << row item = Item.new(name: row['name'],
group: row['group'],
host: row['host'],
protocol: row['protocol'],
user: row['user'],
password: row['password'],
port: row['port'],
comment: row['comment'],
)
return false if item.empty?
data.push(item)
end end
else else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}" @error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false return false
end end
return result return data
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}" @error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
return false return false

View file

@ -298,8 +298,10 @@ class Cli
if not force if not force
result = @mpw.import_preview(file, type) result = @mpw.import_preview(file, type)
puts result
if result.is_a?(Array) and not result.empty? if result.is_a?(Array) and not result.empty?
result.each do |r| result.each do |r|
puts r.class
display_item(r) display_item(r)
end end