Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AddUserExample {
public static void main(String[] args) {
Settings settings =
new SettingsBuilder()
.logging(false)
.readInputrc(false)
.disableCompletion(true)
.disableHistory(true)
.enableAlias(false)
.enableExport(false)
.enableMan(false)
.create();
CommandRegistry registry = new AeshCommandRegistryBuilder()
.command(AddUserCommand.class)
.command(ExitCommand.class)
.create();
AeshConsoleImpl console = (AeshConsoleImpl) new AeshConsoleBuilder()
.settings(settings)
.commandRegistry(registry)
.prompt(new Prompt("[keycloak] "))
.create();
console.start();
if(args.length > 0) {
StringBuilder builder = new StringBuilder("add-user ");
for(String a : args)
builder.append(a).append(" ");
builder.append(Config.getLineSeparator());
console.execute(builder.toString());
}
else
console.execute("add-user -h"+Config.getLineSeparator());
}
@CommandDefinition(name = "add-user", description = "[options...]")
public static class AddUserCommand implements Command {
@Option(shortName = 'r', description = "Name of realm to add user to")
private String realm;
@Option(shortName = 'u', description = "Name of the user", required = true)
private String user;
@Option(shortName = 'p', description = "Password of the user" )
private String password;
@Option(description = "Roles to add to the user")
private String roles;
@Option(description = "Hash iterations")
private int iterations;
@Option(description = "Enable domain mode")
private boolean domain;
@Option(description = "Add user to underlying container. For usage use '--container --help'")
private boolean container;
@Option(description = "Define the location of the server config directory")
private String sc;
@Option(description = "Define the location of the domain config directory")
private String dc;
@Option(shortName = 'h', hasValue = false, description = "Display this help and exit",
overrideRequired = true)
private boolean help;
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
if(help) {
commandInvocation.getShell().out().println(commandInvocation.getHelpInfo("add-user"));
}
else {
//do stuff here, if all options thats required is specified
doAddUser(commandInvocation);
}
return CommandResult.SUCCESS;
}
private void exit(CommandInvocation commandInvocation) {
commandInvocation.stop();
}
private void doAddUser(CommandInvocation commmandInvocation) throws IOException, InterruptedException {
//do work first...
if(true) {
//if password isnt set, we need to prompt for it
if(password == null) {
password = promptForInput("password: ", '*', commmandInvocation);
}
//do some checks that password was ok... then:
if(password != null && password.length() > 0) {
commmandInvocation.getShell().out().println("User " + user + " was added sucessfully with password: " + password);
exit(commmandInvocation);
}
else
commmandInvocation.getShell().out().println("User "+user+" was not added since password was missing/empty");
}
else {
commmandInvocation.getShell().out().println("User " + user + " could not be added because of blabla");
}
}
private String promptForInput(String prompt, Character mask,
CommandInvocation invocation) throws IOException, InterruptedException {
ConsoleBuffer consoleBuffer = new AeshConsoleBufferBuilder()
.shell(invocation.getShell())
.prompt(new Prompt(prompt, mask))
.create();
InputProcessor inputProcessor = new AeshInputProcessorBuilder()
.consoleBuffer(consoleBuffer)
.create();
consoleBuffer.displayPrompt();
String result;
do {
result = inputProcessor.parseOperation(invocation.getInput());
}
while(result == null );
return result;
}
public String getRealm() {
return realm;
}
public String getUser() {
return user;
}
public String getPassword() {
return password;
}
public String getRoles() {
return roles;
}
public int getIterations() {
return iterations;
}
public boolean isDomain() {
return domain;
}
public boolean isContainer() {
return container;
}
public String getSc() {
return sc;
}
public String getDc() {
return dc;
}
public boolean isHelp() {
return help;
}
}
@CommandDefinition(name="exit", description = "exit the program")
public static class ExitCommand implements Command {
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws IOException, InterruptedException {
commandInvocation.stop();
return CommandResult.SUCCESS;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment