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:
parent
7f45037366
commit
4cc134afb4
2 changed files with 38 additions and 9 deletions
45
lib/MPW.rb
45
lib/MPW.rb
|
@ -126,9 +126,9 @@ module MPW
|
|||
def list(options={})
|
||||
result = []
|
||||
|
||||
search = options[:search].to_s.downcase
|
||||
group = options[:group].to_s.downcase
|
||||
protocol = options[:protocol].to_s.downcase
|
||||
search = options[:search].to_s.downcase
|
||||
group = options[:group].to_s.downcase
|
||||
protocol = options[:protocol].to_s.downcase
|
||||
|
||||
@data.each do |item|
|
||||
next if not group.empty? and not group.eql?(item.group.downcase)
|
||||
|
@ -259,22 +259,49 @@ module MPW
|
|||
# @args: file -> path to file import
|
||||
# @rtrn: a hash with the items to import, if there is an error return false
|
||||
def import_preview(file, type=:yaml)
|
||||
result = []
|
||||
data = []
|
||||
|
||||
case type
|
||||
when :csv
|
||||
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
|
||||
|
||||
when :yaml
|
||||
YAML::load_file(file).each do |k, row|
|
||||
result << row
|
||||
YAML::load_file(file).each_value do |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
|
||||
|
||||
else
|
||||
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
|
||||
return false
|
||||
end
|
||||
|
||||
return result
|
||||
|
||||
return data
|
||||
rescue Exception => e
|
||||
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
|
||||
return false
|
||||
|
|
|
@ -298,8 +298,10 @@ class Cli
|
|||
|
||||
if not force
|
||||
result = @mpw.import_preview(file, type)
|
||||
puts result
|
||||
if result.is_a?(Array) and not result.empty?
|
||||
result.each do |r|
|
||||
puts r.class
|
||||
display_item(r)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue