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

add import preview

This commit is contained in:
nishiki 2013-08-26 22:19:37 +02:00
parent a37139e4a9
commit 04dde7cbd2
3 changed files with 64 additions and 10 deletions

41
Cli.rb
View file

@ -151,7 +151,7 @@ class Cli
puts "# Port: #{result[MPW::PORT]}"
puts "# Comment: #{result[MPW::COMMENT]}"
confirm = ask("Are you sur to remove the item: #{id} ? (y/N) ")
confirm = ask("Are you sure to remove the item: #{id} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
@ -183,17 +183,44 @@ class Cli
# Import items from a CSV file
# @args: file -> the import file
def import(file)
if @m.import(file)
if @m.encrypt()
# force -> no resquest a validation
def import(file, force=false)
result = @m.importPreview(file)
if not force
if result.is_a?(Array) && !result.empty?
i = 0
result.each do |r|
puts "# --------------------"
puts "# Id: #{i}"
puts "# Name: #{r[MPW::NAME]}"
puts "# Group: #{r[MPW::GROUP]}"
puts "# Server: #{r[MPW::SERVER]}"
puts "# Type: #{r[MPW::PROTOCOL]}"
puts "# Login: #{r[MPW::LOGIN]}"
puts "# Password: #{r[MPW::PASSWORD]}"
puts "# Port: #{r[MPW::PORT]}"
puts "# Comment: #{r[MPW::COMMENT]}"
i += 1
end
confirm = ask("Are you sure to import this file: #{file} ? (y/N) ")
if confirm =~ /^(y|yes|YES|Yes|Y)$/
force = true
end
else
puts "No data to import!"
end
end
if force
if @m.import(file) && @m.encrypt()
puts "The import is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
else
puts "ERROR: #{@m.error_msg}"
end
end
end

26
MPW.rb
View file

@ -313,7 +313,7 @@ class MPW
end
# Import to csv
# @args: search -> a string to match
# @args: file -> path to file import
# @rtrn: true if the import work
def import(file)
begin
@ -332,5 +332,29 @@ class MPW
return false
end
end
# Return
# @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false
def importPreview(file)
begin
result = Array.new()
data = IO.read(file)
data.lines do |line|
if not line =~ /(.*,){6}/
@error_msg = "Can't import, the file is bad format!"
return false
else
result.push(line.parse_csv)
end
end
return result
rescue
@error_msg = "Can't import, impossible to read #{file}!"
return false
end
end
end

7
mpw
View file

@ -98,8 +98,11 @@ elsif not options[:export].nil?
# Add a new item
elsif not options[:import].nil?
cli.import(options[:import])
if not options[:force].nil?
cli.import(options[:import], options[:force])
else
cli.import(options[:import])
end
else
puts "For help add option -h or --help"