Tools: pass Scanner to tasks and revert options management

- Remove TaskOption and central logic for processing options. It is not flexible and clear enough without investing a lot of effort. Fix is easy - just pass the scanner and let the task do what it needs to do.
This commit is contained in:
ljacqu 2015-12-12 10:37:08 +01:00
parent 0efb419149
commit 1e2a0f98ba
6 changed files with 36 additions and 130 deletions

View File

@ -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<TaskOption> options = task.getOptions();
Map<String, String> 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<String, ToolTask> taskCollection) {
System.out.println("The following tasks are available:");
for (String key : taskCollection.keySet()) {

View File

@ -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<String, String> 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<String, String> defaultMessages = null;
@ -71,18 +74,6 @@ public final class VerifyMessagesTask implements ToolTask {
}
}
@Override
public Iterable<TaskOption> 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<String, Boolean> missingKeys = verifier.getMissingKeys();
if (!missingKeys.isEmpty()) {

View File

@ -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<String, String> 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<TaskOption> 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);
}
}

View File

@ -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");
}
}
}

View File

@ -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;
}
}

View File

@ -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<String, String> options);
/**
* Return the name of the task.
*
* @return Name of the task
*/
String getTaskName();
Iterable<TaskOption> getOptions();
/**
* Execute the task.
*
* @param scanner Scanner to prompt the user with for options. Do not close it.
*/
void execute(Scanner scanner);
}