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

add import and export to csv

This commit is contained in:
nishiki 2013-07-25 19:51:43 +02:00
parent e7776e3bc1
commit 9611f8b23e
3 changed files with 79 additions and 1 deletions

22
Cli.rb
View file

@ -136,6 +136,28 @@ class Cli
end
end
def export(file)
if @m.export(file)
puts "The export in #{file} is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
end
def import(file)
if @m.import(file)
if @m.encrypt()
puts "The import is succesfull!"
else
puts "ERROR: #{@m.error_msg}"
end
else
puts "ERROR: #{@m.error_msg}"
end
end
def ssh(search)
@m.ssh(search)
end

42
MPW.rb
View file

@ -278,9 +278,49 @@ class MPW
return removed
end
# Export to csv
# @args: file -> a string to match
# @rtrn: true if export work
def export(file)
begin
File.open(file, 'w+') do |f|
f << @data
end
return true
rescue
@error_msg = "Can't export, impossible to write in #{file}!"
@error = 8
return false
end
end
# Import to csv
# @args: search -> a string to match
# @rtrn: true if the import work
def import(file)
begin
data_new = IO.read(file)
data_new.lines do |line|
if not line =~ /(.*,){6}/
@error_msg = "Can't import, the file is bad format!"
@error = 9
return false
end
end
@data << data_new
return true
rescue
@error_msg = "Can't import, impossible to read #{file}!"
@error = 10
return false
end
end
# Connect to ssh && display the password
# @args: search -> a string to match
# @args: file -> a string to match
# @rtrn: true if ssh connection work
def ssh(search)
result = self.search(search, 'ssh')

16
mpw
View file

@ -47,6 +47,14 @@ OptionParser.new do |opts|
options[:type] = type
end
opts.on("-e", "--export FILE", "Export to csv file") do |file|
options[:export] = file
end
opts.on("-i", "--import FILE", "Import from csv file") do |file|
options[:import] = file
end
opts.on("-f", "--force", "Force an action") do |b|
options[:force] = true
end
@ -92,6 +100,14 @@ elsif not options[:ssh].nil?
elsif not options[:add].nil?
cli.add()
# Export
elsif not options[:export].nil?
cli.export(options[:export])
# Add a new item
elsif not options[:import].nil?
cli.import(options[:import])
else
puts "For help add option -h or --help"