1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-30 08:53:05 +00:00

change the data format csv to yaml

This commit is contained in:
nishiki 2014-12-06 16:24:40 +01:00
parent 7ed067b1b7
commit a055925866
2 changed files with 90 additions and 96 deletions

View file

@ -31,9 +31,8 @@ module MPW
if File.exist?(@file_gpg) if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(armor: true) crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: passwd).read data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: passwd).read.force_encoding('utf-8')
@data = YAML.load(data_decrypt)
@data = CSV.parse(data_decrypt, {headers: true, header_converters: :symbol})
end end
return true return true
@ -45,17 +44,9 @@ module MPW
# Encrypt a file # Encrypt a file
# @rtrn: true if the file has been encrypted # @rtrn: true if the file has been encrypted
def encrypt def encrypt
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
crypto = GPGME::Crypto.new(armor: true) data_to_encrypt = @data.to_yaml
file_gpg = File.open(@file_gpg, 'w+')
data_to_encrypt = CSV.generate(write_headers: true,
headers: ['id', 'name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment', 'date']) do |csv|
@data.each do |r|
csv << [r[:id], r[:name], r[:group], r[:protocol], r[:host], r[:login], r[:password], r[:port], r[:comment], r[:date]]
end
end
recipients = [] recipients = []
recipients.push(@key) recipients.push(@key)
@ -63,14 +54,16 @@ module MPW
@share_keys.split.each { |k| recipients.push(k) } @share_keys.split.each { |k| recipients.push(k) }
end end
crypto = GPGME::Crypto.new(armor: true)
file_gpg = File.open(@file_gpg, 'w+')
crypto.encrypt(data_to_encrypt, recipients: recipients, output: file_gpg) crypto.encrypt(data_to_encrypt, recipients: recipients, output: file_gpg)
file_gpg.close file_gpg.close
FileUtils.rm("#{@file_gpg}.bk") FileUtils.rm("#{@file_gpg}.bk") if File.exist?("#{@file_gpg}.bk")
return true return true
rescue Exception => e rescue Exception => e
@error_msg = "#{I18n.t('error.gpg_file.encrypt')}\n#{e}" @error_msg = "#{I18n.t('error.gpg_file.encrypt')}\n#{e}"
FileUtils.mv("#{@file_gpg}.bk", @file_gpg) FileUtils.mv("#{@file_gpg}.bk", @file_gpg) if File.exist?("#{@file_gpg}.bk")
return false return false
end end
@ -84,12 +77,11 @@ module MPW
if not search.nil? if not search.nil?
search = search.downcase search = search.downcase
end end
search = search.force_encoding('ASCII-8BIT')
@data.each do |row| @data.each do |id, row|
name = row[:name].nil? ? nil : row[:name].downcase name = row['name'].nil? ? nil : row['name'].downcase
server = row[:host].nil? ? nil : row[:host].downcase server = row['host'].nil? ? nil : row['host'].downcase
comment = row[:comment].nil? ? nil : row[:comment].downcase comment = row['comment'].nil? ? nil : row['comment'].downcase
if name =~ /^.*#{search}.*$/ or server =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/ if name =~ /^.*#{search}.*$/ or server =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/
if (protocol.nil? or protocol.eql?(row[:protocol])) and (group.nil? or group.eql?(row[:group])) if (protocol.nil? or protocol.eql?(row[:protocol])) and (group.nil? or group.eql?(row[:group]))
@ -102,13 +94,11 @@ module MPW
end end
# Search in some csv data # Search in some csv data
# @args: id -> the id item # @args: id_search -> the id item
# @rtrn: a row with the resultat of the search # @rtrn: a row with the resultat of the search
def search_by_id(id) def search_by_id(id_search)
@data.each do |row| @data.each do |id, row|
if row[:id] == id return row if id == id_search
return row
end
end end
return [] return []
@ -130,49 +120,35 @@ module MPW
update = false update = false
i = 0 i = 0
@data.each do |r| if @data.has_key?(id)
if r[:id] == id row = @data[id]
row = r
update = true
break
end
i += 1
end end
if port.to_i <= 0 if port.to_i <= 0
port = nil port = nil
end end
row_update = {} row_update = {}
row_update[:date] = Time.now.to_i row_update['id'] = id.to_s.empty? ? MPW.password(16) : id
row_update['name'] = name.to_s.empty? ? row['name'] : name
row_update['group'] = group.to_s.empty? ? row['group'] : group
row_update['host'] = server.to_s.empty? ? row['host'] : server
row_update['protocol'] = protocol.to_s.empty? ? row['protocol'] : protocol
row_update['login'] = login.to_s.empty? ? row['login'] : login
row_update['password'] = passwd.to_s.empty? ? row['password'] : passwd
row_update['port'] = port.to_s.empty? ? row['port'] : port
row_update['comment'] = comment.to_s.empty? ? row['comment'] : comment
row_update['date'] = Time.now.to_i
row_update[:id] = id.nil? or id.empty? ? MPW.password(16) : id if row_update['name'].to_s.empty?
row_update[:name] = name.nil? or name.empty? ? row[:name] : name
row_update[:group] = group.nil? or group.empty? ? row[:group] : group
row_update[:host] = server.nil? or server.empty? ? row[:host] : server
row_update[:protocol] = protocol.nil? or protocol.empty? ? row[:protocol] : protocol
row_update[:login] = login.nil? or login.empty? ? row[:login] : login
row_update[:password] = passwd.nil? or passwd.empty? ? row[:password] : passwd
row_update[:port] = port.nil? or port.empty? ? row[:port] : port
row_update[:comment] = comment.nil? or comment.empty? ? row[:comment] : comment
row_update[:name] = row_update[:name].nil? ? nil : row_update[:name].force_encoding('ASCII-8BIT')
row_update[:group] = row_update[:group].nil? ? nil : row_update[:group].force_encoding('ASCII-8BIT')
row_update[:host] = row_update[:host].nil? ? nil : row_update[:host].force_encoding('ASCII-8BIT')
row_update[:protocol] = row_update[:protocol].nil? ? nil : row_update[:protocol].force_encoding('ASCII-8BIT')
row_update[:login] = row_update[:login].nil? ? nil : row_update[:login].force_encoding('ASCII-8BIT')
row_update[:password] = row_update[:password].nil? ? nil : row_update[:password].force_encoding('ASCII-8BIT')
row_update[:comment] = row_update[:comment].nil? ? nil : row_update[:comment].force_encoding('ASCII-8BIT')
if row_update[:name].nil? or row_update[:name].empty?
@error_msg = I18n.t('error.update.name_empty') @error_msg = I18n.t('error.update.name_empty')
return false return false
end end
if update if update
@data[i] = row_update @data[id] = row_update
else else
@data.push(row_update) @data[row_update['id']] = row_update
end end
return true return true
@ -182,13 +158,11 @@ module MPW
# @args: id -> the item's identifiant # @args: id -> the item's identifiant
# @rtrn: true if the item has been deleted # @rtrn: true if the item has been deleted
def remove(id) def remove(id)
i = 0 @data.each do |k, row|
@data.each do |row| if k == id
if row[:id] == id @data.delete(id)
@data.delete_at(i)
return true return true
end end
i += 1
end end
@error_msg = I18n.t('error.delete.id_no_exist', id: id) @error_msg = I18n.t('error.delete.id_no_exist', id: id)
@ -197,36 +171,33 @@ module MPW
# Export to csv # Export to csv
# @args: file -> file where you export the data # @args: file -> file where you export the data
# type -> data type # type -> udata type
# @rtrn: true if export work # @rtrn: true if export work
def export(file, type=:csv) def export(file, type=:csv)
case type case type
when :csv when :csv
CSV.open(file, 'w', write_headers: true, CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv| headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
@data.each do |r| @data.each do |id, r|
csv << [r[:name], r[:group], r[:protocol], r[:host], r[:login], r[:password], r[:port], r[:comment]] csv << [r['name'], r['group'], r['protocol'], r['host'], r['login'], r['password'], r['port'], r['comment']]
end end
end end
when :yaml when :yaml
data = {} data = {}
i = 0 @data.each do |id, r|
@data.each do |r| data.merge!({id => {'id' => r['id'],
data.merge!({i => {'id' => r[:id], 'name' => r['name'],
'name' => r[:name], 'group' => r['group'],
'group' => r[:group], 'protocol' => r['protocol'],
'protocol' => r[:protocol], 'host' => r['host'],
'host' => r[:host], 'login' => r['login'],
'login' => r[:login], 'password' => r['password'],
'password' => r[:password], 'port' => r['port'],
'port' => r[:port], 'comment' => r['comment'],
'comment' => r[:comment] }
}
} }
) )
i += 1
end end
File.open(file, 'w') {|f| f << data.to_yaml} File.open(file, 'w') {|f| f << data.to_yaml}
@ -243,12 +214,25 @@ module MPW
# Import to csv # Import to csv
# @args: file -> path to file import # @args: file -> path to file import
# type -> udata type
# @rtrn: true if the import work # @rtrn: true if the import work
def import(file) def import(file, type=:csv)
CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row| case type
if not update(row[:name], row[:group], row[:host], row[:protocol], row[:login], row[:password], row[:port], row[:comment]) when :csv
return false 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
end
end end
when :yaml
YAML::load_file(file).each do |k, row|
if not update(row['name'], row['group'], row['host'], row['protocol'], row['login'], row['password'], row['port'], row['comment'])
return false
end
end
else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false
end end
return true return true
@ -260,10 +244,20 @@ module MPW
# Return a preview import # Return a preview import
# @args: file -> path to file import # @args: file -> path to file import
# @rtrn: an array with the items to import, if there is an error return false # @rtrn: an array with the items to import, if there is an error return false
def import_preview(file) def import_preview(file, type=:csv)
result = [] result = []
CSV.foreach(file, {headers: true, header_converters: :symbol}) do |row| case type
result << row when :csv
CSV.foreach(file, {headers: true}) do |row|
result << row
end
when :yaml
YAML::load_file(file).each do |k, row|
result << row
end
else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
return false
end end
return result return result

View file

@ -77,7 +77,7 @@ class Cli
language = ask(I18n.t('form.setup.lang', lang: lang)).to_s language = ask(I18n.t('form.setup.lang', lang: lang)).to_s
key = ask(I18n.t('form.setup.gpg_key')).to_s key = ask(I18n.t('form.setup.gpg_key')).to_s
share_keys = ask(I18n.t('form.setup.share_gpg_keys')).to_s share_keys = ask(I18n.t('form.setup.share_gpg_keys')).to_s
file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_home)).to_s file_gpg = ask(I18n.t('form.setup.gpg_file', home: @conf.dir_config)).to_s
timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s timeout_pwd = ask(I18n.t('form.setup.timeout')).to_s
sync_type = ask(I18n.t('form.setup.sync_type')).to_s sync_type = ask(I18n.t('form.setup.sync_type')).to_s
@ -187,15 +187,15 @@ class Cli
# @args: item -> an array with the item information # @args: item -> an array with the item information
def displayFormat(item) def displayFormat(item)
puts '--------------------' puts '--------------------'
puts "Id: #{item[:id]}" puts "Id: #{item['id']}"
puts "#{I18n.t('display.name')}: #{item[:name]}" puts "#{I18n.t('display.name')}: #{item['name']}"
puts "#{I18n.t('display.group')}: #{item[:group]}" puts "#{I18n.t('display.group')}: #{item['group']}"
puts "#{I18n.t('display.server')}: #{item[:host]}" puts "#{I18n.t('display.server')}: #{item['host']}"
puts "#{I18n.t('display.protocol')}: #{item[:protocol]}" puts "#{I18n.t('display.protocol')}: #{item['protocol']}"
puts "#{I18n.t('display.login')}: #{item[:login]}" puts "#{I18n.t('display.login')}: #{item['login']}"
puts "#{I18n.t('display.password')}: #{item[:password]}" puts "#{I18n.t('display.password')}: #{item['password']}"
puts "#{I18n.t('display.port')}: #{item[:port]}" puts "#{I18n.t('display.port')}: #{item['port']}"
puts "#{I18n.t('display.comment')}: #{item[:comment]}" puts "#{I18n.t('display.comment')}: #{item['comment']}"
end end
# Display an item in the alternative format # Display an item in the alternative format
@ -320,9 +320,9 @@ class Cli
# @args: file -> the import file # @args: file -> the import file
# force -> no resquest a validation # force -> no resquest a validation
def import(file, force=false) def import(file, force=false)
result = @mpw.import_preview(file)
if not force if not force
result = @mpw.import_preview(file)
if result.is_a?(Array) && !result.empty? if result.is_a?(Array) && !result.empty?
result.each do |r| result.each do |r|
displayFormat(r) displayFormat(r)