HBASE-19770 Separate command return values from interactive shells

Uses a new option to the shell to specify that return values are
unwanted instead of overloading the interactive option. Enable
return_values when the shell is non-interactive.

Signed-off-by: Apekshit Sharma <appy@apache.org>
This commit is contained in:
Josh Elser 2018-01-11 14:47:13 -05:00
parent addd716549
commit b4f6ae86b6
4 changed files with 18 additions and 12 deletions

View File

@ -59,12 +59,15 @@ Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
-n | --noninteractive Do not run within an IRB session -n | --noninteractive Do not run within an IRB session
and exit with non-zero status on and exit with non-zero status on
first error. first error.
-r | --return-values Include return values from commands
executed in the shell.
HERE HERE
found = [] found = []
script2run = nil script2run = nil
log_level = org.apache.log4j.Level::ERROR log_level = org.apache.log4j.Level::ERROR
@shell_debug = false @shell_debug = false
interactive = true interactive = true
return_values = false
for arg in ARGV for arg in ARGV
if arg == '-h' || arg == '--help' if arg == '-h' || arg == '--help'
puts cmdline_help puts cmdline_help
@ -78,6 +81,9 @@ for arg in ARGV
elsif arg == '-n' || arg == '--noninteractive' elsif arg == '-n' || arg == '--noninteractive'
interactive = false interactive = false
found.push(arg) found.push(arg)
elsif arg == '-r' || arg == '--return-values'
return_values = true
found.push(arg)
else else
# Presume it a script. Save it off for running later below # Presume it a script. Save it off for running later below
# after we've set up some environment. # after we've set up some environment.
@ -110,7 +116,7 @@ require 'shell/formatter'
@hbase = Hbase::Hbase.new @hbase = Hbase::Hbase.new
# Setup console # Setup console
@shell = Shell::Shell.new(@hbase, interactive) @shell = Shell::Shell.new(@hbase, interactive, return_values)
@shell.debug = @shell_debug @shell.debug = @shell_debug
# Add commands to this namespace # Add commands to this namespace

View File

@ -71,14 +71,19 @@ module Shell
class Shell class Shell
attr_accessor :hbase attr_accessor :hbase
attr_accessor :interactive attr_accessor :interactive
attr_accessor :return_values
alias interactive? interactive alias interactive? interactive
alias return_values? return_values
@debug = false @debug = false
attr_accessor :debug attr_accessor :debug
def initialize(hbase, interactive = true) def initialize(hbase, interactive = true, return_values = !interactive)
self.hbase = hbase self.hbase = hbase
self.interactive = interactive self.interactive = interactive
self.return_values = return_values
# If we're in non-interactive mode, force return_values
self.return_values = true if not self.interactive
end end
# Returns Admin class from admin.rb # Returns Admin class from admin.rb
@ -135,16 +140,11 @@ module Shell
end end
# call the method 'command' on the specified command # call the method 'command' on the specified command
# If interactive is enabled, then we suppress the return value. The command should have # If return_values is false, then we suppress the return value. The command
# printed relevant output. # should have printed relevant output.
# Return value is only useful in non-interactive mode, for e.g. tests.
def command(command, *args) def command(command, *args)
ret = internal_command(command, :command, *args) ret = internal_command(command, :command, *args)
if interactive ret if return_values
return nil
else
return ret
end
end end
# call a specific internal method in the command instance # call a specific internal method in the command instance

View File

@ -20,7 +20,7 @@ require 'shell'
class NonInteractiveTest < Test::Unit::TestCase class NonInteractiveTest < Test::Unit::TestCase
def setup def setup
@hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration) @hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration)
@shell = Shell::Shell.new(@hbase, false) @shell = Shell::Shell.new(@hbase, false, true)
end end
define_test "Shell::Shell noninteractive mode should throw" do define_test "Shell::Shell noninteractive mode should throw" do

View File

@ -43,7 +43,7 @@ module Hbase
def setup_hbase def setup_hbase
hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration) hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration)
@shell = ::Shell::Shell.new(hbase, interactive = false) @shell = ::Shell::Shell.new(hbase, interactive = false, return_values = true)
end end
def shutdown def shutdown