gnodet-bot commented on code in PR #29917:
URL: https://github.com/apache/camel/pull/29917#discussion_r4573131896
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java:
##########
@@ -347,8 +347,8 @@ protected int terminalWidth() {
protected void printConfigurationValues(String header) {
if (processedCommand != null) {
- final Properties configProperties = new Properties();
- CommandLineHelper.loadProperties(configProperties::putAll);
+ // the global user configuration with the local one laid over it,
as the option defaults come from
Review Comment:
🔤 **Nit: dangling comment** — The sentence ends with `come from` but has no
object; it reads as though part of the comment was cut off.
```suggestion
// the global user configuration with the local one laid over
it, as the option defaults come from it
```
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java:
##########
@@ -136,36 +136,45 @@ public static void
augmentWithUserConfiguration(CommandTree commandLine) {
}
public static void augmentWithUserConfiguration(CommandTree commandLine,
Printer printer) {
- Path file = getUserConfigurationFile();
- if (Files.isRegularFile(file) && Files.exists(file)) {
- Properties properties = new Properties();
- try (Reader reader = Files.newBufferedReader(file,
StandardCharsets.UTF_8)) {
- properties.load(reader);
- } catch (Exception e) {
- // best-effort: a malformed config file (e.g. malformed
unicode escape) must never break the CLI;
- // warn the user so they know to fix it, but let the command
continue without defaults.
- // Warn once per path: loadProperties may also warn for the
same file, so we deduplicate here.
- String key = file.toAbsolutePath().toString();
- if (WARNED_PATHS.add(key)) {
- printer.printErr(
- "Warning: could not parse '" +
file.toAbsolutePath() + "' (" + e.getMessage()
- + "). Edit the file by hand, escaping
'\\' as '\\\\' or using '/'.");
- }
- return;
- }
-
+ Properties properties = loadUserConfiguration(printer);
+ if (!properties.isEmpty()) {
commandLine.setDefaultValueProvider(new
CamelUserConfigDefaultValueProvider(properties));
}
}
- private static Path getUserConfigurationFile() {
- Path file;
- if (Files.exists(Paths.get(LOCAL_USER_CONFIG))) {
- file = Paths.get(LOCAL_USER_CONFIG);
- } else {
- file = getUserPropertyFile(false);
+ /**
+ * The user configuration as the commands use it: the global file in the
user home ({@value #USER_CONFIG}), with the
+ * local file of the current directory ({@value #LOCAL_USER_CONFIG}) laid
over it, so a key of the local file wins.
+ * Before, a local file hid the whole global one, such as its repos or
camel-version (CAMEL-26931). A file that
+ * cannot be parsed is left out, with a warning (once per file).
+ */
+ public static Properties loadUserConfiguration(Printer printer) {
+ Properties answer = new OrderedProperties();
+ readInto(getUserPropertyFile(false), answer, printer);
+ readInto(Paths.get(LOCAL_USER_CONFIG), answer, printer);
+ return answer;
+ }
+
+ private static void readInto(Path file, Properties answer, Printer
printer) {
+ if (!Files.isRegularFile(file)) {
+ return;
+ }
+ Properties properties = new Properties();
Review Comment:
💡 **Minor consistency nit** — `loadUserConfiguration` accumulates into an
`OrderedProperties` for stable key ordering, but `readInto`'s intermediate
buffer uses plain `new Properties()` (JDK `Hashtable` under the hood —
iteration order is undefined). `answer.putAll(properties)` transfers in
whatever order the JDK happens to iterate, which could scramble the insertion
order that `OrderedProperties` tries to maintain. Consider using
`OrderedProperties` for the intermediate load too:
```suggestion
Properties properties = new OrderedProperties();
```
##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CommandLineHelper.java:
##########
@@ -136,36 +136,45 @@ public static void
augmentWithUserConfiguration(CommandTree commandLine) {
}
public static void augmentWithUserConfiguration(CommandTree commandLine,
Printer printer) {
- Path file = getUserConfigurationFile();
- if (Files.isRegularFile(file) && Files.exists(file)) {
- Properties properties = new Properties();
- try (Reader reader = Files.newBufferedReader(file,
StandardCharsets.UTF_8)) {
- properties.load(reader);
- } catch (Exception e) {
- // best-effort: a malformed config file (e.g. malformed
unicode escape) must never break the CLI;
- // warn the user so they know to fix it, but let the command
continue without defaults.
- // Warn once per path: loadProperties may also warn for the
same file, so we deduplicate here.
- String key = file.toAbsolutePath().toString();
- if (WARNED_PATHS.add(key)) {
- printer.printErr(
- "Warning: could not parse '" +
file.toAbsolutePath() + "' (" + e.getMessage()
- + "). Edit the file by hand, escaping
'\\' as '\\\\' or using '/'.");
- }
- return;
- }
-
+ Properties properties = loadUserConfiguration(printer);
+ if (!properties.isEmpty()) {
commandLine.setDefaultValueProvider(new
CamelUserConfigDefaultValueProvider(properties));
}
}
- private static Path getUserConfigurationFile() {
- Path file;
- if (Files.exists(Paths.get(LOCAL_USER_CONFIG))) {
- file = Paths.get(LOCAL_USER_CONFIG);
- } else {
- file = getUserPropertyFile(false);
+ /**
+ * The user configuration as the commands use it: the global file in the
user home ({@value #USER_CONFIG}), with the
+ * local file of the current directory ({@value #LOCAL_USER_CONFIG}) laid
over it, so a key of the local file wins.
+ * Before, a local file hid the whole global one, such as its repos or
camel-version (CAMEL-26931). A file that
+ * cannot be parsed is left out, with a warning (once per file).
Review Comment:
📝 **Missing Javadoc tags** — New public methods should document their
parameters and return value. The `@param printer` and `@return` tags are absent:
```suggestion
/**
* The user configuration as the commands use it: the global file in the
user home ({@value #USER_CONFIG}), with the
* local file of the current directory ({@value #LOCAL_USER_CONFIG})
laid over it, so a key of the local file wins.
* Before, a local file hid the whole global one, such as its repos or
camel-version (CAMEL-26931). A file that
* cannot be parsed is left out, with a warning (once per file).
*
* @param printer the printer used to report parse warnings (once per
file)
* @return the merged user configuration (global overridden by local),
never {@code null}
*/
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]