1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-23 13:57:52 +00:00

fix import and export

This commit is contained in:
nishiki 2014-08-31 17:40:37 +02:00
parent 1d3b127df0
commit be00f04353

View file

@ -26,7 +26,7 @@ module MPW
# @args: password -> the GPG key password
# @rtrn: true if data has been decrypted
def decrypt(passwd=nil)
@data = {}
@data = []
if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(armor: true)
@ -54,9 +54,6 @@ module MPW
end
end
puts 'test'
puts data_to_encrypt
recipients = []
recipients.push(@key)
if !@share_keys.nil?
@share_keys.split.each { |k| recipients.push(k) }
@ -196,10 +193,10 @@ module MPW
# @args: file -> a string to match
# @rtrn: true if export work
def export(file)
File.open(file, 'w+') do |file|
@data.each do |row|
row.delete_at(:id).delete_at(:date)
file << row.to_csv
CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
@data.each do |r|
csv << [r[:name], r[:group], r[:protocol], r[:host], r[:login], r[:password], r[:port], r[:comment]]
end
end
@ -213,16 +210,9 @@ module MPW
# @args: file -> path to file import
# @rtrn: true if the import work
def import(file)
data_new = IO.read(file)
data_new.lines do |line|
if not line =~ /(.*,){6}/
@error_msg = I18n.t('error.import.bad_format')
CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row|
if not update(row[:name], row[:group], row[:host], row[:protocol], row[:login], row[:password], row[:port], row[:comment])
return false
else
row = line.parse_csv.unshift(0)
if not update(row[:name], row[:group], row[:host], row[:protocol], row[:login], row[:password], row[:port], row[:comment])
return false
end
end
end
@ -237,18 +227,8 @@ module MPW
# @rtrn: an array with the items to import, if there is an error return false
def import_preview(file)
result = []
id = 0
data = IO.read(file)
data.lines do |line|
if not line =~ /(.*,){6}/
@error_msg = I18n.t('error.import.bad_format')
return false
else
result.push(line.parse_csv.unshift(id))
end
id += 1
CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row|
result << row
end
return result