mirror of
https://github.com/nishiki/manage-password.git
synced 2025-02-20 01:50:04 +00:00
add an alternatif format for the display
This commit is contained in:
parent
bf4b28e239
commit
314d6e87b4
2 changed files with 38 additions and 24 deletions
32
Cli.rb
32
Cli.rb
|
@ -54,19 +54,23 @@ class Cli
|
|||
# Display the query's result
|
||||
# @args: search -> the string to search
|
||||
# protocol -> search from a particular protocol
|
||||
def display(search, protocol=nil)
|
||||
def display(search, protocol=nil, format=nil)
|
||||
result = @m.search(search, protocol)
|
||||
|
||||
if not result.empty?
|
||||
result.each do |r|
|
||||
displayFormat(r)
|
||||
if format.nil? || !format
|
||||
displayFormat(r)
|
||||
else
|
||||
displayFormatAlt(r)
|
||||
end
|
||||
end
|
||||
else
|
||||
puts "Nothing result!"
|
||||
end
|
||||
end
|
||||
|
||||
# Display an item
|
||||
# Display an item in the default format
|
||||
# @args: item -> an array with the item information
|
||||
def displayFormat(item)
|
||||
puts "# --------------------"
|
||||
|
@ -74,13 +78,29 @@ class Cli
|
|||
puts "# Name: #{item[MPW::NAME]}"
|
||||
puts "# Group: #{item[MPW::GROUP]}"
|
||||
puts "# Server: #{item[MPW::SERVER]}"
|
||||
puts "# Type: #{item[MPW::PROTOCOL]}"
|
||||
puts "# Protocol: #{item[MPW::PROTOCOL]}"
|
||||
puts "# Login: #{item[MPW::LOGIN]}"
|
||||
puts "# Password: #{item[MPW::PASSWORD]}"
|
||||
puts "# Port: #{item[MPW::PORT]}"
|
||||
puts "# Comment: #{item[MPW::COMMENT]}"
|
||||
end
|
||||
|
||||
# Display an item in the alternative format
|
||||
# @args: item -> an array with the item information
|
||||
def displayFormatAlt(item)
|
||||
item[MPW::PORT].nil? ? (port = '') : (port = ":#{item[MPW::PORT]}")
|
||||
|
||||
if item[MPW::PASSWORD].nil? || item[MPW::PASSWORD].empty?
|
||||
if item[MPW::LOGIN].index('@').eql?(nil)
|
||||
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://#{item[MPW::LOGIN]}@#{item[MPW::SERVER]}#{port}"
|
||||
else
|
||||
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}}@#{item[MPW::SERVER]}#{port}"
|
||||
end
|
||||
else
|
||||
puts "# #{item[MPW::ID]} #{item[MPW::PROTOCOL]}://{#{item[MPW::LOGIN]}:#{item[MPW::PASSWORD]}}@#{item[MPW::SERVER]}#{port}"
|
||||
end
|
||||
end
|
||||
|
||||
# Form to add a new item
|
||||
def add()
|
||||
row = Array.new()
|
||||
|
@ -89,7 +109,7 @@ class Cli
|
|||
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 protocol of the connection (ssh, http, other): ")
|
||||
login = ask("Enter the login connection: ")
|
||||
passwd = ask("Enter the the password: ")
|
||||
port = ask("Enter the connection port (optinal): ")
|
||||
|
@ -117,7 +137,7 @@ class Cli
|
|||
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 protocol of the connection [#{row[MPW::PROTOCOL]}]: ")
|
||||
login = ask("Enter the login connection [#{row[MPW::LOGIN]}]: ")
|
||||
passwd = ask("Enter the the password: ")
|
||||
port = ask("Enter the connection port [#{row[MPW::PORT]}]: ")
|
||||
|
|
30
mpw
30
mpw
|
@ -11,6 +11,9 @@ APP_ROOT = File.dirname(Pathname.new(__FILE__).realpath)
|
|||
require "#{APP_ROOT}/Cli.rb"
|
||||
|
||||
options = {}
|
||||
options[:force] = false
|
||||
options[:format] = false
|
||||
|
||||
OptionParser.new do |opts|
|
||||
opts.banner = "Usage: mpw [options]"
|
||||
|
||||
|
@ -19,7 +22,7 @@ OptionParser.new do |opts|
|
|||
end
|
||||
|
||||
opts.on("-A", "--show-all", "Show all items") do |b|
|
||||
options[:showall] = true
|
||||
options[:type] = 'all'
|
||||
options[:display] = ""
|
||||
end
|
||||
|
||||
|
@ -55,12 +58,17 @@ OptionParser.new do |opts|
|
|||
options[:force] = true
|
||||
end
|
||||
|
||||
opts.on("-F", "--format", "Change the display format by an alternatif") do |b|
|
||||
options[:format] = true
|
||||
end
|
||||
|
||||
opts.on("-h", "--help", "Show this message") do |b|
|
||||
puts opts
|
||||
exit 0
|
||||
end
|
||||
end.parse!
|
||||
|
||||
|
||||
cli = Cli.new()
|
||||
|
||||
# Display the item's informations
|
||||
|
@ -68,21 +76,11 @@ if not options[:setup].nil?
|
|||
cli.setup()
|
||||
|
||||
elsif not options[:display].nil?
|
||||
if not options[:type].nil?
|
||||
cli.display(options[:display], options[:type])
|
||||
elsif not options[:showall].nil?
|
||||
cli.display(options[:display], 'all')
|
||||
else
|
||||
cli.display(options[:display])
|
||||
end
|
||||
cli.display(options[:display], options[:type], options[:format])
|
||||
|
||||
# Remove an item
|
||||
elsif not options[:remove].nil?
|
||||
if not options[:force].nil?
|
||||
cli.remove(options[:remove], options[:force])
|
||||
else
|
||||
cli.remove(options[:remove])
|
||||
end
|
||||
cli.remove(options[:remove], options[:force])
|
||||
|
||||
# Update an item
|
||||
elsif not options[:update].nil?
|
||||
|
@ -98,11 +96,7 @@ elsif not options[:export].nil?
|
|||
|
||||
# Add a new item
|
||||
elsif not options[:import].nil?
|
||||
if not options[:force].nil?
|
||||
cli.import(options[:import], options[:force])
|
||||
else
|
||||
cli.import(options[:import])
|
||||
end
|
||||
cli.import(options[:import], options[:force])
|
||||
else
|
||||
puts "For help add option -h or --help"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue