-
Notifications
You must be signed in to change notification settings - Fork 8.8k
fix: ensures that cli and env messages are the same #23797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/cli/NonCliPropertyException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright 2021 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.quarkus.runtime.cli; | ||
|
|
||
| public class NonCliPropertyException extends RuntimeException { | ||
|
|
||
| public NonCliPropertyException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,9 @@ | |
| import org.keycloak.quarkus.runtime.Environment; | ||
|
|
||
| import io.smallrye.config.ConfigValue; | ||
|
|
||
| import picocli.CommandLine; | ||
| import picocli.CommandLine.ParameterException; | ||
| import picocli.CommandLine.Help.Ansi; | ||
| import picocli.CommandLine.Model.CommandSpec; | ||
| import picocli.CommandLine.Model.OptionSpec; | ||
|
|
@@ -82,19 +84,38 @@ public final class Picocli { | |
| public static final String NO_PARAM_LABEL = "none"; | ||
| private static final String ARG_KEY_VALUE_SEPARATOR = "="; | ||
|
|
||
| private static class IncludeOptions { | ||
| boolean includeRuntime; | ||
| boolean includeBuildTime; | ||
| } | ||
|
|
||
| private Picocli() { | ||
| } | ||
|
|
||
| public static void parseAndRun(List<String> cliArgs) { | ||
| CommandLine cmd = createCommandLine(cliArgs); | ||
|
|
||
| String[] argArray = cliArgs.toArray(new String[0]); | ||
| if (Environment.isRebuildCheck()) { | ||
| int exitCode = runReAugmentationIfNeeded(cliArgs, cmd); | ||
| int exitCode = 0; | ||
| try { | ||
| // process the cli args first to init the config file and perform validation | ||
| cmd.parseArgs(argArray); | ||
| exitCode = runReAugmentationIfNeeded(cliArgs, cmd); | ||
| } catch (ParameterException ex) { | ||
| try { | ||
| exitCode = cmd.getParameterExceptionHandler().handleParseException(ex, argArray); | ||
| } catch (Exception e) { | ||
| ExecutionExceptionHandler errorHandler = new ExecutionExceptionHandler(); | ||
| errorHandler.error(cmd.getErr(), e.getMessage(), null); | ||
| exitCode = ex.getCommandLine().getCommandSpec().exitCodeOnInvalidInput(); | ||
| } | ||
| } | ||
| exitOnFailure(exitCode, cmd); | ||
| return; | ||
| } | ||
|
|
||
| int exitCode = cmd.execute(cliArgs.toArray(new String[0])); | ||
| int exitCode = cmd.execute(argArray); | ||
| exitOnFailure(exitCode, cmd); | ||
| } | ||
|
|
||
|
|
@@ -228,6 +249,74 @@ private static boolean hasProviderChanges() { | |
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * validate the expected values of non-cli properties | ||
| * | ||
| * @param cliArgs | ||
| * @param abstractCommand | ||
| */ | ||
| public static void validateNonCliConfig(List<String> cliArgs, AbstractCommand abstractCommand, PrintWriter out) { | ||
| IncludeOptions options = getIncludeOptions(cliArgs, abstractCommand, abstractCommand.getName()); | ||
|
|
||
| if (!options.includeBuildTime && !options.includeRuntime) { | ||
| return; | ||
| } | ||
|
|
||
| List<String> ignoredBuildTime = new ArrayList<>(); | ||
| List<String> ignoredRunTime = new ArrayList<>(); | ||
| for (OptionCategory category : abstractCommand.getOptionCategories()) { | ||
| List<PropertyMapper> mappers = new ArrayList<>(); | ||
| Optional.ofNullable(PropertyMappers.getRuntimeMappers().get(category)).ifPresent(mappers::addAll); | ||
| Optional.ofNullable(PropertyMappers.getBuildTimeMappers().get(category)).ifPresent(mappers::addAll); | ||
| for (PropertyMapper mapper : mappers) { | ||
| // bypass the PropertyMappingInterceptor - the transformations may cause unexpected errors | ||
| String value = null; | ||
| ConfigSource configSource = null; | ||
| for (ConfigSource cs : getConfig().getConfigSources()) { | ||
| if (cs.getOrdinal() < 300) { | ||
| break; // don't consider anything below standard env properties | ||
| } | ||
| value = cs.getValue(mapper.getFrom()); | ||
| if (value != null) { | ||
| configSource = cs; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (value == null) { | ||
| continue; | ||
| } | ||
|
|
||
| if (mapper.isBuildTime() && !options.includeBuildTime) { | ||
| ignoredBuildTime.add(mapper.getFrom()); | ||
| continue; | ||
| } | ||
| if (mapper.isRunTime() && !options.includeRuntime) { | ||
| ignoredRunTime.add(mapper.getFrom()); | ||
| continue; | ||
| } | ||
|
|
||
| if (!PropertyMapperParameterConsumer.isExpectedValue(mapper.getExpectedValues(), value)) { | ||
| throw new NonCliPropertyException(PropertyMapperParameterConsumer.getErrorMessage(mapper.getFrom(), | ||
| value, mapper.getExpectedValues(), mapper.getExpectedValues()) + ". From ConfigSource " + configSource.getName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (!ignoredBuildTime.isEmpty()) { | ||
| outputIgnoredProperties(ignoredBuildTime, true, out); | ||
| } else if (!ignoredRunTime.isEmpty()) { | ||
| outputIgnoredProperties(ignoredRunTime, false, out); | ||
| } | ||
| } | ||
|
|
||
| private static void outputIgnoredProperties(List<String> properties, boolean build, PrintWriter out) { | ||
| out.write(String.format("The following %s time non-cli properties were found, but will be ignored during %s time: %s\n", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice if we could use logger here to allow users to filter this? But as already discussed offline, it seems not something we can easily achieve as deferred logging sometimes swallow messages. |
||
| build ? "build" : "run", build ? "run" : "build", | ||
| properties.stream().collect(Collectors.joining(", ")))); | ||
| out.flush(); | ||
| } | ||
|
|
||
| private static boolean hasConfigChanges(CommandLine cmdCommand) { | ||
| Optional<String> currentProfile = ofNullable(Environment.getProfile()); | ||
| Optional<String> persistedProfile = getBuildTimeProperty("kc.profile"); | ||
|
|
@@ -379,26 +468,33 @@ public static CommandLine createCommandLine(List<String> cliArgs) { | |
| return cmd; | ||
| } | ||
|
|
||
| private static IncludeOptions getIncludeOptions(List<String> cliArgs, AbstractCommand abstractCommand, String commandName) { | ||
| IncludeOptions result = new IncludeOptions(); | ||
| if (abstractCommand == null) { | ||
| return result; | ||
| } | ||
| result.includeRuntime = abstractCommand.includeRuntime(); | ||
| result.includeBuildTime = abstractCommand.includeBuildTime(); | ||
|
|
||
| if (!result.includeBuildTime && !result.includeRuntime) { | ||
| return result; | ||
| } else if (result.includeRuntime && !result.includeBuildTime && !ShowConfig.NAME.equals(commandName)) { | ||
| result.includeBuildTime = isRebuilt() || !cliArgs.contains(OPTIMIZED_BUILD_OPTION_LONG); | ||
| } else if (result.includeBuildTime && !result.includeRuntime) { | ||
| result.includeRuntime = isRebuildCheck(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private static void addCommandOptions(List<String> cliArgs, CommandLine command) { | ||
| if (command != null) { | ||
| boolean includeBuildTime = false; | ||
| boolean includeRuntime = false; | ||
|
|
||
| if (command.getCommand() instanceof AbstractCommand) { | ||
| AbstractCommand abstractCommand = command.getCommand(); | ||
| includeRuntime = abstractCommand.includeRuntime(); | ||
| includeBuildTime = abstractCommand.includeBuildTime(); | ||
| } | ||
| if (command != null && command.getCommand() instanceof AbstractCommand) { | ||
| IncludeOptions options = getIncludeOptions(cliArgs, command.getCommand(), command.getCommandName()); | ||
|
|
||
| if (!includeBuildTime && !includeRuntime) { | ||
| if (!options.includeBuildTime && !options.includeRuntime) { | ||
| return; | ||
| } else if (includeRuntime && !includeBuildTime && !ShowConfig.NAME.equals(command.getCommandName())) { | ||
| includeBuildTime = isRebuilt() || !cliArgs.contains(OPTIMIZED_BUILD_OPTION_LONG); | ||
| } else if (includeBuildTime && !includeRuntime) { | ||
| includeRuntime = isRebuildCheck(); | ||
| } | ||
|
|
||
| addOptionsToCli(command, includeBuildTime, includeRuntime); | ||
| addOptionsToCli(command, options.includeBuildTime, options.includeRuntime); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last nitpick. :) We could use better naming of config sources in the error messages to avoid stuff like:
We can't probably rely on the
getName()as we don't own all config sources so we can't change the names there. We'd might need some sort of mapping.But we already use the same naming e.g. in show-config. So definitely follow-up (if anything).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, mentioned this as well in #23797 (comment) - so we'll capture that as a follow-up.