This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 4b81416e5295 CAMEL-26931: camel-jbang - a bad value of the user
configuration names the file, the key and the value, and camel config set
refuses it (#29966)
4b81416e5295 is described below
commit 4b81416e5295aac29ace695fb784ffc5f47c1640
Author: Claus Ibsen <[email protected]>
AuthorDate: Sun Nov 15 10:25:57 2026 +0100
CAMEL-26931: camel-jbang - a bad value of the user configuration names the
file, the key and the value, and camel config set refuses it (#29966)
The config part of item 4 of
[CAMEL-26931](https://issues.apache.org/jira/browse/CAMEL-26931); the version
part is #29945. It came after #29917 (the local configuration is laid over the
global one), which changed `CommandLineHelper` too.
## Problem
```
$ camel config set runtime=springboot # exit 0, stored
$ camel catalog component
ERROR: DefaultValueProvider failed for option runtime # exit
2, every command with --runtime
Usage: camel catalog component [-h] [--camel-version=<camel-version>] ...
$ camel config unset nope # exit 0, while camel config get
nope exits 3
```
The message of aesh names neither the file, the key nor the value, and the
user may not even know that the value comes from the configuration.
## Change
- The default value provider checks a value of the user configuration with
the converter of the option before it gives it. A value the option does not
take fails with exit code 2 and says what to do; the parse error handler prints
a `CliException` found in the cause chain of a parse error with its own message
and exit code:
```
ERROR: Invalid value in /home/me/.camel-cli.properties:
runtime=springboot (Unsupported runtime springboot). Change it with camel
config set runtime=<value>, or remove it with camel config unset runtime
```
It names the local `camel-cli.properties` when the key is from there.
- `camel config set` checks the value against the options the key is the
default of (`<option>` for every command with it, `camel.<command
path>.<option>` for one), on a full command line: the command line of a run has
only the command it runs. A value one of them does not take exits 2 and is not
stored. A key that no command of the CLI has is stored with a warning, as a
plugin command may use it. It takes about 300 ms.
```
$ camel config set runtime=springboot
ERROR: Invalid value runtime=springboot: --runtime of camel catalog
component does not take it (Unsupported runtime springboot)
$ camel config set max-seconds=abc
ERROR: Invalid value max-seconds=abc: --max-seconds of camel debug does
not take it (Invalid integer value: 'abc')
$ camel config set nope=1
Warning: no command of the CLI has the option nope; the value is stored,
but only a command of a plugin can use it
```
- `camel config unset` of a key that is not set exits with 3, "nope key not
found", as `camel config get` does, and leaves the file as it was.
A converter that needs more than the value (an aesh context) is left to the
parser, so a check never fails a value the parser takes. Upgrade guide entry
added.
## Tests
- `ConfigValueErrorsTest` (new), on the full command line:
`runtime=springboot` in the configuration fails `camel catalog component` with
exit 2 and the file, key and value; `config set runtime=springboot` exits 2 and
stores nothing, `runtime=spring-boot` is stored; `config set nope=1` is stored
with the warning.
- `ConfigUnsetTest.shouldHandleMissingKeyToUnset` pinned exit 0 and no
output for a key that is not set; it now expects exit 3 and "foo key not
found", with the file unchanged.
- End to end with the launcher built from this branch, as above.
- camel-jbang-core: 3069 + 267 pass (`DependencyUpdateTest` flaked once and
passed on the rerun).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 9 +++
.../dsl/jbang/core/commands/config/ConfigSet.java | 64 ++++++++++++++++++
.../jbang/core/commands/config/ConfigUnset.java | 12 +++-
.../MissingPluginParameterExceptionHandler.java | 10 +++
.../dsl/jbang/core/common/CommandLineHelper.java | 74 ++++++++++++++++++++-
.../core/commands/config/ConfigUnsetTest.java | 6 +-
.../commands/config/ConfigValueErrorsTest.java | 76 ++++++++++++++++++++++
7 files changed, 244 insertions(+), 7 deletions(-)
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index d0b06d00c7d5..b3ed7a493bc6 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -1486,6 +1486,15 @@ whereas an explicit empty list in the config restricts
the plugin to its own com
integration reported Running, did nothing, and exited with `0`. In dev mode
(`--dev`, `camel dev`) the run still waits
for files to be added. The message names the `.yml` files of the folder, as
the YAML DSL reads `.yaml` files only.
+=== camel-jbang - camel config set checks the value, and camel config unset of
a key that is not set exits with 3
+
+`camel config set` checks the value against the options the key is the default
of, and refuses a value one of them
+does not take, such as `runtime=springboot` or `max-seconds=abc`, with exit
code `2`. Before, it stored the value, and
+then every command with that option failed with "DefaultValueProvider failed
for option runtime". Such a value in the
+configuration file now fails a command with a message that names the file, the
key and the value. A key that no
+command of the CLI has is still stored, with a warning. `camel config unset`
of a key that is not set now exits with
+code `3` (not found) and says so, as `camel config get` does; it exited with
`0`.
+
=== camel-jbang - camel run exits with 1 when the integration fails to start
When `camel run` cannot start the integration, such as for a route that cannot
be created or started, it now exits
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigSet.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigSet.java
index 36006eebfc60..a6bc1ec72b32 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigSet.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigSet.java
@@ -17,8 +17,13 @@
package org.apache.camel.dsl.jbang.core.commands.config;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
import org.aesh.command.CommandDefinition;
+import org.aesh.command.impl.internal.ProcessedOption;
import org.aesh.command.option.Argument;
import org.aesh.command.option.Option;
import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
@@ -26,6 +31,7 @@ import
org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.CliException;
import org.apache.camel.dsl.jbang.core.common.CliExitCode;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.CommandTree;
import org.apache.camel.util.StringHelper;
@CommandDefinition(name = "set", description = "Set user configuration value",
generateHelp = true,
@@ -55,6 +61,7 @@ public class ConfigSet extends CamelCommand {
if (name == null || name.isBlank() || configuration.split("=").length
== 1) {
throw CliException.usage("Configuration parameter not in key=value
format: " + configuration);
}
+ checkValue(name.trim(), StringHelper.after(configuration, "=").trim());
CommandLineHelper.createPropertyFile(local);
if (!CommandLineHelper.loadProperties(properties -> {
@@ -69,4 +76,61 @@ public class ConfigSet extends CamelCommand {
return CliExitCode.OK;
}
+
+ /**
+ * Checks the value against the options the key is the default of: {@code
<option>} for every command with that
+ * option, {@code camel.<command path>.<option>} for one command. A value
an option does not take, such as
+ * runtime=springboot, was stored, and then every command with that option
failed (CAMEL-26931). A key that no
+ * command has is stored with a warning, as nothing uses it.
+ */
+ private void checkValue(String key, String value) {
+ if (CommandTree.of(this) == null) {
+ // not run from a command line, such as in a test of this command
alone
+ return;
+ }
+ // all the commands: the command line of a run has only the command it
runs, for a fast start
+ CommandTree root = getMain().newCommandLine();
+ boolean[] known = new boolean[1];
+ String problem = checkValue(root, key, value, known, new HashSet<>());
+ if (problem != null) {
+ throw CliException.usage("Invalid value " + key + "=" + value + ":
" + problem);
+ }
+ if (!known[0]) {
+ printer().printDiagnostic("Warning: no command of the CLI has the
option " + key
+ + "; the value is stored, but only a
command of a plugin can use it");
+ }
+ }
+
+ private static String checkValue(CommandTree tree, String key, String
value, boolean[] known, Set<CommandTree> seen) {
+ if (!seen.add(tree)) {
+ return null;
+ }
+ try {
+ String prefix = tree.getQualifiedName().replace(' ', '.') + ".";
+ for (ProcessedOption option :
tree.processedCommand().getOptions()) {
+ List<String> names = new ArrayList<>();
+ names.add(option.name());
+ names.addAll(option.getAliases());
+ for (String name : names) {
+ if (name.equals(key) || (prefix + name).equals(key)) {
+ known[0] = true;
+ String problem =
CommandLineHelper.invalidValue(option, value);
+ if (problem != null) {
+ return "--" + option.name() + " of " +
tree.getQualifiedName() + " does not take it ("
+ + problem + ")";
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ // a command whose model cannot be built is left out of the check
+ }
+ for (CommandTree sub : tree.getSubcommands().values()) {
+ String problem = checkValue(sub, key, value, known, seen);
+ if (problem != null) {
+ return problem;
+ }
+ }
+ return null;
+ }
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnset.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnset.java
index 44dc24584f97..8346cc333bcc 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnset.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnset.java
@@ -42,13 +42,21 @@ public class ConfigUnset extends CamelCommand {
@Override
public Integer doCall() throws Exception {
+ boolean[] found = new boolean[1];
if (!CommandLineHelper.loadProperties(properties -> {
- properties.remove(key);
- CommandLineHelper.storeProperties(properties, printer(), !global);
+ found[0] = properties.remove(key) != null;
+ if (found[0]) {
+ CommandLineHelper.storeProperties(properties, printer(),
!global);
+ }
}, !global, printer())) {
printer().printErr("Cannot unset configuration: the configuration
file is malformed. Fix it by hand first.");
return CliExitCode.FAILURE;
}
+ if (!found[0]) {
+ // as camel config get does (CAMEL-26931)
+ printer().printErr(key + " key not found");
+ return CliExitCode.NOT_FOUND;
+ }
return CliExitCode.OK;
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/exceptionhandler/MissingPluginParameterExceptionHandler.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/exceptionhandler/MissingPluginParameterExceptionHandler.java
index 937efae100df..e5eb9a71bace 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/exceptionhandler/MissingPluginParameterExceptionHandler.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/exceptionhandler/MissingPluginParameterExceptionHandler.java
@@ -20,6 +20,7 @@ import java.util.Optional;
import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.common.CliException;
import org.apache.camel.dsl.jbang.core.common.CliExitCode;
import org.apache.camel.dsl.jbang.core.common.CommandTree;
import org.apache.camel.dsl.jbang.core.common.JsonOutput;
@@ -48,6 +49,15 @@ public class MissingPluginParameterExceptionHandler {
public int handleParseException(Exception ex, String[] args, CommandTree
root) {
Printer err = main.getOut();
CommandTree command = commandOf(root, args);
+ for (Throwable c = ex.getCause(); c != null; c = c.getCause()) {
+ if (c instanceof CliException cli) {
+ // an error of the CLI while parsing, such as a value of the
user configuration an option does not
+ // take: its message says what to fix, which the message of
the parser does not (CAMEL-26931)
+ err.printErr(cli.getMessage());
+ printJsonError(command, args, cli.getExitCode(),
cli.getMessage());
+ return cli.getExitCode();
+ }
+ }
String message = ex.getMessage();
if (command.getCommand() instanceof UsageErrorHandler handler) {
Integer exitCode = handler.usageError(message, args);
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
index 336ec2943539..512a2c16ac23 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java
@@ -32,7 +32,11 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.aesh.command.DefaultValueProvider;
+import org.aesh.command.converter.Converter;
+import org.aesh.command.converter.ConverterInvocation;
import org.aesh.command.impl.internal.ProcessedOption;
+import org.aesh.command.validator.OptionValidatorException;
+import org.aesh.console.AeshContext;
import org.apache.camel.dsl.jbang.run.common.CamelHome;
import org.apache.camel.dsl.jbang.run.common.Printer;
import org.apache.camel.util.OrderedProperties;
@@ -293,6 +297,69 @@ public final class CommandLineHelper {
return CamelHome.getWorkDir();
}
+ /**
+ * The value of the user configuration for an option, when the option
takes it. A value it does not take, such as
+ * runtime=springboot, failed every command with that option with
"DefaultValueProvider failed for option runtime",
+ * which named neither the file, the key nor the value (CAMEL-26931); it
now fails with exit code 2 and says so.
+ */
+ static String checkConfigValue(ProcessedOption option, String key, String
value) {
+ String problem = invalidValue(option, value);
+ if (problem != null) {
+ throw CliException.usage("Invalid value in " + configFileOf(key) +
": " + key + "=" + value + " (" + problem
+ + "). Change it with camel config set " +
key + "=<value>, or remove it with"
+ + " camel config unset " + key);
+ }
+ return value;
+ }
+
+ /**
+ * Why the option does not take the value, or null when it does (or when
that cannot be told before the command line
+ * is parsed).
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public static String invalidValue(ProcessedOption option, String value) {
+ Converter converter = option.converter();
+ if (converter == null) {
+ return null;
+ }
+ try {
+ converter.convert(new ConverterInvocation() {
+ @Override
+ public String getInput() {
+ return value;
+ }
+
+ @Override
+ public AeshContext getAeshContext() {
+ return null;
+ }
+ });
+ return null;
+ } catch (OptionValidatorException | IllegalArgumentException e) {
+ return e.getMessage() != null ? e.getMessage() :
e.getClass().getSimpleName();
+ } catch (RuntimeException e) {
+ // a converter that needs more than the value: the parser converts
it, and reports what it finds
+ return null;
+ }
+ }
+
+ /** The file of the user configuration the key is from: the local file
wins over the global one. */
+ private static Path configFileOf(String key) {
+ Path local = Paths.get(LOCAL_USER_CONFIG);
+ if (Files.isRegularFile(local)) {
+ Properties properties = new Properties();
+ try (Reader reader = Files.newBufferedReader(local,
StandardCharsets.UTF_8)) {
+ properties.load(reader);
+ if (properties.containsKey(key)) {
+ return local.toAbsolutePath();
+ }
+ } catch (Exception e) {
+ // the global file then
+ }
+ }
+ return getUserPropertyFile(false);
+ }
+
/**
* The defaults of the options from the user configuration: {@code
camel.<command path>.<option>} for one command,
* else {@code <option>} (as the properties default provider of picocli
looked them up). The commands of camel
@@ -317,16 +384,17 @@ public final class CommandLineHelper {
names.addAll(option.getAliases());
if (tree != null) {
for (String name : names) {
- String value =
properties.getProperty(tree.getQualifiedName().replace(' ', '.') + "." + name);
+ String key = tree.getQualifiedName().replace(' ', '.') +
"." + name;
+ String value = properties.getProperty(key);
if (value != null) {
- return value;
+ return checkConfigValue(option, key, value);
}
}
}
for (String name : names) {
String value = properties.getProperty(name);
if (value != null) {
- return value;
+ return checkConfigValue(option, name, value);
}
}
return null;
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnsetTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnsetTest.java
index d0e232ad21d6..a30c7989ce72 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnsetTest.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigUnsetTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.dsl.jbang.core.commands.config;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.commands.UserConfigHelper;
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -55,9 +56,10 @@ class ConfigUnsetTest extends BaseConfigTestSupport {
ConfigUnset command = new ConfigUnset(new
CamelJBangMain().withPrinter(printer));
command.key = "foo";
- command.doCall();
+ // a key that is not set is not found, as for camel config get
(CAMEL-26931); it exited with 0
+ Assertions.assertEquals(CliExitCode.NOT_FOUND, command.doCall());
- Assertions.assertEquals("", printer.getOutput());
+ Assertions.assertEquals("ERROR: foo key not found",
printer.getOutput().trim());
CommandLineHelper.loadProperties(properties -> {
Assertions.assertEquals(2, properties.size());
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigValueErrorsTest.java
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigValueErrorsTest.java
new file mode 100644
index 000000000000..e9be0a8133cc
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/config/ConfigValueErrorsTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.dsl.jbang.core.commands.config;
+
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.dsl.jbang.core.commands.UserConfigHelper;
+import org.apache.camel.dsl.jbang.core.common.CliExitCode;
+import org.apache.camel.dsl.jbang.core.common.CommandLineHelper;
+import org.apache.camel.dsl.jbang.core.common.CommandTree;
+import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * A value of the user configuration that an option does not take
(CAMEL-26931): every command with the option failed
+ * with "DefaultValueProvider failed for option runtime", and camel config set
stored it without a word.
+ */
+class ConfigValueErrorsTest extends BaseConfigTestSupport {
+
+ private final StringPrinter out = new StringPrinter();
+
+ private int run(String... args) {
+ CamelJBangMain main = new CamelJBangMain().withPrinter(out);
+ CommandTree commandLine = main.newCommandLine();
+ CommandLineHelper.augmentWithUserConfiguration(commandLine, out);
+ return main.run(commandLine, args);
+ }
+
+ @Test
+ void aBadValueNamesTheFileTheKeyAndTheValue() throws Exception {
+ UserConfigHelper.createUserConfig("runtime=springboot\n");
+
+ assertThat(run("catalog", "component")).isEqualTo(CliExitCode.USAGE);
+ assertThat(out.getStderr())
+ .contains("Invalid value in " +
CommandLineHelper.getHomeDir().resolve(CommandLineHelper.USER_CONFIG)
+ + ": runtime=springboot")
+ .contains("camel config unset runtime")
+ .doesNotContain("DefaultValueProvider failed");
+ }
+
+ @Test
+ void configSetRefusesAValueTheOptionDoesNotTake() throws Exception {
+ UserConfigHelper.createUserConfig("");
+
+ assertThat(run("config", "set",
"runtime=springboot")).isEqualTo(CliExitCode.USAGE);
+ assertThat(out.getStderr()).contains("Invalid value
runtime=springboot");
+ CommandLineHelper.loadProperties(properties ->
assertThat(properties).doesNotContainKey("runtime"));
+
+ assertThat(run("config", "set",
"runtime=spring-boot")).isEqualTo(CliExitCode.OK);
+ CommandLineHelper.loadProperties(properties ->
assertThat(properties).containsEntry("runtime", "spring-boot"));
+ }
+
+ @Test
+ void configSetWarnsOfAKeyNoCommandHas() throws Exception {
+ UserConfigHelper.createUserConfig("");
+
+ assertThat(run("config", "set", "nope=1")).isEqualTo(CliExitCode.OK);
+ assertThat(out.getStderr()).contains("no command of the CLI has the
option nope");
+ CommandLineHelper.loadProperties(properties ->
assertThat(properties).containsEntry("nope", "1"));
+ }
+}