Last active
March 6, 2019 12:38
-
-
Save stalep/525176b1d74870ce884cf628dfb9c917 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.aesh.command.AeshCommandRuntimeBuilder; | |
import org.aesh.command.Command; | |
import org.aesh.command.CommandDefinition; | |
import org.aesh.command.CommandException; | |
import org.aesh.command.CommandNotFoundException; | |
import org.aesh.command.CommandResult; | |
import org.aesh.command.CommandRuntime; | |
import org.aesh.command.Executor; | |
import org.aesh.command.impl.registry.AeshCommandRegistryBuilder; | |
import org.aesh.command.invocation.CommandInvocation; | |
import org.aesh.command.invocation.CommandInvocationConfiguration; | |
import org.aesh.command.invocation.CommandInvocationProvider; | |
import org.aesh.command.option.Option; | |
import org.aesh.command.parser.CommandLineParserException; | |
import org.aesh.command.registry.CommandRegistry; | |
import org.aesh.command.registry.CommandRegistryException; | |
import org.aesh.command.shell.Shell; | |
import org.aesh.command.validator.CommandValidatorException; | |
import org.aesh.command.validator.OptionValidatorException; | |
import org.aesh.readline.Prompt; | |
import org.aesh.readline.action.KeyAction; | |
import java.io.IOException; | |
import java.util.logging.Logger; | |
/** | |
* @author <a href="mailto:[email protected]">Ståle W. Pedersen</a> | |
*/ | |
public class QuarkusCommandRunner { | |
public static void run(Class<? extends Command> command, String[] args) { | |
//all the quarkus code is omitted | |
try { | |
QuarkusContext quarkusContext = new QuarkusContext(); | |
CommandRegistry<QuarkusCommandInvocation> registry = | |
AeshCommandRegistryBuilder.<QuarkusCommandInvocation>builder() | |
.command(command) | |
.create(); | |
CommandRuntime<QuarkusCommandInvocation> runtime = | |
AeshCommandRuntimeBuilder.<QuarkusCommandInvocation>builder() | |
.commandRegistry(registry) | |
.commandInvocationProvider(new QuarkusCommandInvocationProvider(quarkusContext)) | |
.build(); | |
StringBuilder sb = new StringBuilder(registry.getAllCommandNames().iterator().next()); | |
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(' '); | |
} | |
} | |
} | |
} | |
runtime.executeCommand(sb.toString()); | |
} | |
//simplified exceptions for now | |
catch (CommandNotFoundException | CommandException | CommandLineParserException | | |
CommandValidatorException | OptionValidatorException | InterruptedException | | |
IOException | CommandRegistryException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} | |
@CommandDefinition(name = "test", description = "a test...") | |
class QuarkusTestCommandRunner implements Command<QuarkusCommandInvocation> { | |
@Option(shortName = 'c', defaultValue = "foo", description = "name of the caller") | |
private String caller; | |
public static void main(String[] args) { | |
QuarkusCommandRunner.run(QuarkusTestCommandRunner.class, args); | |
} | |
@Override | |
public CommandResult execute(QuarkusCommandInvocation commandInvocation) throws CommandException, InterruptedException { | |
commandInvocation.println("Hi "+caller+"! - you are now using: "+commandInvocation.quarkusContext().name()); | |
return CommandResult.SUCCESS; | |
} | |
} | |
class QuarkusContext { | |
//here we put in all the methods etc we expose to the QuarkusCommandRunners | |
public Logger quarkusLogger() { | |
return null; | |
} | |
public String getAccessToMoreQuarkusStuff() { | |
return null; | |
} | |
public String name() { | |
return "Quarkus"; | |
} | |
} | |
class QuarkusCommandInvocation<CI extends CommandInvocation> implements CommandInvocation { | |
private final CommandInvocation<CI> commandInvocation; | |
private final QuarkusContext context; | |
QuarkusCommandInvocation(CommandInvocation<CI> commandInvocation, QuarkusContext context) { | |
this.commandInvocation = commandInvocation; | |
this.context = context; | |
} | |
@Override | |
public Shell getShell() { | |
return commandInvocation.getShell(); | |
} | |
@Override | |
public void setPrompt(Prompt prompt) { | |
commandInvocation.setPrompt(prompt); | |
} | |
@Override | |
public Prompt getPrompt() { | |
return commandInvocation.getPrompt(); | |
} | |
@Override | |
public String getHelpInfo(String commandName) { | |
return commandInvocation.getHelpInfo(commandName); | |
} | |
@Override | |
public void stop() { | |
commandInvocation.stop(); | |
} | |
@Override | |
public KeyAction input() throws InterruptedException { | |
return commandInvocation.input(); | |
} | |
@Override | |
public String inputLine() throws InterruptedException { | |
return commandInvocation.inputLine(); | |
} | |
@Override | |
public String inputLine(Prompt prompt) throws InterruptedException { | |
return commandInvocation.inputLine(prompt); | |
} | |
@Override | |
public void executeCommand(String input) throws CommandNotFoundException, | |
CommandLineParserException, OptionValidatorException, | |
CommandValidatorException, CommandException, InterruptedException, IOException { | |
commandInvocation.executeCommand(input); | |
} | |
@Override | |
public void print(String msg, boolean paging) { | |
commandInvocation.print(msg, paging); | |
} | |
@Override | |
public void println(String msg, boolean paging) { | |
commandInvocation.println(msg, paging); | |
} | |
public QuarkusContext quarkusContext() { | |
return context; | |
} | |
@Override | |
public Executor<CI> buildExecutor(String line) throws CommandNotFoundException, | |
CommandLineParserException, OptionValidatorException, CommandValidatorException, IOException { | |
return commandInvocation.buildExecutor(line); | |
} | |
@Override | |
public CommandInvocationConfiguration getConfiguration() { | |
return null; | |
} | |
} | |
class QuarkusCommandInvocationProvider implements CommandInvocationProvider<QuarkusCommandInvocation> { | |
private final QuarkusContext context; | |
QuarkusCommandInvocationProvider(QuarkusContext context) { | |
this.context = context; | |
} | |
@Override | |
public QuarkusCommandInvocation enhanceCommandInvocation(CommandInvocation commandInvocation) { | |
return new QuarkusCommandInvocation(commandInvocation, context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment