mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-21 10:20:05 +00:00
add import preview
This commit is contained in:
parent
a37139e4a9
commit
04dde7cbd2
3 changed files with 64 additions and 10 deletions
41
Cli.rb
41
Cli.rb
|
@ -151,7 +151,7 @@ class Cli
|
||||||
puts "# Port: #{result[MPW::PORT]}"
|
puts "# Port: #{result[MPW::PORT]}"
|
||||||
puts "# Comment: #{result[MPW::COMMENT]}"
|
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)$/
|
if confirm =~ /^(y|yes|YES|Yes|Y)$/
|
||||||
force = true
|
force = true
|
||||||
end
|
end
|
||||||
|
@ -183,17 +183,44 @@ class Cli
|
||||||
|
|
||||||
# Import items from a CSV file
|
# Import items from a CSV file
|
||||||
# @args: file -> the import file
|
# @args: file -> the import file
|
||||||
def import(file)
|
# force -> no resquest a validation
|
||||||
if @m.import(file)
|
def import(file, force=false)
|
||||||
if @m.encrypt()
|
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!"
|
puts "The import is succesfull!"
|
||||||
else
|
else
|
||||||
puts "ERROR: #{@m.error_msg}"
|
puts "ERROR: #{@m.error_msg}"
|
||||||
end
|
end
|
||||||
else
|
|
||||||
puts "ERROR: #{@m.error_msg}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
26
MPW.rb
26
MPW.rb
|
@ -313,7 +313,7 @@ class MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
# Import to csv
|
# Import to csv
|
||||||
# @args: search -> a string to match
|
# @args: file -> path to file import
|
||||||
# @rtrn: true if the import work
|
# @rtrn: true if the import work
|
||||||
def import(file)
|
def import(file)
|
||||||
begin
|
begin
|
||||||
|
@ -332,5 +332,29 @@ class MPW
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
7
mpw
7
mpw
|
@ -98,8 +98,11 @@ elsif not options[:export].nil?
|
||||||
|
|
||||||
# Add a new item
|
# Add a new item
|
||||||
elsif not options[:import].nil?
|
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
|
else
|
||||||
puts "For help add option -h or --help"
|
puts "For help add option -h or --help"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue