From 965d830db3d00b3a37c9aabb8de2cfe49e5a1fc0 Mon Sep 17 00:00:00 2001 From: Sean Busbey Date: Wed, 15 Dec 2021 14:35:01 -0600 Subject: [PATCH 1/2] HBASE-26543 correct parsing of shell args with GetoptLong --- hbase-shell/src/main/ruby/jar-bootstrap.rb | 67 +++++++++++----------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/hbase-shell/src/main/ruby/jar-bootstrap.rb b/hbase-shell/src/main/ruby/jar-bootstrap.rb index 03dba743b3c7..04adea6605cf 100644 --- a/hbase-shell/src/main/ruby/jar-bootstrap.rb +++ b/hbase-shell/src/main/ruby/jar-bootstrap.rb @@ -78,62 +78,61 @@ def add_to_configuration(c, arg) c end +_configuration = nil + +# strip out any config definitions that won't work with GetoptLong +D_ARG = '-D'.freeze +ARGV.delete_if do |arg| + unless arg.start_with?(D_ARG) and arg.include?("=") + false + else + _configuration = add_to_configuration(_configuration, arg[2..-1]) + true + end +end + opts = GetoptLong.new( [ '--help', '-h', GetoptLong::NO_ARGUMENT ], - [ '--debug', '-d', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--noninteractive', '-n', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--top-level-defs', GetoptLong::OPTIONAL_ARGUMENT ], - [ '--Dkey=value', '-D', GetoptLong::NO_ARGUMENT ] + [ '--debug', '-d', GetoptLong::NO_ARGUMENT ], + [ '--noninteractive', '-n', GetoptLong::NO_ARGUMENT ], + [ '--top-level-defs', GetoptLong::NO_ARGUMENT ], + [ '-D', GetoptLong::REQUIRED_ARGUMENT ], + [ '--return-values', '-r', GetoptLong::NO_ARGUMENT ] ) +opts.ordering = GetoptLong::REQUIRE_ORDER -found = [] script2run = nil log_level = org.apache.log4j.Level::ERROR @shell_debug = false interactive = true top_level_definitions = false -_configuration = nil -D_ARG = '-D'.freeze opts.each do |opt, arg| - case opt || arg - when '--help' || '-h' + case opt + when '--help' puts cmdline_help + exit when D_ARG - argValue = ARGV.shift || (raise "#{D_ARG} takes a 'key=value' parameter") - _configuration = add_to_configuration(_configuration, argValue) - found.push(arg) - found.push(argValue) - when arg.start_with?(D_ARG) - _configuration = add_to_configuration(_configuration, arg[2..-1]) - found.push(arg) - when '--debug'|| '-d' + _configuration = add_to_configuration(_configuration, arg) + when '--debug' log_level = org.apache.log4j.Level::DEBUG $fullBackTrace = true @shell_debug = true - found.push(arg) puts 'Setting DEBUG log level...' - when '--noninteractive'|| '-n' - interactive = false - found.push(arg) - when '--return-values' || 'r' - warn '[INFO] the -r | --return-values option is ignored. we always behave '\ + when '--noninteractive' + interactive = false + when '--return-values' + warn '[INFO] the -r | --return-values option is ignored. we always behave '\ 'as though it was given.' - found.push(arg) - when '--top-level-defs' - top_level_definitions = true - else - # Presume it a script. Save it off for running later below - # after we've set up some environment. - script2run = arg - found.push(arg) - # Presume that any other args are meant for the script. + when '--top-level-defs' + top_level_definitions = true end end +if ARGV.length > 0 + script2run = ARGV.shift +end -# Delete all processed args -found.each { |arg| ARGV.delete(arg) } # Make sure debug flag gets back to IRB ARGV.unshift('-d') if @shell_debug From 6b70d50b47ebe23793ab505e383cdfc4d682f42a Mon Sep 17 00:00:00 2001 From: Sean Busbey Date: Wed, 5 Jan 2022 17:13:22 -0600 Subject: [PATCH 2/2] HBASE-26543 correct issues brought up by Rubocop. --- hbase-shell/src/main/ruby/jar-bootstrap.rb | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/hbase-shell/src/main/ruby/jar-bootstrap.rb b/hbase-shell/src/main/ruby/jar-bootstrap.rb index 04adea6605cf..47f2de487ffd 100644 --- a/hbase-shell/src/main/ruby/jar-bootstrap.rb +++ b/hbase-shell/src/main/ruby/jar-bootstrap.rb @@ -78,26 +78,26 @@ def add_to_configuration(c, arg) c end -_configuration = nil +conf_from_cli = nil # strip out any config definitions that won't work with GetoptLong D_ARG = '-D'.freeze ARGV.delete_if do |arg| - unless arg.start_with?(D_ARG) and arg.include?("=") - false - else - _configuration = add_to_configuration(_configuration, arg[2..-1]) + if arg.start_with?(D_ARG) && arg.include?('=') + conf_from_cli = add_to_configuration(conf_from_cli, arg[2..-1]) true + else + false end end opts = GetoptLong.new( - [ '--help', '-h', GetoptLong::NO_ARGUMENT ], - [ '--debug', '-d', GetoptLong::NO_ARGUMENT ], - [ '--noninteractive', '-n', GetoptLong::NO_ARGUMENT ], - [ '--top-level-defs', GetoptLong::NO_ARGUMENT ], - [ '-D', GetoptLong::REQUIRED_ARGUMENT ], - [ '--return-values', '-r', GetoptLong::NO_ARGUMENT ] + ['--help', '-h', GetoptLong::NO_ARGUMENT], + ['--debug', '-d', GetoptLong::NO_ARGUMENT], + ['--noninteractive', '-n', GetoptLong::NO_ARGUMENT], + ['--top-level-defs', GetoptLong::NO_ARGUMENT], + ['-D', GetoptLong::REQUIRED_ARGUMENT], + ['--return-values', '-r', GetoptLong::NO_ARGUMENT] ) opts.ordering = GetoptLong::REQUIRE_ORDER @@ -113,7 +113,7 @@ def add_to_configuration(c, arg) puts cmdline_help exit when D_ARG - _configuration = add_to_configuration(_configuration, arg) + conf_from_cli = add_to_configuration(conf_from_cli, arg) when '--debug' log_level = org.apache.log4j.Level::DEBUG $fullBackTrace = true @@ -129,9 +129,7 @@ def add_to_configuration(c, arg) end end -if ARGV.length > 0 - script2run = ARGV.shift -end +script2run = ARGV.shift unless ARGV.empty? # Make sure debug flag gets back to IRB ARGV.unshift('-d') if @shell_debug @@ -150,7 +148,7 @@ def add_to_configuration(c, arg) require 'shell/formatter' # Setup the HBase module. Create a configuration. -@hbase = _configuration.nil? ? Hbase::Hbase.new : Hbase::Hbase.new(_configuration) +@hbase = conf_from_cli.nil? ? Hbase::Hbase.new : Hbase::Hbase.new(conf_from_cli) # Setup console @shell = Shell::Shell.new(@hbase, interactive)