Skip to content

Instantly share code, notes, and snippets.

@stalep
Created September 30, 2019 14:50
Show Gist options
  • Save stalep/c592ea869aa1126eb60951343ab0d040 to your computer and use it in GitHub Desktop.
Save stalep/c592ea869aa1126eb60951343ab0d040 to your computer and use it in GitHub Desktop.
import org.aesh.command.AeshCommandRuntimeBuilder;
import org.aesh.command.Command;
import org.aesh.command.CommandException;
import org.aesh.command.CommandNotFoundException;
import org.aesh.command.CommandRuntime;
import org.aesh.command.impl.registry.AeshCommandRegistryBuilder;
import org.aesh.command.invocation.CommandInvocation;
import org.aesh.command.parser.CommandLineParserException;
import org.aesh.command.registry.CommandRegistryException;
import org.aesh.command.validator.CommandValidatorException;
import org.aesh.command.validator.OptionValidatorException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* @author <a href="mailto:stalep@gmail.com">Ståle Pedersen</a>
*/
public class InfinispanRuntimeRunner {
private Class<? extends Command> command;
private List<Class<? extends Command>> subCommands;
private CommandRuntime runtime;
private String[] args;
private InfinispanRuntimeRunner() {
subCommands = new ArrayList<>();
}
public static InfinispanRuntimeRunner builder() {
return new InfinispanRuntimeRunner();
}
public InfinispanRuntimeRunner command(Class<? extends Command> command) {
this.command = command;
return this;
}
public InfinispanRuntimeRunner subCommand(Class<? extends Command> command) {
subCommands.add(command);
return this;
}
public InfinispanRuntimeRunner commandRuntime(CommandRuntime runtime) {
this.runtime = runtime;
return this;
}
public InfinispanRuntimeRunner args(String[] args) {
this.args = args;
return this;
}
public void execute() {
if (command == null && runtime == null)
throw new RuntimeException("Command needs to be added");
try {
String commandName = null;
if (runtime == null) {
if(subCommands.isEmpty()) {
runtime = AeshCommandRuntimeBuilder.builder().
commandRegistry(AeshCommandRegistryBuilder.builder().command(command).create())
.build();
}
else {
//make sure we get the main command name (lets just create a tmp registry to fetch it)
commandName = AeshCommandRegistryBuilder.builder()
.command(command).create().getAllCommandNames().iterator().next();
AeshCommandRegistryBuilder<CommandInvocation> registryBuilder = AeshCommandRegistryBuilder.builder();
registryBuilder.command(command);
registryBuilder.commands(subCommands);
runtime = AeshCommandRuntimeBuilder.builder().commandRegistry(registryBuilder.create()).build();
}
}
final Set<String> commandNames = runtime.getCommandRegistry().getAllCommandNames();
if (commandNames.isEmpty())
throw new RuntimeException("Command needs to be added to the registry.");
else if (commandNames.size() > 1)
throw new RuntimeException("Only one command can be added to the registry.");
if(commandName == null)
commandName = commandNames.iterator().next();
StringBuilder sb = new StringBuilder(commandName);
if (args.length > 0) {
sb.append(" ");
if (args.length == 1) {
sb.append(args[0]);
} else {
for (String arg : args) {
if (arg.indexOf(' ') >= 0) {
sb.append('"').append(arg).append("\" ");
} else {
sb.append(arg).append(' ');
}
}
}
}
try {
runtime.executeCommand(sb.toString());
} catch (CommandNotFoundException e) {
System.err.println("Command not found: " + sb.toString());
} catch (CommandException | CommandLineParserException | CommandValidatorException | OptionValidatorException e) {
showHelpIfNeeded(runtime, commandName, e);
} catch (InterruptedException | IOException e) {
System.err.println(e.getMessage());
}
} catch (CommandRegistryException e) {
throw new RuntimeException("Exception while executing command: " + e.getMessage());
}
}
private static void showHelpIfNeeded(CommandRuntime runtime, String commandName, Exception e) {
if (e != null) {
System.err.println(e.getMessage());
}
System.err.println(runtime.commandInfo(commandName));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment