1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +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

@ -126,9 +126,9 @@ module MPW
def list(options={}) def list(options={})
result = [] result = []
search = options[:search].to_s.downcase search = options[:search].to_s.downcase
group = options[:group].to_s.downcase group = options[:group].to_s.downcase
protocol = options[:protocol].to_s.downcase protocol = options[:protocol].to_s.downcase
@data.each do |item| @data.each do |item|
next if not group.empty? and not group.eql?(item.group.downcase) next if not group.empty? and not group.eql?(item.group.downcase)
@ -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