diff --git a/src/tools/ToolsRunner.java b/src/tools/ToolsRunner.java index 04093bd1..eddedc66 100644 --- a/src/tools/ToolsRunner.java +++ b/src/tools/ToolsRunner.java @@ -1,5 +1,3 @@ -import utils.ScannerHelper; -import utils.TaskOption; import utils.ToolTask; import utils.ToolsConstants; @@ -38,31 +36,13 @@ public final class ToolsRunner { ToolTask task = tasks.get(inputTask); if (task != null) { - executeTask(task, scanner); + task.execute(scanner); } else { System.out.println("Unknown task"); } scanner.close(); } - /** - * Execute the given tool task after prompting the user for the required options. - * - * @param task The task to run - * @param scanner The scanner instance - */ - private static void executeTask(ToolTask task, Scanner scanner) { - Iterable options = task.getOptions(); - Map inputOptions = new HashMap<>(); - for (TaskOption option : options) { - System.out.println(option.getDescription()); - String input = ScannerHelper.getAnswer(option.getDefaultOption(), scanner, option.getOptions()); - inputOptions.put(option.getName(), input); - } - - task.execute(inputOptions); - } - private static void listAllTasks(Map taskCollection) { System.out.println("The following tasks are available:"); for (String key : taskCollection.keySet()) { diff --git a/src/tools/messages/VerifyMessagesTask.java b/src/tools/messages/VerifyMessagesTask.java index cb1d3f45..2a371216 100644 --- a/src/tools/messages/VerifyMessagesTask.java +++ b/src/tools/messages/VerifyMessagesTask.java @@ -2,13 +2,11 @@ package messages; import fr.xephi.authme.util.StringUtils; import utils.FileUtils; -import utils.TaskOption; import utils.ToolTask; import utils.ToolsConstants; import java.io.File; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -37,9 +35,14 @@ public final class VerifyMessagesTask implements ToolTask { } @Override - public void execute(Map options) { - String inputFile = options.get("custom.file"); - boolean addMissingKeys = options.get("add.missing.keys").equals("y"); + public void execute(Scanner scanner) { + System.out.println("Check a specific file only?"); + System.out.println("- Empty line will check all files in the resources messages folder (default)"); + System.out.println(format("- %s will be replaced to the messages folder %s", SOURCES_TAG, MESSAGES_FOLDER)); + String inputFile = scanner.nextLine(); + + System.out.println("Add any missing keys to files? ['y' = yes]"); + boolean addMissingKeys = "y".equals(scanner.nextLine()); // Set up needed objects Map defaultMessages = null; @@ -71,18 +74,6 @@ public final class VerifyMessagesTask implements ToolTask { } } - @Override - public Iterable getOptions() { - String customFileDescription = StringUtils.join(System.getProperty("line.separator"), - "Check a specific file only? (optional - enter custom filename)", - "- Empty line will check all files in the resources messages folder", - format("- %s will be replaced to the messages folder %s", SOURCES_TAG, MESSAGES_FOLDER)); - - return Arrays.asList( - new TaskOption("custom.file", customFileDescription, "", null), - new TaskOption("add.missing.keys", "Add missing keys to files? [y/n]", "n", "y", "n")); - } - private static void verifyFile(MessageFileVerifier verifier) { Map missingKeys = verifier.getMissingKeys(); if (!missingKeys.isEmpty()) { diff --git a/src/tools/permissions/PermissionsListWriter.java b/src/tools/permissions/PermissionsListWriter.java index 3b1e9c33..1d06b8ce 100644 --- a/src/tools/permissions/PermissionsListWriter.java +++ b/src/tools/permissions/PermissionsListWriter.java @@ -3,12 +3,11 @@ package permissions; import utils.ANewMap; import utils.FileUtils; import utils.TagReplacer; -import utils.TaskOption; import utils.ToolTask; import utils.ToolsConstants; -import java.util.Arrays; import java.util.Map; +import java.util.Scanner; import java.util.Set; /** @@ -25,10 +24,16 @@ public class PermissionsListWriter implements ToolTask { } @Override - public void execute(Map options) { + public void execute(Scanner scanner) { // Ask if result should be written to file - boolean includeDescription = options.get("include.description").equals("y"); - boolean writeToFile = options.get("write.to.file").equals("y"); + System.out.println("Include description? [Enter 'n' for no]"); + boolean includeDescription = !matches("n", scanner); + + boolean writeToFile = false; + if (includeDescription) { + System.out.println("Write to file? [Enter 'y' for yes]"); + writeToFile = matches("y", scanner); + } if (!includeDescription) { outputSimpleList(); @@ -39,13 +44,6 @@ public class PermissionsListWriter implements ToolTask { } } - @Override - public Iterable getOptions() { - return Arrays.asList( - new TaskOption("include.description", "Include description? [y/n]", "y", "y", "n"), - new TaskOption("write.to.file", "Write to file? [y/n]", "n", "y", "n")); - } - private static void generateAndWriteFile() { final String permissionsTagValue = generatePermissionsList(); @@ -83,4 +81,9 @@ public class PermissionsListWriter implements ToolTask { System.out.println("Total: " + nodes.size()); } + private static boolean matches(String answer, Scanner sc) { + String userInput = sc.nextLine(); + return answer.equalsIgnoreCase(userInput); + } + } diff --git a/src/tools/utils/ScannerHelper.java b/src/tools/utils/ScannerHelper.java deleted file mode 100644 index b7741164..00000000 --- a/src/tools/utils/ScannerHelper.java +++ /dev/null @@ -1,33 +0,0 @@ -package utils; - -import fr.xephi.authme.util.StringUtils; - -import java.util.Scanner; - -/** - * Helper class for retrieving an answer from a Scanner. - */ -public class ScannerHelper { - - // def may be null to force the selection of one of the options - // options may be null to just select whatever comes in - public static String getAnswer(String def, Scanner scanner, String... options) { - while (true) { - String input = scanner.nextLine(); - if (StringUtils.isEmpty(input) && def != null) { - return def; - } - - if (options == null) { - return input; - } else { - for (String option : options) { - if (input.equals(option)) { - return option; - } - } - } - System.out.println("Invalid answer, please try again"); - } - } -} diff --git a/src/tools/utils/TaskOption.java b/src/tools/utils/TaskOption.java deleted file mode 100644 index f4d32efa..00000000 --- a/src/tools/utils/TaskOption.java +++ /dev/null @@ -1,43 +0,0 @@ -package utils; - -/** - * Option required by a tool task. - */ -public class TaskOption { - - private final String name; - private final String description; - private final String defaultOption; - private final String[] options; - - /** - * Constructor. - * - * @param name Name of the option (to refer to the option) - * @param description Description shown to the user when asked to set the option - * @param defaultOption The default option. Can be null to force a value from options. - * @param options Collection of possible options. Can be null to allow any input. - */ - public TaskOption(String name, String description, String defaultOption, String... options) { - this.name = name; - this.description = description; - this.defaultOption = defaultOption; - this.options = options; - } - - public String getName() { - return name; - } - - public String getDescription() { - return description; - } - - public String getDefaultOption() { - return defaultOption; - } - - public String[] getOptions() { - return options; - } -} diff --git a/src/tools/utils/ToolTask.java b/src/tools/utils/ToolTask.java index d74fcc3f..56a2a788 100644 --- a/src/tools/utils/ToolTask.java +++ b/src/tools/utils/ToolTask.java @@ -1,6 +1,6 @@ package utils; -import java.util.Map; +import java.util.Scanner; /** * Common interface for tool tasks. Note that the implementing tasks are instantiated @@ -8,10 +8,18 @@ import java.util.Map; */ public interface ToolTask { - void execute(Map options); - + /** + * Return the name of the task. + * + * @return Name of the task + */ String getTaskName(); - Iterable getOptions(); + /** + * Execute the task. + * + * @param scanner Scanner to prompt the user with for options. Do not close it. + */ + void execute(Scanner scanner); }