Skip to content

Instantly share code, notes, and snippets.

@stalep
Created July 18, 2016 00:20
Show Gist options
  • Save stalep/98480622cbd3bcd68ae0be60e0b029c9 to your computer and use it in GitHub Desktop.
Save stalep/98480622cbd3bcd68ae0be60e0b029c9 to your computer and use it in GitHub Desktop.
import org.jboss.aesh.cl.CommandDefinition;
import org.jboss.aesh.cl.GroupCommandDefinition;
import org.jboss.aesh.cl.Option;
import org.jboss.aesh.console.AeshConsoleBuilder;
import org.jboss.aesh.console.AeshConsoleImpl;
import org.jboss.aesh.console.Prompt;
import org.jboss.aesh.console.command.Command;
import org.jboss.aesh.console.command.CommandException;
import org.jboss.aesh.console.command.CommandResult;
import org.jboss.aesh.console.command.invocation.CommandInvocation;
import org.jboss.aesh.console.command.registry.AeshCommandRegistryBuilder;
import org.jboss.aesh.console.command.registry.CommandRegistry;
import org.jboss.aesh.console.settings.Settings;
import org.jboss.aesh.console.settings.SettingsBuilder;
/**
* @author <a href=mailto:stale.pedersen@jboss.org">Ståle W. Pedersen</a>
*/
public class KeycloakCLIExample {
public static void main(String[] args) {
Settings settings =
new SettingsBuilder()
.logging(false)
.readInputrc(false)
.disableCompletion(true)
.disableHistory(true)
.enableAlias(false)
.enableExport(false)
.create();
CommandRegistry registry = new AeshCommandRegistryBuilder()
.command(KCReg.class)
.create();
AeshConsoleImpl console = (AeshConsoleImpl) new AeshConsoleBuilder()
.settings(settings)
.commandRegistry(registry)
.prompt(new Prompt(""))
.create();
if(args.length > 0) {
StringBuilder b = new StringBuilder();
for (String s : args)
b.append(s).append(" ");
console.start();
console.execute("kcreg " + b.toString());
}
else
System.out.println(console.getHelpInfo("kcreg"));
}
@GroupCommandDefinition(name = "kcreg", description = "Keycloak registration blablabl\n yes here is a new line...", groupCommands = {Login.class, InitialAccess.class})
public static class KCReg implements Command {
@Option(hasValue = false)
private boolean help;
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help)
commandInvocation.println(commandInvocation.getHelpInfo("kcreg"));
//make sure we stop æsh after each invocation
commandInvocation.stop();
return CommandResult.SUCCESS;
}
}
@CommandDefinition(name = "login", description = "")
public static class Login implements Command {
@Option(name = "help", shortName = 'h', description = "Display help info", hasValue = false)
private boolean help;
@Option(name = "realm", shortName = 'r', description = "Specify to which realm you want to blablaba")
private String realm;
@Option(name = "user", shortName = 'u', description = "User name you want to balblabla")
private String user;
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help)
commandInvocation.println(commandInvocation.getHelpInfo("kcreg login"));
//do some logic here...
//make sure we stop æsh after each invocation
commandInvocation.stop();
return CommandResult.SUCCESS;
}
}
@CommandDefinition(name = "initial-access", description = "")
public static class InitialAccess implements Command {
@Option(name = "help", shortName = 'h', description = "Display help info", hasValue = false)
private boolean help;
@Option
private String name;
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
if(help)
commandInvocation.println(commandInvocation.getHelpInfo("kcreg initial-access"));
//make sure we stop æsh after each invocation
commandInvocation.stop();
return CommandResult.SUCCESS;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment