From 314d6e87b46f33bec87c55e7e6b543eddbbe4103 Mon Sep 17 00:00:00 2001 From: nishiki Date: Mon, 2 Sep 2013 19:50:40 +0200 Subject: [PATCH] add an alternatif format for the display --- Cli.rb | 32 ++++++++++++++++++++++++++------ mpw | 30 ++++++++++++------------------ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/Cli.rb b/Cli.rb index 4067543..48f408a 100644 --- a/Cli.rb +++ b/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]}]: ") diff --git a/mpw b/mpw index 7a5e483..c4db372 100755 --- a/mpw +++ b/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"