1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2024-11-27 07:33:05 +00:00

unstable: work in progress to Item class

This commit is contained in:
nishiki 2015-01-28 23:08:25 +01:00
parent 7e546b65d2
commit 16fd5d6c24
3 changed files with 79 additions and 60 deletions

View file

@ -67,6 +67,7 @@ module MPW
def set_name(name)
if name.to_s.empty?
return false
end
@name = name
return true

View file

@ -33,20 +33,22 @@ module MPW
crypto = GPGME::Crypto.new(armor: true)
data_decrypt = crypto.decrypt(IO.read(@file_gpg), password: password).read.force_encoding('utf-8')
if not data_decrypt.to_s.empty?
YAML.load(data_decrypt).each do |d|
@data.push(MPW::Item.new(id: d['id'],
name: d['name'],
group: d['group'],
host: d['host'],
protocol: d['protocol'],
user: d['login'],
password: d['password'],
port: d['port'],
comment: d['comment'],
last_edit: d['last_edit'],
created: d['created'],
)
YAML.load(data_decrypt).each_value do |d|
@data.push(Item.new(id: d['id'],
name: d['name'],
group: d['group'],
host: d['host'],
protocol: d['protocol'],
user: d['login'],
password: d['password'],
port: d['port'],
comment: d['comment'],
last_edit: d['last_edit'],
created: d['created'],
)
)
end
end
end
return true
@ -60,7 +62,21 @@ module MPW
def encrypt
FileUtils.cp(@file_gpg, "#{@file_gpg}.bk") if File.exist?(@file_gpg)
data_to_encrypt = @data.to_yaml
data_to_encrypt = {}
@data.each do |item|
data_to_encrypt.merge!({'id' => item.id,
'name' => item.name,
'group' => item.group,
'host' => item.host,
'protocol' => item.protocol,
'user' => item.user,
'password' => item.password,
'port' => item.port,
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
})
end
recipients = []
recipients.push(@key)
@ -70,7 +86,7 @@ module MPW
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.to_yaml, recipients: recipients, output: file_gpg)
file_gpg.close
FileUtils.rm("#{@file_gpg}.bk") if File.exist?("#{@file_gpg}.bk")
@ -88,7 +104,11 @@ module MPW
def list(options={})
result = []
search = defined?(options[:search]) ? options[:search].downcase : ''
if not defined?(options[:search]) or options[:search].nil?
search = ''
else
search = options[:search].downcase
end
@data.each do |item|
name = item.name.nil? ? nil : item.name.downcase
@ -96,10 +116,7 @@ module MPW
comment = item.comment.nil? ? nil : item.comment.downcase
if name =~ /^.*#{search}.*$/ or host =~ /^.*#{search}.*$/ or comment =~ /^.*#{search}.*$/
if (not defined?(options[:protocol] or options[:protocol].eql?(item.protocol)) and
(group.nil? or options[:group].eql?(item.group))
result.push(item)
end
result.push(item)
end
end
@ -117,21 +134,6 @@ module MPW
return nil
end
# Remove an item
# @args: id -> the item's identifiant
# @rtrn: true if the item has been deleted
def remove(id)
@data.each_value do |row|
if row['id'] == id
@data.delete(id)
return true
end
end
@error_msg = I18n.t('error.delete.id_no_exist', id: id)
return false
end
# Export to csv
# @args: file -> file where you export the data
# type -> udata type
@ -140,14 +142,30 @@ module MPW
case type
when :csv
CSV.open(file, 'w', write_headers: true,
headers: ['name', 'group', 'protocol', 'host', 'login', 'password', 'port', 'comment']) do |csv|
@data.each do |id, r|
csv << [r['name'], r['group'], r['protocol'], r['host'], r['login'], r['password'], r['port'], r['comment']]
headers: ['name', 'group', 'protocol', 'host', 'user', 'password', 'port', 'comment']) do |csv|
@data.each do |item|
csv << [item.id, item.group, item.protocol, item.host, item.user, item.password, item.port, item.comment]
end
end
when :yaml
File.open(file, 'w') {|f| f << @data.to_yaml}
data = {}
@data.each do |item|
data.merge!({'id' => item.id,
'name' => item.name,
'group' => item.group,
'host' => item.host,
'protocol' => item.protocol,
'user' => item.user,
'password' => item.password,
'port' => item.port,
'comment' => item.comment,
'last_edit' => item.last_edit,
'created' => item.created,
})
end
File.open(file, 'w') {|f| f << data.to_yaml}
else
@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"

View file

@ -171,7 +171,7 @@ class Cli
# @args: search -> the string to search
# protocol -> search from a particular protocol
def display(search, protocol=nil, group=nil)
result = @mpw.search(search, group, protocol)
result = @mpw.list(search: search)
case result.length
when 0
@ -180,10 +180,10 @@ class Cli
display_item(result.first)
else
i = 1
result.each do |r|
result.each do |item|
print "#{i}: ".cyan
print r['name']
print " -> #{r['comment']}".magenta if not r['comment'].to_s.empty?
print item.name
print " -> #{item.comment}".magenta if not item.comment.to_s.empty?
print "\n"
i += 1
@ -203,23 +203,23 @@ class Cli
def display_item(item)
puts '--------------------'.cyan
print 'Id: '.cyan
puts item['id']
puts item.id
print "#{I18n.t('display.name')}: ".cyan
puts item['name']
puts item.name
print "#{I18n.t('display.group')}: ".cyan
puts item['group']
puts item.group
print "#{I18n.t('display.server')}: ".cyan
puts item['host']
puts item.host
print "#{I18n.t('display.protocol')}: ".cyan
puts item['protocol']
puts item.protocol
print "#{I18n.t('display.login')}: ".cyan
puts item['login']
puts item.user
print "#{I18n.t('display.password')}: ".cyan
puts item['password']
puts item.password
print "#{I18n.t('display.port')}: ".cyan
puts item['port']
puts item.port
print "#{I18n.t('display.comment')}: ".cyan
puts item['comment']
puts item.comment
end
# Form to add a new item
@ -251,19 +251,19 @@ class Cli
# Update an item
# @args: id -> the item's id
def update(id)
row = @mpw.search_by_id(id)
item = @mpw.search_by_id(id)
if not row.empty?
puts I18n.t('form.update.title')
puts '--------------------'
name = ask(I18n.t('form.update.name' , name: row['name'])).to_s
group = ask(I18n.t('form.update.group' , group: row['group'])).to_s
server = ask(I18n.t('form.update.server' , server: row['host'])).to_s
protocol = ask(I18n.t('form.update.protocol', protocol: row['protocol'])).to_s
login = ask(I18n.t('form.update.login' , login: row['login'])).to_s
name = ask(I18n.t('form.update.name' , name: item.name)).to_s
group = ask(I18n.t('form.update.group' , group: item.group)).to_s
server = ask(I18n.t('form.update.server' , server: item.host)).to_s
protocol = ask(I18n.t('form.update.protocol', protocol: item.protocol)).to_s
login = ask(I18n.t('form.update.login' , login: item.user)).to_s
passwd = ask(I18n.t('form.update.password')).to_s
port = ask(I18n.t('form.update.port' , port: row['port'])).to_s
comment = ask(I18n.t('form.update.comment' , comment: row['comment'])).to_s
port = ask(I18n.t('form.update.port' , port: item.port)).to_s
comment = ask(I18n.t('form.update.comment' , comment: item.comment)).to_s
if @mpw.update(name, group, server, protocol, login, passwd, port, comment, id)
if @mpw.encrypt