Skip to content

Commit

Permalink
HDDS-12012. Defer ozone repair prompt after subcommand validation
Browse files Browse the repository at this point in the history
  • Loading branch information
adoroszlai committed Jan 6, 2025
1 parent 3d35b01 commit 375c1eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import org.apache.hadoop.hdds.cli.RepairSubcommand;
import picocli.CommandLine;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
* Ozone Repair Command line tool.
*/
Expand All @@ -37,39 +34,10 @@
mixinStandardHelpOptions = true)
public class OzoneRepair extends GenericCli implements ExtensibleParentCommand {

public static final String WARNING_SYS_USER_MESSAGE =
"ATTENTION: Running as user %s. Make sure this is the same user used to run the Ozone process." +
" Are you sure you want to continue (y/N)? ";

public static void main(String[] argv) {
new OzoneRepair().run(argv);
}

@Override
public int execute(String[] argv) {
if (argv.length == 0 || argv[0].equals("--help") || argv[0].equals("-h")) {
return super.execute(argv);
}

String currentUser = getSystemUserName();
if (!("y".equalsIgnoreCase(getConsoleReadLineWithFormat(currentUser)))) {
System.out.println("Aborting command.");
return 1;
}
System.out.println("Run as user: " + currentUser);

return super.execute(argv);
}

public String getSystemUserName() {
return System.getProperty("user.name");
}

public String getConsoleReadLineWithFormat(String currentUser) {
System.err.printf(WARNING_SYS_USER_MESSAGE, currentUser);
return (new Scanner(System.in, StandardCharsets.UTF_8.name())).nextLine().trim();
}

@Override
public Class<?> subcommandType() {
return RepairSubcommand.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
import picocli.CommandLine;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.Callable;

/** Parent class for all actionable repair commands. */
public abstract class RepairTool extends AbstractSubcommand implements Callable<Void> {

private static final String WARNING_SYS_USER_MESSAGE =
"ATTENTION: Running as user %s. Make sure this is the same user used to run the Ozone process." +
" Are you sure you want to continue (y/N)? ";

@CommandLine.Option(names = {"--force"},
description = "Use this flag if you want to bypass the check in false-positive cases.")
private boolean force;
Expand All @@ -35,7 +41,9 @@ public abstract class RepairTool extends AbstractSubcommand implements Callable<

@Override
public final Void call() throws Exception {
execute();
if (confirmUser()) {
execute();
}
return null;
}

Expand Down Expand Up @@ -84,4 +92,27 @@ private String formatMessage(String msg, Object[] args) {
return msg;
}

protected boolean confirmUser() {
final String currentUser = getSystemUserName();
final boolean confirmed = "y".equalsIgnoreCase(getConsoleReadLineWithFormat(currentUser));

if (confirmed) {
info("Run as user: " + currentUser);
} else {
error("Aborting command.");
}

return confirmed;
}

private String getSystemUserName() {
return System.getProperty("user.name");
}

private String getConsoleReadLineWithFormat(String currentUser) {
err().printf(WARNING_SYS_USER_MESSAGE, currentUser);
return new Scanner(System.in, StandardCharsets.UTF_8.name())
.nextLine()
.trim();
}
}

0 comments on commit 375c1eb

Please sign in to comment.