Skip to content

Commit

Permalink
HBASE-28632 Make -h arg respected by hbck2 and exit if unrecognized a…
Browse files Browse the repository at this point in the history
…rguments are passed (#143)

The -h argument in hbck is not respected and instead of displaying the argument usage guide, the command continued to execute. Any unrecognized arguments should cause an exception and exit.

Co-authored-by: Sravi Kommineni <[email protected]>
Reviewed-by: Ray Mattingly <[email protected]>
Signed-off-by: Nick Dimiduk <[email protected]>
  • Loading branch information
ksravista and Sravi Kommineni authored Jun 21, 2024
1 parent 4286235 commit 7c738fc
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
89 changes: 87 additions & 2 deletions hbase-hbck2/src/main/java/org/apache/hbase/HBCK2.java
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,11 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
// Now process command.
String[] commands = commandLine.getArgs();
String command = commands[0];

if (commandHasHelpOption(commands)) {
return showUsagePerCommand(command, options);
}

switch (command) {
// Case handlers all have same format. Check first that the server supports
// the feature FIRST, then move to process the command.
Expand Down Expand Up @@ -1345,6 +1350,69 @@ private int doCommandLine(CommandLine commandLine, Options options) throws IOExc
return EXIT_SUCCESS;
}

static int showUsagePerCommand(String command, Options options) throws IOException {
boolean invalidCommand = false;
try (StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw)) {
writer.println("Command:");
switch (command) {
case ADD_MISSING_REGIONS_IN_META_FOR_TABLES:
usageAddFsRegionsMissingInMeta(writer);
break;
case ASSIGNS:
usageAssigns(writer);
break;
case BYPASS:
usageBypass(writer);
break;
case FILESYSTEM:
usageFilesystem(writer);
break;
case FIX_META:
usageFixMeta(writer);
break;
case GENERATE_TABLE_INFO:
usageGenerateMissingTableInfo(writer);
break;
case REPLICATION:
usageReplication(writer);
break;
case EXTRA_REGIONS_IN_META:
usageExtraRegionsInMeta(writer);
break;
case REPORT_MISSING_REGIONS_IN_META:
usageReportMissingRegionsInMeta(writer);
break;
case SET_REGION_STATE:
usageSetRegionState(writer);
break;
case SET_TABLE_STATE:
usageSetTableState(writer);
break;
case SCHEDULE_RECOVERIES:
usageScheduleRecoveries(writer);
break;
case RECOVER_UNKNOWN:
usageRecoverUnknown(writer);
break;
case UNASSIGNS:
usageUnassigns(writer);
break;
case REGIONINFO_MISMATCH:
usageRegioninfoMismatch(writer);
break;
default:
showErrorMessage("Invalid arg: " + command);
invalidCommand = true;
break;
}
if (!invalidCommand) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("HBCK2 [OPTIONS] COMMAND <ARGS>", "Options:", options, sw.toString());
}
return invalidCommand ? EXIT_FAILURE : EXIT_SUCCESS;
}
}

private static String toString(List<?> things) {
return things.stream().map(Object::toString).collect(Collectors.joining(", "));
}
Expand Down Expand Up @@ -1507,22 +1575,39 @@ private Pair<CommandLine, List<String>> parseCommandWithFixAndInputOptions(Strin
return Pair.newPair(commandLine, params);
}

private boolean commandHasHelpOption(String[] args) {
args = purgeFirst(args);
Options options = new Options();
Option helpOption =
Option.builder("h").longOpt("help").desc("help message for a command").build();

options.addOption(helpOption);
CommandLine test = getCommandLine(args, options, false);
return test != null && test.hasOption(helpOption.getOpt());
}

/**
* Get a commandLine object with options and a arg list
*/
private CommandLine getCommandLine(String[] args, Options options) {
private CommandLine getCommandLine(String[] args, Options options, boolean showException) {
// Parse command-line.
CommandLineParser parser = new DefaultParser();
CommandLine commandLine;
try {
commandLine = parser.parse(options, args, false);
} catch (ParseException e) {
showErrorMessage(e.getMessage());
if (showException) {
showErrorMessage(e.getMessage());
}
return null;
}
return commandLine;
}

private CommandLine getCommandLine(String[] args, Options options) {
return getCommandLine(args, options, true);
}

/** Returns Read arguments from args or a list of input files */
private List<String> getFromArgsOrFiles(List<String> args, boolean getFromFile)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public void testHelp() throws IOException {
// Passing -h/--help does the same
output = retrieveOptionOutput(new String[] { "-h" });
assertTrue(output, output.startsWith("usage: HBCK2"));

// passing -h after the command, should print usage for that command
output = retrieveOptionOutput(new String[] { "addFsRegionsMissingInMeta", "-h" });
assertFalse(output.contains("ERROR:"));
assertTrue(output.contains("Options:"));

// invalid argument -h should print error
output = retrieveOptionOutput(new String[] { "invalidArg", "-h" });
assertTrue(output.contains("ERROR:"));
}

@Test
Expand Down

0 comments on commit 7c738fc

Please sign in to comment.