1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-16 22:50:10 +03:00

CLI: Add support for compounds commands. New status color

Author: Alejandro Huertas <ahuertas@opennebula.systems>
(cherry picked from commit 5509d7e25727fcf00e539bdf8501e9a75f696791)
This commit is contained in:
Ruben S. Montero 2018-09-20 17:25:42 +02:00
parent 382f6f3a3b
commit 7bcc29befc
2 changed files with 23 additions and 6 deletions

View File

@ -110,9 +110,11 @@ module CLIHelper
ANSI_RED="\33[31m"
ANSI_GREEN="\33[32m"
ANSI_RESET="\33[0m"
ANSI_YELLOW="\33[33m"
OK_STATES=%w{runn rdy on}
BAD_STATES=%w{fail err err}
OK_STATES=%w{runn rdy on configured}
BAD_STATES=%w{fail err err error}
REGULAR_STATES=%w{pending}
def CLIHelper.color_state(stat)
if $stdout.tty?
@ -121,6 +123,8 @@ module CLIHelper
ANSI_GREEN+stat+ANSI_RESET
when *BAD_STATES
ANSI_RED+stat+ANSI_RESET
when *REGULAR_STATES
ANSI_YELLOW+stat+ANSI_RESET
else
stat
end
@ -270,7 +274,7 @@ module CLIHelper
def print_table(data, options)
if !options[:csv] && !options[:noheader]
CLIHelper.print_header(header_str)
CLIHelper.print_header(header_str)
end
data ? print_data(data, options) : puts

View File

@ -74,7 +74,7 @@ module CommandParser
instance_eval(&block)
addons = Dir["#{OpenNebulaHelper::CLI_ADDONS_LOCATION}/#{File.basename($0)}/*"]
if defined?(addons) and !addons.nil?
if defined?(addons) and !addons.nil?
addons.each do |addon_path|
addon_code = File.read(addon_path)
instance_eval(addon_code)
@ -280,6 +280,10 @@ module CommandParser
# end
#
def command(name, desc, *args_format, &block)
if name.is_a? (Array)
name = name.join(" ").to_sym
end
cmd = Hash.new
cmd[:desc] = desc
cmd[:arity] = 0
@ -432,8 +436,17 @@ module CommandParser
comm = @main
elsif
if @args[0] && !@args[0].match(/^-/)
@comm_name = comm_name = @args.shift.to_sym
comm = @commands[comm_name]
while comm.nil? and @args.size > 0 do
current = @args.shift
if comm_name.empty?
@comm_name = comm_name = "#{current}".to_sym
else
@comm_name = comm_name = "#{comm_name} #{current}".to_sym
end
comm = @commands[comm_name]
end
end
end