diff --git a/Cli.rb b/Cli.rb index fa2450c..a254ee1 100644 --- a/Cli.rb +++ b/Cli.rb @@ -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 diff --git a/MPW.rb b/MPW.rb index 49ba93d..dde5db3 100644 --- a/MPW.rb +++ b/MPW.rb @@ -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 diff --git a/mpw b/mpw index cf9ecbd..7a5e483 100755 --- a/mpw +++ b/mpw @@ -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"