From 375c1eb7be23b95583b2c4fb0e8cd86e3e7760b2 Mon Sep 17 00:00:00 2001 From: "Doroszlai, Attila" Date: Mon, 6 Jan 2025 13:43:11 +0100 Subject: [PATCH] HDDS-12012. Defer ozone repair prompt after subcommand validation --- .../hadoop/ozone/repair/OzoneRepair.java | 32 ------------------ .../hadoop/ozone/repair/RepairTool.java | 33 ++++++++++++++++++- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java index 864022da6f3c..2518714a8f85 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java @@ -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. */ @@ -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; diff --git a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java index 20a30f0b1873..d5404cfe8162 100644 --- a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java +++ b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/RepairTool.java @@ -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 { + 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; @@ -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; } @@ -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(); + } }