KEYCLOAK-3766 kcreg should display help when no arguments are passed to command

This commit is contained in:
Marko Strukelj 2016-10-27 10:22:52 +02:00
parent 69dddfa73a
commit 5925a99800
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() {
super.processGlobalOptions();

View file

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

View file

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

View file

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

View file

@ -43,7 +43,7 @@ public class ConfigInitialTokenCmd extends AbstractAuthOptionsCmd implements Com
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try {
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
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 {
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 {
try {
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
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 {
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 {
try {
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
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 {
List<String> args = new ArrayList<>();

View file

@ -100,12 +100,12 @@ public class CreateCmd extends AbstractAuthOptionsCmd implements Command {
List<AttributeOperation> attrs = new LinkedList<>();
try {
processGlobalOptions();
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
processGlobalOptions();
if (args != null) {
Iterator<String> it = args.iterator();
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() {
return usage();
}

View file

@ -54,12 +54,12 @@ public class DeleteCmd extends AbstractAuthOptionsCmd {
@Override
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try {
processGlobalOptions();
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
processGlobalOptions();
if (args == null || args.isEmpty()) {
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() {
return usage();
}

View file

@ -72,12 +72,12 @@ public class GetCmd extends AbstractAuthOptionsCmd {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try {
processGlobalOptions();
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
processGlobalOptions();
if (args == null || args.isEmpty()) {
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() {
return usage();
}

View file

@ -101,12 +101,12 @@ public class UpdateCmd extends AbstractAuthOptionsCmd {
List<AttributeOperation> attrs = new LinkedList<>();
try {
processGlobalOptions();
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
processGlobalOptions();
String clientId = 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() {
return usage();
}

View file

@ -60,12 +60,12 @@ public class UpdateTokenCmd extends AbstractAuthOptionsCmd {
public CommandResult execute(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
try {
processGlobalOptions();
if (printHelp()) {
return CommandResult.SUCCESS;
return help ? CommandResult.SUCCESS : CommandResult.FAILURE;
}
processGlobalOptions();
if (args == null || args.isEmpty()) {
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() {
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'",
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");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
assertExitCodeAndStdErrSize(exe, 1, 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");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
assertExitCodeAndStdErrSize(exe, 1, 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");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("error message", "No file nor attribute values specified", exe.stderrLines().get(0));
assertExitCodeAndStdErrSize(exe, 1, 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");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
assertExitCodeAndStdErrSize(exe, 1, 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");
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));
exe = execute("update-token");
assertExitCodeAndStreamSizes(exe, 1, 0, 1);
Assert.assertEquals("error message", "CLIENT not specified", exe.stderrLines().get(0));
assertExitCodeAndStdErrSize(exe, 1, 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");
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
exe = execute("config truststore --trustpass secret '" + truststore.getAbsolutePath() + "'");
KcRegExec exe = execute("config truststore --trustpass secret '" + truststore.getAbsolutePath() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// perform authentication against server - asks for password, then for truststore password