Merge pull request #3431 from mstruk/KEYCLOAK-3766

KEYCLOAK-3766 kcreg should display help when no arguments are passed to command
This commit is contained in:
Stian Thorgersen 2016-10-28 06:44:07 +02:00 committed by GitHub
commit 7579606ac4
14 changed files with 155 additions and 72 deletions

View file

@ -98,6 +98,14 @@ public abstract class AbstractAuthOptionsCmd extends AbstractGlobalOptionsCmd {
} }
} }
protected boolean noOptions() {
return server == null && realm == null && clientId == null && secret == null &&
user == null && password == null &&
keystore == null && storePass == null && keyPass == null && alias == null &&
trustStore == null && trustPass == null &&
token == null && config == null;
}
protected void processGlobalOptions() { protected void processGlobalOptions() {
super.processGlobalOptions(); super.processGlobalOptions();

View file

@ -27,7 +27,7 @@ public abstract class AbstractGlobalOptionsCmd implements Command {
} }
protected boolean printHelp() { protected boolean printHelp() {
if (help) { if (help || nothingToDo()) {
printOut(help()); printOut(help());
return true; return true;
} }
@ -35,6 +35,10 @@ public abstract class AbstractGlobalOptionsCmd implements Command {
return false; return false;
} }
protected boolean nothingToDo() {
return false;
}
protected String help() { protected String help() {
return KcRegCmd.usage(); return KcRegCmd.usage();
} }

View file

@ -42,44 +42,44 @@ public class ConfigCmd extends AbstractAuthOptionsCmd implements Command {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
if (args == null || args.size() == 0) { if (args != null && args.size() > 0) {
if (printHelp()) { String cmd = args.get(0);
return CommandResult.SUCCESS; switch (cmd) {
} case "credentials": {
ConfigCredentialsCmd command = new ConfigCredentialsCmd();
throw new RuntimeException("Sub-command required by '" + OsUtil.CMD + " config' - one of: 'credentials', 'truststore', 'initial-token', 'registration-token'"); command.initFromParent(this);
} return command.execute(commandInvocation);
}
String cmd = args.get(0); case "truststore": {
switch (cmd) { ConfigTruststoreCmd command = new ConfigTruststoreCmd();
case "credentials": { command.initFromParent(this);
ConfigCredentialsCmd command = new ConfigCredentialsCmd(); return command.execute(commandInvocation);
command.initFromParent(this); }
return command.execute(commandInvocation); case "initial-token": {
} ConfigInitialTokenCmd command = new ConfigInitialTokenCmd();
case "truststore": { command.initFromParent(this);
ConfigTruststoreCmd command = new ConfigTruststoreCmd(); return command.execute(commandInvocation);
command.initFromParent(this); }
return command.execute(commandInvocation); case "registration-token": {
} ConfigRegistrationTokenCmd command = new ConfigRegistrationTokenCmd();
case "initial-token": { command.initFromParent(this);
ConfigInitialTokenCmd command = new ConfigInitialTokenCmd(); return command.execute(commandInvocation);
command.initFromParent(this); }
return command.execute(commandInvocation); default: {
} if (printHelp()) {
case "registration-token": { return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
ConfigRegistrationTokenCmd command = new ConfigRegistrationTokenCmd(); }
command.initFromParent(this); throw new RuntimeException("Unknown sub-command: " + cmd);
return command.execute(commandInvocation);
}
default: {
if (printHelp()) {
return CommandResult.SUCCESS;
} }
throw new RuntimeException("Unknown sub-command: " + cmd);
} }
} }
if (printHelp()) {
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
throw new RuntimeException("Sub-command required by '" + OsUtil.CMD + " config' - one of: 'credentials', 'truststore', 'initial-token', 'registration-token'");
} finally { } finally {
commandInvocation.stop(); commandInvocation.stop();
} }

View file

@ -62,18 +62,23 @@ public class ConfigCredentialsCmd extends AbstractAuthOptionsCmd implements Comm
@Override @Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
return process(commandInvocation); return process(commandInvocation);
} finally { } finally {
commandInvocation.stop(); commandInvocation.stop();
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions();
}
public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
// check server // check server

View file

@ -43,7 +43,7 @@ public class ConfigInitialTokenCmd extends AbstractAuthOptionsCmd implements Com
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
return process(commandInvocation); return process(commandInvocation);
@ -52,6 +52,11 @@ public class ConfigInitialTokenCmd extends AbstractAuthOptionsCmd implements Com
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && parent.args.size() == 1;
}
public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();

View file

@ -40,7 +40,7 @@ public class ConfigRegistrationTokenCmd extends AbstractAuthOptionsCmd implement
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
return process(commandInvocation); return process(commandInvocation);
@ -49,6 +49,11 @@ public class ConfigRegistrationTokenCmd extends AbstractAuthOptionsCmd implement
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && parent.args.size() == 1;
}
public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();

View file

@ -40,7 +40,7 @@ public class ConfigTruststoreCmd extends AbstractAuthOptionsCmd implements Comma
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
return process(commandInvocation); return process(commandInvocation);
@ -49,6 +49,11 @@ public class ConfigTruststoreCmd extends AbstractAuthOptionsCmd implements Comma
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && parent.args.size() == 1;
}
public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
List<String> args = new ArrayList<>(); List<String> args = new ArrayList<>();

View file

@ -100,12 +100,12 @@ public class CreateCmd extends AbstractAuthOptionsCmd implements Command {
List<AttributeOperation> attrs = new LinkedList<>(); List<AttributeOperation> attrs = new LinkedList<>();
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
if (args != null) { if (args != null) {
Iterator<String> it = args.iterator(); Iterator<String> it = args.iterator();
while (it.hasNext()) { while (it.hasNext()) {
@ -230,6 +230,12 @@ public class CreateCmd extends AbstractAuthOptionsCmd implements Command {
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && regType == null && file == null && (args == null || args.size() == 0);
}
protected String help() { protected String help() {
return usage(); return usage();
} }

View file

@ -54,12 +54,12 @@ public class DeleteCmd extends AbstractAuthOptionsCmd {
@Override @Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
if (args == null || args.isEmpty()) { if (args == null || args.isEmpty()) {
throw new RuntimeException("CLIENT not specified"); throw new RuntimeException("CLIENT not specified");
} }
@ -113,6 +113,11 @@ public class DeleteCmd extends AbstractAuthOptionsCmd {
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && (args == null || args.size() == 0);
}
protected String help() { protected String help() {
return usage(); return usage();
} }

View file

@ -72,12 +72,12 @@ public class GetCmd extends AbstractAuthOptionsCmd {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
if (args == null || args.isEmpty()) { if (args == null || args.isEmpty()) {
throw new RuntimeException("CLIENT not specified"); throw new RuntimeException("CLIENT not specified");
} }
@ -175,6 +175,11 @@ public class GetCmd extends AbstractAuthOptionsCmd {
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && endpoint == null && (args == null || args.size() == 0);
}
protected String help() { protected String help() {
return usage(); return usage();
} }

View file

@ -101,12 +101,12 @@ public class UpdateCmd extends AbstractAuthOptionsCmd {
List<AttributeOperation> attrs = new LinkedList<>(); List<AttributeOperation> attrs = new LinkedList<>();
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
String clientId = null; String clientId = null;
if (args != null) { if (args != null) {
@ -333,6 +333,11 @@ public class UpdateCmd extends AbstractAuthOptionsCmd {
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && regType == null && file == null && (args == null || args.size() == 0);
}
protected String help() { protected String help() {
return usage(); return usage();
} }

View file

@ -60,12 +60,12 @@ public class UpdateTokenCmd extends AbstractAuthOptionsCmd {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException { public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try { try {
processGlobalOptions();
if (printHelp()) { if (printHelp()) {
return CommandResult.SUCCESS; return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
} }
processGlobalOptions();
if (args == null || args.isEmpty()) { if (args == null || args.isEmpty()) {
throw new RuntimeException("CLIENT not specified"); throw new RuntimeException("CLIENT not specified");
} }
@ -132,6 +132,11 @@ public class UpdateTokenCmd extends AbstractAuthOptionsCmd {
} }
} }
@Override
protected boolean nothingToDo() {
return noOptions() && (args == null || args.size() == 0);
}
protected String help() { protected String help() {
return usage(); return usage();
} }

View file

@ -51,21 +51,49 @@ public class KcRegTest extends AbstractCliTest {
"Sub-command required by '" + OsUtil.CMD + " config' - one of: 'credentials', 'truststore', 'initial-token', 'registration-token'", "Sub-command required by '" + OsUtil.CMD + " config' - one of: 'credentials', 'truststore', 'initial-token', 'registration-token'",
exe.stderrLines().get(0)); exe.stderrLines().get(0));
exe = execute("config credentials");
assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " config credentials --server SERVER_URL --realm REALM [ARGUMENTS]", exe.stdoutLines().get(0));
exe = execute("config initial-token");
assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " config initial-token --server SERVER --realm REALM [--delete | TOKEN] [ARGUMENTS]", exe.stdoutLines().get(0));
exe = execute("config registration-token");
assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " config registration-token --server SERVER --realm REALM --client CLIENT [--delete | TOKEN] [ARGUMENTS]", exe.stdoutLines().get(0));
exe = execute("config truststore");
assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " config truststore [TRUSTSTORE | --delete] [--trustpass PASSWOD] [ARGUMENTS]", exe.stdoutLines().get(0));
exe = execute("create"); exe = execute("create");
assertExitCodeAndStreamSizes(exe, 1, 0, 1); assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0)); Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " create [ARGUMENTS]", exe.stdoutLines().get(0));
//Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
exe = execute("get"); exe = execute("get");
assertExitCodeAndStreamSizes(exe, 1, 0, 1); assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0)); Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " get CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
//Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
exe = execute("update"); exe = execute("update");
assertExitCodeAndStreamSizes(exe, 1, 0, 1); assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0)); Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " update CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
//Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
exe = execute("delete"); exe = execute("delete");
assertExitCodeAndStreamSizes(exe, 1, 0, 1); assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0)); Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " delete CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
//Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
exe = execute("attrs"); exe = execute("attrs");
Assert.assertEquals("exit code", 0, exe.exitCode()); Assert.assertEquals("exit code", 0, exe.exitCode());
@ -73,8 +101,10 @@ public class KcRegTest extends AbstractCliTest {
Assert.assertEquals("first line", "Attributes for default format:", exe.stdoutLines().get(0)); Assert.assertEquals("first line", "Attributes for default format:", exe.stdoutLines().get(0));
exe = execute("update-token"); exe = execute("update-token");
assertExitCodeAndStreamSizes(exe, 1, 0, 1); assertExitCodeAndStdErrSize(exe, 1, 0);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0)); Assert.assertTrue("help message returned", exe.stdoutLines().size() > 10);
Assert.assertEquals("help message", "Usage: " + OsUtil.CMD + " update-token CLIENT [ARGUMENTS]", exe.stdoutLines().get(0));
//Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
exe = execute("help"); exe = execute("help");
assertExitCodeAndStdErrSize(exe, 0, 0); assertExitCodeAndStdErrSize(exe, 0, 0);

View file

@ -78,13 +78,8 @@ public class KcRegTruststoreTest extends AbstractCliTest {
} }
} }
// Check missing argument error
KcRegExec exe = execute("config truststore");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("no truststore error", "No truststore specified", exe.stderrLines().get(0));
// configure truststore with password // configure truststore with password
exe = execute("config truststore --trustpass secret '" + truststore.getAbsolutePath() + "'"); KcRegExec exe = execute("config truststore --trustpass secret '" + truststore.getAbsolutePath() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0); assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// perform authentication against server - asks for password, then for truststore password // perform authentication against server - asks for password, then for truststore password