mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-22 02:40:04 +00:00
add import and export to csv
This commit is contained in:
parent
e7776e3bc1
commit
9611f8b23e
3 changed files with 79 additions and 1 deletions
22
Cli.rb
22
Cli.rb
|
@ -136,6 +136,28 @@ class Cli
|
||||||
end
|
end
|
||||||
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)
|
def ssh(search)
|
||||||
@m.ssh(search)
|
@m.ssh(search)
|
||||||
end
|
end
|
||||||
|
|
42
MPW.rb
42
MPW.rb
|
@ -279,8 +279,48 @@ class MPW
|
||||||
return removed
|
return removed
|
||||||
end
|
end
|
||||||
|
|
||||||
# Connect to ssh && display the password
|
|
||||||
|
# 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
|
# @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: file -> a string to match
|
||||||
# @rtrn: true if ssh connection work
|
# @rtrn: true if ssh connection work
|
||||||
def ssh(search)
|
def ssh(search)
|
||||||
result = self.search(search, 'ssh')
|
result = self.search(search, 'ssh')
|
||||||
|
|
16
mpw
16
mpw
|
@ -47,6 +47,14 @@ OptionParser.new do |opts|
|
||||||
options[:type] = type
|
options[:type] = type
|
||||||
end
|
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|
|
opts.on("-f", "--force", "Force an action") do |b|
|
||||||
options[:force] = true
|
options[:force] = true
|
||||||
end
|
end
|
||||||
|
@ -92,6 +100,14 @@ elsif not options[:ssh].nil?
|
||||||
elsif not options[:add].nil?
|
elsif not options[:add].nil?
|
||||||
cli.add()
|
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
|
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