1
0
Fork 0
mirror of https://github.com/nishiki/manage-password.git synced 2025-03-20 05:14:36 +00:00

add name and group for an item, and delete the id

This commit is contained in:
nishiki 2013-08-26 20:52:18 +02:00
parent 627f44ba02
commit 029f2c2064
2 changed files with 63 additions and 39 deletions

29
Cli.rb
View file

@ -58,15 +58,20 @@ class Cli
result = @m.search(search, protocol) result = @m.search(search, protocol)
if not result.empty? if not result.empty?
i = 0
result.each do |r| result.each do |r|
puts "# --------------------" puts "# --------------------"
puts "# Id: #{r[MPW::ID]}" puts "# Id: #{i}"
puts "# Name: #{r[MPW::NAME]}"
puts "# Group: #{r[MPW::GROUP]}"
puts "# Server: #{r[MPW::SERVER]}" puts "# Server: #{r[MPW::SERVER]}"
puts "# Type: #{r[MPW::PROTOCOL]}" puts "# Type: #{r[MPW::PROTOCOL]}"
puts "# Login: #{r[MPW::LOGIN]}" puts "# Login: #{r[MPW::LOGIN]}"
puts "# Password: #{r[MPW::PASSWORD]}" puts "# Password: #{r[MPW::PASSWORD]}"
puts "# Port: #{r[MPW::PORT]}" puts "# Port: #{r[MPW::PORT]}"
puts "# Comment: #{r[MPW::COMMENT]}" puts "# Comment: #{r[MPW::COMMENT]}"
i += 1
end end
else else
puts "Nothing result!" puts "Nothing result!"
@ -78,17 +83,21 @@ class Cli
row = Array.new() row = Array.new()
puts "# Add a new item" puts "# Add a new item"
puts "# --------------------" puts "# --------------------"
server = ask("Enter the server name or ip: ") name = ask("Enter the name: ")
group = ask("Enter the group [default=No Group]: ")
server = ask("Enter the hostname or ip: ")
protocol = ask("Enter the type of connection (ssh, web, other): ") protocol = ask("Enter the type of connection (ssh, web, other): ")
login = ask("Enter the login connection: ") login = ask("Enter the login connection: ")
passwd = ask("Enter the the password: ") passwd = ask("Enter the the password: ")
port = ask("Enter the connection port (optinal): ") port = ask("Enter the connection port (optinal): ")
comment = ask("Enter a comment (optinal): ") comment = ask("Enter a comment (optinal): ")
@m.add(server, protocol, login, passwd, port, comment) if @m.add(name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt()
if @m.encrypt() puts "Item has been added!"
puts "Item has been added!" else
puts "ERROR: #{@m.error_msg}"
end
else else
puts "ERROR: #{@m.error_msg}" puts "ERROR: #{@m.error_msg}"
end end
@ -100,16 +109,18 @@ class Cli
row = @m.searchById(id) row = @m.searchById(id)
if not row.empty? if not row.empty?
puts "# Add a new password" puts "# Update an item"
puts "# --------------------" puts "# --------------------"
server = ask("Enter the server name or ip [#{row[MPW::SERVER]}]: ") name = ask("Enter the name [#{row[MPW::NAME]}]: ")
group = ask("Enter the group [#{row[MPW::GROUP]}]: ")
server = ask("Enter the hostname or ip [#{row[MPW::SERVER]}]: ")
protocol = ask("Enter the type of connection [#{row[MPW::PROTOCOL]}]: ") protocol = ask("Enter the type of connection [#{row[MPW::PROTOCOL]}]: ")
login = ask("Enter the login connection [#{row[MPW::LOGIN]}]: ") login = ask("Enter the login connection [#{row[MPW::LOGIN]}]: ")
passwd = ask("Enter the the password: ") passwd = ask("Enter the the password: ")
port = ask("Enter the connection port [#{row[MPW::PORT]}]: ") port = ask("Enter the connection port [#{row[MPW::PORT]}]: ")
comment = ask("Enter a comment [#{row[MPW::COMMENT]}]: ") comment = ask("Enter a comment [#{row[MPW::COMMENT]}]: ")
if @m.update(id, server, protocol, login, passwd, port, comment) if @m.update(id, name, group, server, protocol, login, passwd, port, comment)
if @m.encrypt() if @m.encrypt()
puts "Item has been updated!" puts "Item has been updated!"
else else

73
MPW.rb
View file

@ -10,22 +10,21 @@ require 'yaml'
class MPW class MPW
ID = 0 NAME = 0
PROTOCOL = 1 GROUP = 1
SERVER = 2 PROTOCOL = 2
LOGIN = 3 SERVER = 3
PASSWORD = 4 LOGIN = 4
PORT = 5 PASSWORD = 5
COMMENT = 6 PORT = 6
COMMENT = 7
attr_accessor :error
attr_accessor :error_msg attr_accessor :error_msg
# Constructor # Constructor
def initialize() def initialize()
@file_config = "#{Dir.home()}/.mpw.cfg" @file_config = "#{Dir.home()}/.mpw.cfg"
@error_mgs = nil @error_mgs = nil
@error = 0
end end
# Create a new config file # Create a new config file
@ -38,7 +37,6 @@ class MPW
if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/ if not key =~ /[a-zA-Z0-9.-_]+\@[a-zA-Z0-9]+\.[a-zA-Z]+/
@error_msg = "The key string isn't in good format!" @error_msg = "The key string isn't in good format!"
@error = 1
return false return false
end end
@ -63,7 +61,6 @@ class MPW
end end
rescue rescue
@error_msg = "Can't write the config file!" @error_msg = "Can't write the config file!"
@error = 2
return false return false
end end
@ -86,7 +83,6 @@ class MPW
rescue rescue
@error_msg = "Checkconfig failed!" @error_msg = "Checkconfig failed!"
@error = 3
return false return false
end end
@ -125,7 +121,6 @@ class MPW
end end
@error_msg = "Can't decrypt file!" @error_msg = "Can't decrypt file!"
@error = 4
return false return false
end end
end end
@ -155,7 +150,6 @@ class MPW
return true return true
rescue rescue
@error_msg = "Can't encrypt the GPG file!" @error_msg = "Can't encrypt the GPG file!"
@error = 5
return false return false
end end
end end
@ -182,27 +176,42 @@ class MPW
# @args: id -> the id item # @args: id -> the id item
# @rtrn: a row with the resultat of the search # @rtrn: a row with the resultat of the search
def searchById(id) def searchById(id)
i = 0
@data.lines do |line| @data.lines do |line|
row = line.parse_csv row = line.parse_csv
if !id.nil? && id.eql?(row[ID]) if !id.nil? && id.eql?(i.to_s)
return row return row
end end
i += 1
end end
return Array.new() return Array.new()
end end
# Add a new item # Add a new item
# @args: server -> the ip or server # @args: name -> the item name
# group -> the item group
# server -> the ip or server
# protocol -> the protocol # protocol -> the protocol
# login -> the login # login -> the login
# passwd -> the password # passwd -> the password
# port -> the port # port -> the port
# comment -> a comment # comment -> a comment
def add(server, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil) # @rtrn: true if it works
def add(name, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
row = Array.new() row = Array.new()
if name.nil? || name.empty?
@error_msg = "You must define a name!"
return false
end
row[ID] = Time.now.to_i.to_s(16) if group.nil? || group.empty
group 'No Group'
end
row[NAME] = name
row[GROUP] = group
row[SERVER] = server row[SERVER] = server
row[PROTOCOL] = protocol row[PROTOCOL] = protocol
row[LOGIN] = login row[LOGIN] = login
@ -211,27 +220,33 @@ class MPW
row[COMMENT] = comment row[COMMENT] = comment
@data << "#{row.join(',')}\n" @data << "#{row.join(',')}\n"
return true
end end
# Update an item # Update an item
# @args: id -> the item's identifiant # @args: id -> the item's identifiant
# server -> the ip or server # name -> the item name
# group -> the item group
# server -> the ip or hostname
# protocol -> the protocol # protocol -> the protocol
# login -> the login # login -> the login
# passwd -> the password # passwd -> the password
# port -> the port # port -> the port
# comment -> a comment # comment -> a comment
# @rtrn: true if the item has been updated # @rtrn: true if the item has been updated
def update(id, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil) def update(id, name=nil, group=nil, server=nil, protocol=nil, login=nil, passwd=nil, port=nil, comment=nil)
updated = false updated = false
data_tmp = '' data_tmp = ''
i = 0
@data.lines do |line| @data.lines do |line|
row = line.parse_csv if id.eql?(i.to_s)
if id.eql?(row[ID]) row = line.parse_csv
row_update = Array.new() row_update = Array.new()
row_update[ID] = row[ID] name.empty? ? (row_update[NAME] = row[NAME]) : (row_update[NAME] = name)
group.empty? ? (row_update[GROUP] = row[GROUP]) : (row_update[GROUP] = group)
server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server) server.empty? ? (row_update[SERVER] = row[SERVER]) : (row_update[SERVER] = server)
protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol) protocol.empty? ? (row_update[PROTOCOL] = row[PROTOCOL]) : (row_update[PROTOCOL] = protocol)
login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login) login.empty? ? (row_update[LOGIN] = row[LOGIN]) : (row_update[LOGIN] = login)
@ -244,12 +259,13 @@ class MPW
else else
data_tmp << line data_tmp << line
end end
i += 1
end end
@data = data_tmp @data = data_tmp
if not updated if not updated
@error_msg = "Can't update the item: #{id}!" @error_msg = "Can't update the item: #{id}!"
@error = 6
end end
return updated return updated
@ -262,19 +278,19 @@ class MPW
removed = false removed = false
data_tmp = "" data_tmp = ""
i = 0
@data.lines do |line| @data.lines do |line|
row = line.parse_csv if id.eql?(i.to_s)
if id.eql?(row[ID])
removed = true removed = true
else else
data_tmp << line data_tmp << line
end end
i += 1
end end
@data = data_tmp @data = data_tmp
if not removed if not removed
@error_msg = "Can't remove the item: #{id}!" @error_msg = "Can't remove the item: #{id}!"
@error = 7
end end
return removed return removed
@ -292,7 +308,6 @@ class MPW
return true return true
rescue rescue
@error_msg = "Can't export, impossible to write in #{file}!" @error_msg = "Can't export, impossible to write in #{file}!"
@error = 8
return false return false
end end
end end
@ -306,7 +321,6 @@ class MPW
data_new.lines do |line| data_new.lines do |line|
if not line =~ /(.*,){6}/ if not line =~ /(.*,){6}/
@error_msg = "Can't import, the file is bad format!" @error_msg = "Can't import, the file is bad format!"
@error = 9
return false return false
end end
end end
@ -315,7 +329,6 @@ class MPW
return true return true
rescue rescue
@error_msg = "Can't import, impossible to read #{file}!" @error_msg = "Can't import, impossible to read #{file}!"
@error = 10
return false return false
end end
end end