#513 Mark tools runnable by command line with specific interface

This commit is contained in:
ljacqu 2016-05-08 17:22:02 +02:00
parent 662f28ab4f
commit 23da023d53
5 changed files with 59 additions and 30 deletions

View File

@ -1,5 +1,6 @@
package tools; package tools;
import tools.utils.AutoToolTask;
import tools.utils.ToolTask; import tools.utils.ToolTask;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
@ -30,37 +31,42 @@ public final class ToolsRunner {
Map<String, ToolTask> tasks = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Map<String, ToolTask> tasks = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
collectTasksInDirectory(toolsFolder, tasks); collectTasksInDirectory(toolsFolder, tasks);
String inputTask;
Scanner scanner = new Scanner(System.in);
boolean interactive = true;
if (args == null || args.length == 0) { if (args == null || args.length == 0) {
listAllTasks(tasks); promptAndExecuteTask(tasks);
// Prompt user for task and handle input
System.out.println("Please enter the task to run:");
inputTask = scanner.nextLine();
} else { } else {
interactive = false; executeAutomaticTasks(tasks, args);
inputTask = args[0];
} }
}
private static void promptAndExecuteTask(Map<String, ToolTask> 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); ToolTask task = tasks.get(inputTask);
if (task != null) { if (task != null) {
if(interactive) {
task.execute(scanner); task.execute(scanner);
} else {
task.execute(null);
}
} else { } else {
System.out.println("Unknown task"); System.out.println("Unknown task");
} }
scanner.close(); scanner.close();
} }
private static void listAllTasks(Map<String, ToolTask> taskCollection) { private static void executeAutomaticTasks(Map<String, ToolTask> tasks, String... requests) {
System.out.println("The following tasks are available:"); for (String taskName : requests) {
for (String key : taskCollection.keySet()) { ToolTask task = tasks.get(taskName);
System.out.println("- " + key); 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();
}
} }
} }

View File

@ -10,10 +10,10 @@ import fr.xephi.authme.initialization.AuthMeServiceInitializer;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import tools.utils.AutoToolTask;
import tools.utils.FileUtils; import tools.utils.FileUtils;
import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValue.NestedTagValue;
import tools.utils.TagValueHolder; import tools.utils.TagValueHolder;
import tools.utils.ToolTask;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.util.Collection; 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.mock;
import static org.mockito.Mockito.when; 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"; private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md";
@ -35,6 +35,11 @@ public class CommandPageCreater implements ToolTask {
@Override @Override
public void execute(Scanner scanner) { public void execute(Scanner scanner) {
executeDefault();
}
@Override
public void executeDefault() {
CommandInitializer commandInitializer = new CommandInitializer(getMockInitializer()); CommandInitializer commandInitializer = new CommandInitializer(getMockInitializer());
final Set<CommandDescription> baseCommands = commandInitializer.getCommands(); final Set<CommandDescription> baseCommands = commandInitializer.getCommands();
NestedTagValue commandTags = new NestedTagValue(); NestedTagValue commandTags = new NestedTagValue();

View File

@ -1,10 +1,10 @@
package tools.hashmethods; package tools.hashmethods;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import tools.utils.AutoToolTask;
import tools.utils.FileUtils; import tools.utils.FileUtils;
import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValue.NestedTagValue;
import tools.utils.TagValueHolder; import tools.utils.TagValueHolder;
import tools.utils.ToolTask;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.util.Map; import java.util.Map;
@ -15,13 +15,18 @@ import java.util.Scanner;
* *
* @see {@link fr.xephi.authme.security.HashAlgorithm} * @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 CUR_FOLDER = ToolsConstants.TOOLS_SOURCE_ROOT + "hashmethods/";
private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "hash_algorithms.md"; private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "hash_algorithms.md";
@Override @Override
public void execute(Scanner scanner) { public void execute(Scanner scanner) {
executeDefault();
}
@Override
public void executeDefault() {
// Gather info and construct a row for each method // Gather info and construct a row for each method
EncryptionMethodInfoGatherer infoGatherer = new EncryptionMethodInfoGatherer(); EncryptionMethodInfoGatherer infoGatherer = new EncryptionMethodInfoGatherer();
Map<HashAlgorithm, MethodDescription> descriptions = infoGatherer.getDescriptions(); Map<HashAlgorithm, MethodDescription> descriptions = infoGatherer.getDescriptions();

View File

@ -1,9 +1,9 @@
package tools.permissions; package tools.permissions;
import tools.utils.AutoToolTask;
import tools.utils.FileUtils; import tools.utils.FileUtils;
import tools.utils.TagValue.NestedTagValue; import tools.utils.TagValue.NestedTagValue;
import tools.utils.TagValueHolder; import tools.utils.TagValueHolder;
import tools.utils.ToolTask;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.util.Map; import java.util.Map;
@ -14,7 +14,7 @@ import java.util.Set;
* Task responsible for formatting a permissions node list and * Task responsible for formatting a permissions node list and
* for writing it to a file if desired. * 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"; private static final String PERMISSIONS_OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "permission_nodes.md";
@ -25,11 +25,6 @@ public class PermissionsListWriter implements ToolTask {
@Override @Override
public void execute(Scanner scanner) { public void execute(Scanner scanner) {
if(scanner == null) {
generateAndWriteFile();
return;
}
// Ask if result should be written to file // Ask if result should be written to file
System.out.println("Include description? [Enter 'n' for no]"); System.out.println("Include description? [Enter 'n' for no]");
boolean includeDescription = !matches("n", scanner); boolean includeDescription = !matches("n", scanner);
@ -49,6 +44,11 @@ public class PermissionsListWriter implements ToolTask {
} }
} }
@Override
public void executeDefault() {
generateAndWriteFile();
}
private static void generateAndWriteFile() { private static void generateAndWriteFile() {
final NestedTagValue permissionsTagValue = generatePermissionsList(); final NestedTagValue permissionsTagValue = generatePermissionsList();

View File

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