diff --git a/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/EntryCommand.java b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/EntryCommand.java index b69e40f467..f285fb1c3a 100644 --- a/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/EntryCommand.java +++ b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/EntryCommand.java @@ -1,11 +1,9 @@ package io.quarkus.ts.qe.command; import io.quarkus.logging.Log; -import io.quarkus.picocli.runtime.annotations.TopCommand; import picocli.CommandLine; -@TopCommand @CommandLine.Command(name = "customized-command", mixinStandardHelpOptions = true, subcommands = { HelloCommand.class, AgeCommand.class }) public class EntryCommand implements Runnable { @@ -14,6 +12,6 @@ public class EntryCommand implements Runnable { @Override public void run() { - Log.info("Running command with name: " + spec.name()); + Log.info("Running EntryCommand with name: " + spec.name()); } } diff --git a/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherCommand.java b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherCommand.java new file mode 100644 index 0000000000..403f49b946 --- /dev/null +++ b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherCommand.java @@ -0,0 +1,26 @@ +package io.quarkus.ts.qe.command; + +import io.quarkus.logging.Log; + +import picocli.CommandLine; + +@CommandLine.Command(name = "start", description = "Starts the service.") +public class OtherCommand implements Runnable { + + @CommandLine.Mixin + CommonOptions commonOptions; + + @CommandLine.Option(names = { "-t", "--timeout" }, description = "Timeout in seconds", defaultValue = "60") + int timeout; + + @Override + public void run() { + Log.infof("Service started with timeout: %d and verbosity: %s%n", timeout, commonOptions); + } + +} + +class CommonOptions { + @CommandLine.Option(names = { "-v", "--verbose" }, description = "Enable verbose mode") + boolean verbose; +} diff --git a/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherEntryCommand.java b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherEntryCommand.java new file mode 100644 index 0000000000..565c61b57e --- /dev/null +++ b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/command/OtherEntryCommand.java @@ -0,0 +1,16 @@ +package io.quarkus.ts.qe.command; + +import io.quarkus.logging.Log; + +import picocli.CommandLine; + +@CommandLine.Command(name = "other", mixinStandardHelpOptions = true, subcommands = { OtherCommand.class }) +public class OtherEntryCommand implements Runnable { + @CommandLine.Spec + CommandLine.Model.CommandSpec spec; + + @Override + public void run() { + Log.info("OtherEntryCommand with name " + spec.name()); + } +} diff --git a/quarkus-picocli/src/main/java/io/quarkus/ts/qe/configuration/Config.java b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/configuration/Config.java new file mode 100644 index 0000000000..9727de05b6 --- /dev/null +++ b/quarkus-picocli/src/main/java/io/quarkus/ts/qe/configuration/Config.java @@ -0,0 +1,27 @@ +package io.quarkus.ts.qe.configuration; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; + +import io.quarkus.arc.profile.IfBuildProfile; +import io.quarkus.picocli.runtime.annotations.TopCommand; +import io.quarkus.ts.qe.command.EntryCommand; +import io.quarkus.ts.qe.command.OtherEntryCommand; + +@ApplicationScoped +public class Config { + @Produces + @TopCommand + @IfBuildProfile("dev") + public Object devCommand() { + return EntryCommand.class; + } + + @Produces + @TopCommand + @IfBuildProfile("prod") + public Object prodCommand() { + return OtherEntryCommand.class; + } + +} \ No newline at end of file diff --git a/quarkus-picocli/src/main/resources/application.properties b/quarkus-picocli/src/main/resources/application.properties new file mode 100644 index 0000000000..cd11129cd8 --- /dev/null +++ b/quarkus-picocli/src/main/resources/application.properties @@ -0,0 +1 @@ +quarkus.profile=dev diff --git a/quarkus-picocli/src/test/java/PicocliIT.java b/quarkus-picocli/src/test/java/PicocliIT.java index 3417112942..16909926cc 100644 --- a/quarkus-picocli/src/test/java/PicocliIT.java +++ b/quarkus-picocli/src/test/java/PicocliIT.java @@ -21,33 +21,33 @@ public class PicocliIT { static final RestService greetingApp = new RestService() .withProperty("quarkus.args", GREETING_COMMAND_DESC + " " + GREETING_NAME_OPTION + " " + GREETING_NAME_VALUE) + .withProperty("quarkus.profile", "dev") .setAutoStart(false); @QuarkusApplication static final RestService ageApp = new RestService() .withProperty("quarkus.args", AGE_COMMAND_DESC + " " + AGE_OPTION + " " + AGE_VALUE) + .withProperty("quarkus.profile", "dev") .setAutoStart(false); @QuarkusApplication static final RestService greetingBlankArgumentApp = new RestService() .withProperty("quarkus.args", "" + " " + GREETING_NAME_OPTION + " " + GREETING_NAME_VALUE) + .withProperty("quarkus.profile", "dev") .setAutoStart(false); @QuarkusApplication static final RestService greetingInvalidArgumentApp = new RestService() .withProperty("quarkus.args", GREETING_COMMAND_DESC + " " + "-x" + " " + GREETING_NAME_VALUE) + .withProperty("quarkus.profile", "dev") .setAutoStart(false); @QuarkusApplication static final RestService bothTopCommandApp = new RestService() - .setAutoStart(false); - - @QuarkusApplication - static final RestService customizedCommandApp = new RestService() - .withProperty("quarkus.args", "customized-command") + .withProperty("quarkus.profile", "dev") .setAutoStart(false); @Test @@ -62,11 +62,12 @@ public void verifyErrorForApplicationScopedBeanInPicocliCommand() { } @Test - public void verifyGreetingCommandOutputsExpectedMessage() { + public void verifyGreetingCommandOutputsExpectedMessage() throws InterruptedException { String expectedOutput = String.format("Hello %s!", GREETING_NAME_VALUE); try { runAsync(greetingApp::start); greetingApp.logs().assertContains(expectedOutput); + Thread.sleep(3500); } finally { greetingApp.stop(); } @@ -114,14 +115,4 @@ public void verifyErrorForMultipleCommandsWithoutTopCommand() { } } - @Test - public void verifyCustomizedCommandLineBehavior() { - String expectedOutput = "customized"; - try { - runAsync(customizedCommandApp::start); - customizedCommandApp.logs().assertContains(expectedOutput); - } finally { - customizedCommandApp.stop(); - } - } } diff --git a/quarkus-picocli/src/test/java/ProdiIT.java b/quarkus-picocli/src/test/java/ProdiIT.java new file mode 100644 index 0000000000..7eb1041373 --- /dev/null +++ b/quarkus-picocli/src/test/java/ProdiIT.java @@ -0,0 +1,29 @@ +import static java.util.concurrent.CompletableFuture.runAsync; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.bootstrap.RestService; +import io.quarkus.test.scenarios.QuarkusScenario; +import io.quarkus.test.services.QuarkusApplication; + +@Disabled ("No possible to test 2 profiles with the same build") +@QuarkusScenario +public class ProdiIT { + @QuarkusApplication + static final RestService customized = new RestService() + .withProperty("quarkus.args", "start -t 60 -v") + .withProperty("quarkus.profile", "prod") + .setAutoStart(false); + + @Test + public void verifyCustomizedCommandLineBehavior() { + String expectedOutput = "Service started with timeout: 60 and verbosity"; + try { + runAsync(customized::start); + customized.logs().assertContains(expectedOutput); + } finally { + customized.stop(); + } + } +}