From 59823a301ac028635cb85da91ff1f2c202ce75d8 Mon Sep 17 00:00:00 2001 From: Steven Hawkins Date: Tue, 21 Nov 2023 10:19:00 -0500 Subject: [PATCH] fix: skip rebuild if using the tools command (#24858) closes #24551 Signed-off-by: Steve Hawkins --- .../keycloak/quarkus/runtime/cli/Picocli.java | 25 +++++++----- .../quarkus/runtime/cli/command/Tools.java | 2 + .../it/cli/dist/ToolsCommandDistTest.java | 38 +++++++++++++++++++ 3 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ToolsCommandDistTest.java diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java index 75057f6988..a595682ce2 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/Picocli.java @@ -60,6 +60,7 @@ import org.keycloak.quarkus.runtime.cli.command.ImportRealmMixin; import org.keycloak.quarkus.runtime.cli.command.Main; import org.keycloak.quarkus.runtime.cli.command.ShowConfig; import org.keycloak.quarkus.runtime.cli.command.StartDev; +import org.keycloak.quarkus.runtime.cli.command.Tools; import org.keycloak.quarkus.runtime.configuration.ConfigArgsConfigSource; import org.keycloak.quarkus.runtime.configuration.Configuration; import org.keycloak.quarkus.runtime.configuration.PersistedConfigSource; @@ -129,11 +130,19 @@ public final class Picocli { private static int runReAugmentationIfNeeded(List cliArgs, CommandLine cmd) { int exitCode = 0; - if (shouldSkipRebuild(cliArgs)) { + CommandLine currentCommandSpec = getCurrentCommandSpec(cliArgs, cmd.getCommandSpec()); + + if (currentCommandSpec == null) { + return exitCode; // possible if using --version or the user made a mistake + } + + String currentCommandName = currentCommandSpec.getCommandName(); + + if (shouldSkipRebuild(cliArgs, currentCommandName)) { return exitCode; } - if (cliArgs.contains(StartDev.NAME)) { + if (currentCommandName.equals(StartDev.NAME)) { String profile = Environment.getProfile(); if (profile == null) { @@ -141,24 +150,22 @@ public final class Picocli { Environment.forceDevProfile(); } } - if (requiresReAugmentation(getCurrentCommandSpec(cliArgs, cmd.getCommandSpec()))) { + if (requiresReAugmentation(currentCommandSpec)) { exitCode = runReAugmentation(cliArgs, cmd); } return exitCode; } - private static boolean shouldSkipRebuild(List cliArgs) { + private static boolean shouldSkipRebuild(List cliArgs, String currentCommandName) { return cliArgs.contains("--help") || cliArgs.contains("-h") || cliArgs.contains("--help-all") - || cliArgs.contains(ShowConfig.NAME); + || currentCommandName.equals(ShowConfig.NAME) + || currentCommandName.equals(Tools.NAME); } - public static boolean requiresReAugmentation(CommandLine cmdCommand) { - if (cmdCommand == null) { - return false; // possible if using --version or the user made a mistake - } + private static boolean requiresReAugmentation(CommandLine cmdCommand) { if (hasConfigChanges(cmdCommand)) { if (!ConfigArgsConfigSource.getAllCliArgs().contains(StartDev.NAME) && "dev".equals(getConfig().getOptionalValue("kc.profile", String.class).orElse(null))) { return false; diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/command/Tools.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/command/Tools.java index cd91abd689..7aa7c39f15 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/command/Tools.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/command/Tools.java @@ -24,4 +24,6 @@ import picocli.CommandLine.Command; subcommands = {Completion.class}) public class Tools { + public static final String NAME = "tools"; + } diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ToolsCommandDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ToolsCommandDistTest.java new file mode 100644 index 0000000000..ceefe86687 --- /dev/null +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/ToolsCommandDistTest.java @@ -0,0 +1,38 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.keycloak.it.cli.dist; + +import io.quarkus.test.junit.main.Launch; +import io.quarkus.test.junit.main.LaunchResult; + +import org.junit.jupiter.api.Test; +import org.keycloak.it.junit5.extension.DistributionTest; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +@DistributionTest +public class ToolsCommandDistTest { + + @Test + @Launch({ "tools", "completion" }) + void initialCompletionShouldSucceed(LaunchResult result) { + assertTrue(result.getOutput().contains("Define a completion specification"), + () -> "The Output:\n" + result.getOutput() + "doesn't contains the expected string."); + } + +}