mirror of
https://github.com/nishiki/manage-password.git
synced 2024-11-27 07:33:05 +00:00
improve csv with symbole
This commit is contained in:
parent
be2fb0fd3f
commit
f06059518c
2 changed files with 75 additions and 84 deletions
115
MPW/MPW.rb
115
MPW/MPW.rb
|
@ -12,17 +12,6 @@ module MPW
|
||||||
|
|
||||||
class MPW
|
class MPW
|
||||||
|
|
||||||
ID = 0
|
|
||||||
NAME = 1
|
|
||||||
GROUP = 2
|
|
||||||
PROTOCOL = 3
|
|
||||||
SERVER = 4
|
|
||||||
LOGIN = 5
|
|
||||||
PASSWORD = 6
|
|
||||||
PORT = 7
|
|
||||||
COMMENT = 8
|
|
||||||
DATE = 9
|
|
||||||
|
|
||||||
attr_accessor :error_msg
|
attr_accessor :error_msg
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
|
@ -37,15 +26,13 @@ module MPW
|
||||||
# @args: password -> the GPG key password
|
# @args: password -> the GPG key password
|
||||||
# @rtrn: true if data has been decrypted
|
# @rtrn: true if data has been decrypted
|
||||||
def decrypt(passwd=nil)
|
def decrypt(passwd=nil)
|
||||||
@data = []
|
@data = {}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
data_decrypt.lines do |line|
|
@data = CSV.parse(data_decrypt, {headers: true, header_converters: :symbol})
|
||||||
@data.push(line.parse_csv)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -57,21 +44,25 @@ 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
|
||||||
crypto = GPGME::Crypto.new(:armor => true)
|
crypto = GPGME::Crypto.new(armor: true)
|
||||||
file_gpg = File.open(@file_gpg, 'w+')
|
file_gpg = File.open(@file_gpg, 'w+')
|
||||||
|
|
||||||
data_to_encrypt = ''
|
data_to_encrypt = CSV.generate(write_headers: true,
|
||||||
@data.each do |row|
|
headers: ['id', 'name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment', 'date']) do |csv|
|
||||||
data_to_encrypt << row.to_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
|
end
|
||||||
|
|
||||||
|
puts 'test'
|
||||||
|
puts data_to_encrypt
|
||||||
recipients = []
|
recipients = []
|
||||||
recipients.push(@key)
|
recipients.push(@key)
|
||||||
if !@share_keys.nil?
|
if !@share_keys.nil?
|
||||||
@share_keys.split.each { |k| recipients.push(k) }
|
@share_keys.split.each { |k| recipients.push(k) }
|
||||||
end
|
end
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
@ -93,12 +84,12 @@ module MPW
|
||||||
search = search.force_encoding('ASCII-8BIT')
|
search = search.force_encoding('ASCII-8BIT')
|
||||||
|
|
||||||
@data.each do |row|
|
@data.each do |row|
|
||||||
name = row[NAME].nil? ? nil : row[NAME].downcase
|
name = row[:name].nil? ? nil : row[:name].downcase
|
||||||
server = row[SERVER].nil? ? nil : row[SERVER].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}.*$/ || server =~ /^.*#{search}.*$/ || comment =~ /^.*#{search}.*$/
|
if name =~ /^.*#{search}.*$/ || server =~ /^.*#{search}.*$/ || comment =~ /^.*#{search}.*$/
|
||||||
if (protocol.nil? || protocol.eql?(row[PROTOCOL])) && (group.nil? || group.eql?(row[GROUP]))
|
if (protocol.nil? || protocol.eql?(row[:protocol])) && (group.nil? || group.eql?(row[:group]))
|
||||||
result.push(row)
|
result.push(row)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -112,7 +103,7 @@ module MPW
|
||||||
# @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)
|
||||||
@data.each do |row|
|
@data.each do |row|
|
||||||
if row[ID] == id
|
if row[:id] == id
|
||||||
return row
|
return row
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -132,12 +123,12 @@ module MPW
|
||||||
# comment -> a comment
|
# comment -> a comment
|
||||||
# @rtrn: true if the item has been updated
|
# @rtrn: true if the item has been updated
|
||||||
def update(name, group, server, protocol, login, passwd, port, comment, id=nil)
|
def update(name, group, server, protocol, login, passwd, port, comment, id=nil)
|
||||||
row = []
|
row = {}
|
||||||
update = false
|
update = false
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
@data.each do |r|
|
@data.each do |r|
|
||||||
if r[ID] == id
|
if r[:id] == id
|
||||||
row = r
|
row = r
|
||||||
update = true
|
update = true
|
||||||
break
|
break
|
||||||
|
@ -149,28 +140,28 @@ module MPW
|
||||||
port = nil
|
port = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
row_update = []
|
row_update = {}
|
||||||
row_update[DATE] = Time.now.to_i
|
row_update[:date] = Time.now.to_i
|
||||||
|
|
||||||
row_update[ID] = id.nil? || id.empty? ? MPW.password(16) : id
|
row_update[:id] = id.nil? || id.empty? ? MPW.password(16) : id
|
||||||
row_update[NAME] = name.nil? || name.empty? ? row[NAME] : name
|
row_update[:name] = name.nil? || name.empty? ? row[:name] : name
|
||||||
row_update[GROUP] = group.nil? || group.empty? ? row[GROUP] : group
|
row_update[:group] = group.nil? || group.empty? ? row[:group] : group
|
||||||
row_update[SERVER] = server.nil? || server.empty? ? row[SERVER] : server
|
row_update[:host] = server.nil? || server.empty? ? row[:host] : server
|
||||||
row_update[PROTOCOL] = protocol.nil? || protocol.empty? ? row[PROTOCOL] : protocol
|
row_update[:protocol] = protocol.nil? || protocol.empty? ? row[:protocol] : protocol
|
||||||
row_update[LOGIN] = login.nil? || login.empty? ? row[LOGIN] : login
|
row_update[:login] = login.nil? || login.empty? ? row[:login] : login
|
||||||
row_update[PASSWORD] = passwd.nil? || passwd.empty? ? row[PASSWORD] : passwd
|
row_update[:password] = passwd.nil? || passwd.empty? ? row[:password] : passwd
|
||||||
row_update[PORT] = port.nil? || port.empty? ? row[PORT] : port
|
row_update[:port] = port.nil? || port.empty? ? row[:port] : port
|
||||||
row_update[COMMENT] = comment.nil? || comment.empty? ? row[COMMENT] : comment
|
row_update[:comment] = comment.nil? || comment.empty? ? row[:comment] : comment
|
||||||
|
|
||||||
row_update[NAME] = row_update[NAME].nil? ? nil : row_update[NAME].force_encoding('ASCII-8BIT')
|
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[:group] = row_update[:group].nil? ? nil : row_update[:group].force_encoding('ASCII-8BIT')
|
||||||
row_update[SERVER] = row_update[SERVER].nil? ? nil : row_update[SERVER].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[: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[: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[: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')
|
row_update[:comment] = row_update[:comment].nil? ? nil : row_update[:comment].force_encoding('ASCII-8BIT')
|
||||||
|
|
||||||
if row_update[NAME].nil? || row_update[NAME].empty?
|
if row_update[:name].nil? || 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
|
||||||
|
@ -190,14 +181,14 @@ module MPW
|
||||||
def remove(id)
|
def remove(id)
|
||||||
i = 0
|
i = 0
|
||||||
@data.each do |row|
|
@data.each do |row|
|
||||||
if row[ID] == id
|
if row[:id] == id
|
||||||
@data.delete_at(i)
|
@data.delete_at(i)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
i += 1
|
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)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -207,14 +198,14 @@ module MPW
|
||||||
def export(file)
|
def export(file)
|
||||||
File.open(file, 'w+') do |file|
|
File.open(file, 'w+') do |file|
|
||||||
@data.each do |row|
|
@data.each do |row|
|
||||||
row.delete_at(ID).delete_at(DATE)
|
row.delete_at(:id).delete_at(:date)
|
||||||
file << row.to_csv
|
file << row.to_csv
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.export.write', :file => file)}\n#{e}"
|
@error_msg = "#{I18n.t('error.export.write', file: file)}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -229,7 +220,7 @@ module MPW
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
row = line.parse_csv.unshift(0)
|
row = line.parse_csv.unshift(0)
|
||||||
if not update(row[NAME], row[GROUP], row[SERVER], row[PROTOCOL], row[LOGIN], row[PASSWORD], row[PORT], row[COMMENT])
|
if not update(row[:name], row[:group], row[:host], row[:protocol], row[:login], row[:password], row[:port], row[:comment])
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -237,7 +228,7 @@ module MPW
|
||||||
|
|
||||||
return true
|
return true
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}"
|
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -262,7 +253,7 @@ module MPW
|
||||||
|
|
||||||
return result
|
return result
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
@error_msg = "#{I18n.t('error.import.read', :file => file)}\n#{e}"
|
@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -280,9 +271,9 @@ module MPW
|
||||||
|
|
||||||
# Update item
|
# Update item
|
||||||
data_remote.each do |r|
|
data_remote.each do |r|
|
||||||
if l[ID] == r[ID]
|
if l[:id] == r[:id]
|
||||||
if l[DATE].to_i < r[DATE].to_i
|
if l[:date].to_i < r[:date].to_i
|
||||||
update(r[NAME], r[GROUP], r[SERVER], r[PROTOCOL], r[LOGIN], r[PASSWORD], r[PORT], r[COMMENT], l[ID])
|
update(r[:name], r[:group], r[:host], r[:protocol], r[:login], r[:password], r[:port], r[:comment], l[:id])
|
||||||
end
|
end
|
||||||
update = true
|
update = true
|
||||||
data_remote.delete_at(j)
|
data_remote.delete_at(j)
|
||||||
|
@ -292,16 +283,16 @@ module MPW
|
||||||
end
|
end
|
||||||
|
|
||||||
# Delete an old item
|
# Delete an old item
|
||||||
if !update && l[DATE].to_i < last_update
|
if !update && l[:date].to_i < last_update
|
||||||
remove(l[ID])
|
remove(l[:id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add item
|
# Add item
|
||||||
data_remote.each do |r|
|
data_remote.each do |r|
|
||||||
if r[DATE].to_i > last_update
|
if r[:date].to_i > last_update
|
||||||
update(r[NAME], r[GROUP], r[SERVER], r[PROTOCOL], r[LOGIN], r[PASSWORD], r[PORT], r[COMMENT], r[ID])
|
update(r[:name], r[:group], r[:host], r[:protocol], r[:login], r[:password], r[:port], r[:comment], r[:id])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -187,30 +187,30 @@ 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[MPW::MPW::ID]}"
|
puts "Id: #{item[:id]}"
|
||||||
puts "#{I18n.t('display.name')}: #{item[MPW::MPW::NAME]}"
|
puts "#{I18n.t('display.name')}: #{item[:name]}"
|
||||||
puts "#{I18n.t('display.group')}: #{item[MPW::MPW::GROUP]}"
|
puts "#{I18n.t('display.group')}: #{item[:group]}"
|
||||||
puts "#{I18n.t('display.server')}: #{item[MPW::MPW::SERVER]}"
|
puts "#{I18n.t('display.server')}: #{item[:host]}"
|
||||||
puts "#{I18n.t('display.protocol')}: #{item[MPW::MPW::PROTOCOL]}"
|
puts "#{I18n.t('display.protocol')}: #{item[:protocol]}"
|
||||||
puts "#{I18n.t('display.login')}: #{item[MPW::MPW::LOGIN]}"
|
puts "#{I18n.t('display.login')}: #{item[:login]}"
|
||||||
puts "#{I18n.t('display.password')}: #{item[MPW::MPW::PASSWORD]}"
|
puts "#{I18n.t('display.password')}: #{item[:password]}"
|
||||||
puts "#{I18n.t('display.port')}: #{item[MPW::MPW::PORT]}"
|
puts "#{I18n.t('display.port')}: #{item[:port]}"
|
||||||
puts "#{I18n.t('display.comment')}: #{item[MPW::MPW::COMMENT]}"
|
puts "#{I18n.t('display.comment')}: #{item[:comment]}"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Display an item in the alternative format
|
# Display an item in the alternative format
|
||||||
# @args: item -> an array with the item information
|
# @args: item -> an array with the item information
|
||||||
def displayFormatAlt(item)
|
def displayFormatAlt(item)
|
||||||
port = item[MPW::MPW::PORT].nil? ? '' : ":#{item[MPW::MPW::PORT]}"
|
port = item[:port].nil? ? '' : ":#{item[:port]}"
|
||||||
|
|
||||||
if item[MPW::MPW::PASSWORD].nil? || item[MPW::MPW::PASSWORD].empty?
|
if item[:password].nil? || item[:password].empty?
|
||||||
if item[MPW::MPW::LOGIN].include('@')
|
if item[:login].include('@')
|
||||||
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://#{item[MPW::MPW::LOGIN]}@#{item[MPW::MPW::SERVER]}#{port}"
|
puts "# #{item[:id]} #{item[:protocol]}://#{item[:login]}@#{item[:host]}#{port}"
|
||||||
else
|
else
|
||||||
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://{#{item[MPW::MPW::LOGIN]}}@#{item[MPW::MPW::SERVER]}#{port}"
|
puts "# #{item[:id]} #{item[:protocol]}://{#{item[:login]}}@#{item[:host]}#{port}"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
puts "# #{item[MPW::MPW::ID]} #{item[MPW::MPW::PROTOCOL]}://{#{item[MPW::MPW::LOGIN]}:#{item[MPW::MPW::PASSWORD]}}@#{item[MPW::MPW::SERVER]}#{port}"
|
puts "# #{item[:id]} #{item[:protocol]}://{#{item[:login]}:#{item[:password]}}@#{item[:host]}#{port}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -248,14 +248,14 @@ class Cli
|
||||||
if not row.empty?
|
if not row.empty?
|
||||||
puts I18n.t('form.update.title')
|
puts I18n.t('form.update.title')
|
||||||
puts '--------------------'
|
puts '--------------------'
|
||||||
name = ask(I18n.t('form.update.name' , :name => row[MPW::MPW::NAME])).to_s
|
name = ask(I18n.t('form.update.name' , name: row[:name])).to_s
|
||||||
group = ask(I18n.t('form.update.group' , :group => row[MPW::MPW::GROUP])).to_s
|
group = ask(I18n.t('form.update.group' , group: row[:group])).to_s
|
||||||
server = ask(I18n.t('form.update.server' , :server => row[MPW::MPW::SERVER])).to_s
|
server = ask(I18n.t('form.update.server' , server: row[:host])).to_s
|
||||||
protocol = ask(I18n.t('form.update.protocol', :protocol => row[MPW::MPW::PROTOCOL])).to_s
|
protocol = ask(I18n.t('form.update.protocol', protocol: row[:protocol])).to_s
|
||||||
login = ask(I18n.t('form.update.login' , :login => row[MPW::MPW::LOGIN])).to_s
|
login = ask(I18n.t('form.update.login' , login: row[:login])).to_s
|
||||||
passwd = ask(I18n.t('form.update.password')).to_s
|
passwd = ask(I18n.t('form.update.password')).to_s
|
||||||
port = ask(I18n.t('form.update.port' , :port => row[MPW::MPW::PORT])).to_s
|
port = ask(I18n.t('form.update.port' , port: row[:port])).to_s
|
||||||
comment = ask(I18n.t('form.update.comment' , :comment => row[MPW::MPW::COMMENT])).to_s
|
comment = ask(I18n.t('form.update.comment' , comment: row[:comment])).to_s
|
||||||
|
|
||||||
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
|
||||||
if @mpw.encrypt
|
if @mpw.encrypt
|
||||||
|
|
Loading…
Reference in a new issue