From 23da023d53363a3fa9bf7302277feced3719133b Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 8 May 2016 17:22:02 +0200 Subject: [PATCH] #513 Mark tools runnable by command line with specific interface --- src/test/java/tools/ToolsRunner.java | 44 +++++++++++-------- .../tools/commands/CommandPageCreater.java | 9 +++- .../HashAlgorithmsDescriptionTask.java | 9 +++- .../permissions/PermissionsListWriter.java | 14 +++--- src/test/java/tools/utils/AutoToolTask.java | 13 ++++++ 5 files changed, 59 insertions(+), 30 deletions(-) create mode 100644 src/test/java/tools/utils/AutoToolTask.java diff --git a/src/test/java/tools/ToolsRunner.java b/src/test/java/tools/ToolsRunner.java index 0683d12f4..9986e9819 100644 --- a/src/test/java/tools/ToolsRunner.java +++ b/src/test/java/tools/ToolsRunner.java @@ -1,5 +1,6 @@ package tools; +import tools.utils.AutoToolTask; import tools.utils.ToolTask; import tools.utils.ToolsConstants; @@ -30,37 +31,42 @@ public final class ToolsRunner { Map tasks = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); collectTasksInDirectory(toolsFolder, tasks); - String inputTask; - Scanner scanner = new Scanner(System.in); - boolean interactive = true; - if (args == null || args.length == 0) { - listAllTasks(tasks); - // Prompt user for task and handle input - System.out.println("Please enter the task to run:"); - inputTask = scanner.nextLine(); + promptAndExecuteTask(tasks); } else { - interactive = false; - inputTask = args[0]; + executeAutomaticTasks(tasks, args); } + } + + private static void promptAndExecuteTask(Map tasks) { + System.out.println("The following tasks are available:"); + for (String key : tasks.keySet()) { + System.out.println("- " + key); + } + + System.out.println("Please enter the task to run:"); + Scanner scanner = new Scanner(System.in); + String inputTask = scanner.nextLine(); ToolTask task = tasks.get(inputTask); if (task != null) { - if(interactive) { - task.execute(scanner); - } else { - task.execute(null); - } + task.execute(scanner); } else { System.out.println("Unknown task"); } scanner.close(); } - private static void listAllTasks(Map taskCollection) { - System.out.println("The following tasks are available:"); - for (String key : taskCollection.keySet()) { - System.out.println("- " + key); + private static void executeAutomaticTasks(Map tasks, String... requests) { + for (String taskName : requests) { + ToolTask task = tasks.get(taskName); + if (task == null) { + System.out.format("Unknown task '%s'%n", taskName); + } else if (!(task instanceof AutoToolTask)) { + System.out.format("Task '%s' cannot be run on command line%n", taskName); + } else { + ((AutoToolTask) task).executeDefault(); + } } } diff --git a/src/test/java/tools/commands/CommandPageCreater.java b/src/test/java/tools/commands/CommandPageCreater.java index 17308ebf8..74bc74a9e 100644 --- a/src/test/java/tools/commands/CommandPageCreater.java +++ b/src/test/java/tools/commands/CommandPageCreater.java @@ -10,10 +10,10 @@ import fr.xephi.authme.initialization.AuthMeServiceInitializer; import fr.xephi.authme.permission.PermissionNode; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import tools.utils.AutoToolTask; import tools.utils.FileUtils; import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValueHolder; -import tools.utils.ToolTask; import tools.utils.ToolsConstants; import java.util.Collection; @@ -24,7 +24,7 @@ import static org.mockito.Matchers.isA; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class CommandPageCreater implements ToolTask { +public class CommandPageCreater implements AutoToolTask { private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md"; @@ -35,6 +35,11 @@ public class CommandPageCreater implements ToolTask { @Override public void execute(Scanner scanner) { + executeDefault(); + } + + @Override + public void executeDefault() { CommandInitializer commandInitializer = new CommandInitializer(getMockInitializer()); final Set baseCommands = commandInitializer.getCommands(); NestedTagValue commandTags = new NestedTagValue(); diff --git a/src/test/java/tools/hashmethods/HashAlgorithmsDescriptionTask.java b/src/test/java/tools/hashmethods/HashAlgorithmsDescriptionTask.java index e62f5b3d2..edbf54307 100644 --- a/src/test/java/tools/hashmethods/HashAlgorithmsDescriptionTask.java +++ b/src/test/java/tools/hashmethods/HashAlgorithmsDescriptionTask.java @@ -1,10 +1,10 @@ package tools.hashmethods; import fr.xephi.authme.security.HashAlgorithm; +import tools.utils.AutoToolTask; import tools.utils.FileUtils; import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValueHolder; -import tools.utils.ToolTask; import tools.utils.ToolsConstants; import java.util.Map; @@ -15,13 +15,18 @@ import java.util.Scanner; * * @see {@link fr.xephi.authme.security.HashAlgorithm} */ -public class HashAlgorithmsDescriptionTask implements ToolTask { +public class HashAlgorithmsDescriptionTask implements AutoToolTask { private static final String CUR_FOLDER = ToolsConstants.TOOLS_SOURCE_ROOT + "hashmethods/"; private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "hash_algorithms.md"; @Override public void execute(Scanner scanner) { + executeDefault(); + } + + @Override + public void executeDefault() { // Gather info and construct a row for each method EncryptionMethodInfoGatherer infoGatherer = new EncryptionMethodInfoGatherer(); Map descriptions = infoGatherer.getDescriptions(); diff --git a/src/test/java/tools/permissions/PermissionsListWriter.java b/src/test/java/tools/permissions/PermissionsListWriter.java index 0e54a3834..e0c43b227 100644 --- a/src/test/java/tools/permissions/PermissionsListWriter.java +++ b/src/test/java/tools/permissions/PermissionsListWriter.java @@ -1,9 +1,9 @@ package tools.permissions; +import tools.utils.AutoToolTask; import tools.utils.FileUtils; import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValueHolder; -import tools.utils.ToolTask; import tools.utils.ToolsConstants; import java.util.Map; @@ -14,7 +14,7 @@ import java.util.Set; * Task responsible for formatting a permissions node list and * for writing it to a file if desired. */ -public class PermissionsListWriter implements ToolTask { +public class PermissionsListWriter implements AutoToolTask { private static final String PERMISSIONS_OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "permission_nodes.md"; @@ -25,11 +25,6 @@ public class PermissionsListWriter implements ToolTask { @Override public void execute(Scanner scanner) { - if(scanner == null) { - generateAndWriteFile(); - return; - } - // Ask if result should be written to file System.out.println("Include description? [Enter 'n' for no]"); boolean includeDescription = !matches("n", scanner); @@ -49,6 +44,11 @@ public class PermissionsListWriter implements ToolTask { } } + @Override + public void executeDefault() { + generateAndWriteFile(); + } + private static void generateAndWriteFile() { final NestedTagValue permissionsTagValue = generatePermissionsList(); diff --git a/src/test/java/tools/utils/AutoToolTask.java b/src/test/java/tools/utils/AutoToolTask.java new file mode 100644 index 000000000..b635fe5fe --- /dev/null +++ b/src/test/java/tools/utils/AutoToolTask.java @@ -0,0 +1,13 @@ +package tools.utils; + +/** + * Interface for tasks that can be run automatically, i.e. without any user input. + */ +public interface AutoToolTask extends ToolTask { + + /** + * Execute the task with default settings. + */ + void executeDefault(); + +}