Created
September 16, 2019 13:50
-
-
Save stalep/1577b299329874f79255ca91b57dc76f 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
package examples; | |
import org.aesh.AeshConsoleRunner; | |
import org.aesh.AeshRuntimeRunner; | |
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.Executor; | |
import org.aesh.command.GroupCommandDefinition; | |
import org.aesh.command.invocation.CommandInvocation; | |
import org.aesh.command.invocation.CommandInvocationConfiguration; | |
import org.aesh.command.invocation.CommandInvocationProvider; | |
import org.aesh.command.option.Argument; | |
import org.aesh.command.option.Option; | |
import org.aesh.command.parser.CommandLineParserException; | |
import org.aesh.command.settings.SettingsBuilder; | |
import org.aesh.command.shell.Shell; | |
import org.aesh.command.validator.CommandValidatorException; | |
import org.aesh.command.validator.OptionValidatorException; | |
import org.aesh.io.Resource; | |
import org.aesh.readline.Prompt; | |
import org.aesh.readline.action.KeyAction; | |
import org.aesh.terminal.utils.Config; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Logger; | |
public class InfinispanCli { | |
public static void main(String[] args) { | |
AeshRuntimeRunner.builder().interactive(true).command(InfinispanMainCommand.class).args(args).execute(); | |
} | |
@GroupCommandDefinition(name = "infinispan", description = "testing", | |
groupCommands = {ConnectCommand.class, BatchCommand.class}) | |
public static class InfinispanMainCommand implements Command { | |
@Option(shortName = 'h', hasValue = false) | |
private boolean help; | |
@Option(shortName = 'v', hasValue = false) | |
private boolean version; | |
@Option(shortName = 'i', hasValue = false, description = "Starts an interactive Infinispan Shell") | |
private boolean interactive; | |
@Option | |
private String connect; | |
public CommandResult execute(CommandInvocation commandInvocation) { | |
if (help) | |
commandInvocation.println(commandInvocation.getHelpInfo()); | |
if (version) | |
commandInvocation.println(version()); | |
if (interactive) | |
startInteractive(); | |
return CommandResult.SUCCESS; | |
} | |
private String version() { | |
return "Infinispan v2019.9.16"; | |
} | |
private void startInteractive() { | |
InfinispanContext context = new InfinispanContext(); | |
AeshConsoleRunner.builder() | |
.command(ConnectCommand.class) | |
.command(BatchCommand.class) | |
.settings(SettingsBuilder.builder() | |
.commandInvocationProvider(new InfinispanCommandInvocationProvider(context)) | |
.build()) | |
.prompt("[infinispan]$ ") | |
.addExitCommand() | |
.start(); | |
} | |
} | |
@CommandDefinition(name = "connect", description = "Connecting to a Infinispan instance") | |
public static class ConnectCommand implements Command { | |
@Option(shortName = 'h', hasValue = false) | |
private boolean help; | |
@Argument(description = "Connects to the given name") | |
private String connection; | |
public CommandResult execute(CommandInvocation commandInvocation) { | |
if (help) | |
commandInvocation.println(commandInvocation.getHelpInfo()); | |
if (connection != null) | |
connect(); | |
return CommandResult.SUCCESS; | |
} | |
private void connect() { | |
} | |
} | |
@CommandDefinition(name = "batch", description = "Execute a batch of commands") | |
public static class BatchCommand implements Command { | |
@Option(shortName = 'h', hasValue = false) | |
private boolean help; | |
@Argument(description = "Batch file") | |
private Resource file; //æsh will automatically create filecompletor for Resources | |
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException { | |
if (help) | |
commandInvocation.println(commandInvocation.getHelpInfo()); | |
if (file != null && file.isLeaf()) { | |
try { | |
List<String> script = readScriptFile(file); | |
for (String line : script) { | |
commandInvocation.executeCommand(line + Config.getLineSeparator()); | |
} | |
} catch (Exception ex) { | |
throw new CommandException(ex); | |
} | |
} | |
return CommandResult.SUCCESS; | |
} | |
private List<String> readScriptFile(Resource resource) throws IOException { | |
List<String> lines = new ArrayList<>(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(resource.read())); | |
String line = br.readLine(); | |
while (line != null) { | |
if (line.trim().length() > 0 && !line.trim().startsWith("#")) | |
lines.add(line); | |
line = br.readLine(); | |
} | |
return lines; | |
} | |
} | |
} | |
class InfinispanContext { | |
//here we put in all the methods etc we need for the InfinispanContext | |
public Logger infinispanLogger() { | |
return null; | |
} | |
public String name() { | |
return "Infinispan"; | |
} | |
} | |
class InfinispanCommandInvocation<CI extends CommandInvocation> implements CommandInvocation { | |
private final CommandInvocation<CI> commandInvocation; | |
private final InfinispanContext context; | |
InfinispanCommandInvocation(CommandInvocation<CI> commandInvocation, InfinispanContext 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 String getHelpInfo() { | |
return commandInvocation.getHelpInfo(); | |
} | |
@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 InfinispanContext infinispanContext() { | |
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 InfinispanCommandInvocationProvider implements CommandInvocationProvider<InfinispanCommandInvocation> { | |
private final InfinispanContext context; | |
InfinispanCommandInvocationProvider(InfinispanContext context) { | |
this.context = context; | |
} | |
@Override | |
public InfinispanCommandInvocation enhanceCommandInvocation(CommandInvocation commandInvocation) { | |
return new InfinispanCommandInvocation(commandInvocation, context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment