You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/05/23 09:48:46 UTC

[camel] 01/05: camel-jbang - Polished

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

commit 5f87d8b01a617ef33fb2b5eef821b46d22c9610f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 23 10:01:40 2022 +0200

    camel-jbang - Polished
---
 .../dsl/jbang/core/commands/CamelJBangMain.java    |  2 +-
 .../camel/dsl/jbang/core/commands/Profile.java     |  3 ++-
 .../{PropertiesHelper.java => ProfileHelper.java}  | 31 +++++++++++++---------
 3 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
index 85752728754..979082ce800 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java
@@ -61,7 +61,7 @@ public class CamelJBangMain implements Callable<Integer> {
             return new String[] { v };
         });
 
-        PropertiesHelper.augmentWithProperties(commandLine, args);
+        ProfileHelper.augmentWithProperties(commandLine, args);
         int exitCode = commandLine.execute(args);
         System.exit(exitCode);
     }
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Profile.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Profile.java
index 92c12948ff2..bc7e5168b81 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Profile.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Profile.java
@@ -25,8 +25,9 @@ import picocli.CommandLine;
 public class Profile implements Callable<Integer> {
 
     @CommandLine.Option(names = { "--profile" }, scope = CommandLine.ScopeType.INHERIT, defaultValue = "application",
-                        description = "Profile")
+                        description = "Profile to use (Loads properties file with the same profile name).")
     private String profile;
+
     @CommandLine.Unmatched
     private List<String> unmatched;
 
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
similarity index 86%
rename from dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java
rename to dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
index 88dec26c6e1..13cd2f42fd5 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ProfileHelper.java
@@ -30,7 +30,11 @@ import java.util.stream.Collectors;
 
 import picocli.CommandLine;
 
-public final class PropertiesHelper {
+/**
+ * Helper for CLI arguments with profile
+ */
+public final class ProfileHelper {
+
     private static final String PROPERTIES_FILE_EXTENSION = ".properties";
     private static final String DEFAULT_PROFILE = "application";
     private static final String PROFILE = "profile";
@@ -40,22 +44,23 @@ public final class PropertiesHelper {
     private static final List<String> COMMON_ARGUMENTS = List.of("namespace", "name", "version");
     private static String propertiesFilename = "application.properties";
 
-    private PropertiesHelper() {
+    private ProfileHelper() {
     }
 
     public static void augmentWithProperties(CommandLine commandLine, String... args) {
-
         String profile = getProfile(args);
-        if (!Objects.equals(profile, DEFAULT_PROFILE)) {
-            propertiesFilename = profile + PROPERTIES_FILE_EXTENSION;
-            // only show if not default
-            System.out.println("Augmenting properties with profile " + profile);
-        }
+        propertiesFilename = profile + PROPERTIES_FILE_EXTENSION;
 
         Properties fileProperties = readProperties();
-        Properties properties = replacePrefix(fileProperties);
-        Properties augmentedProperties = augmentProperties(properties, commandLine);
-        commandLine.setDefaultValueProvider(new CommandLine.PropertiesDefaultProvider(augmentedProperties));
+        if (!fileProperties.isEmpty()) {
+            if (!Objects.equals(profile, DEFAULT_PROFILE)) {
+                // only show if not default
+                System.out.println("Augmenting properties with profile " + profile);
+            }
+            Properties properties = replacePrefix(fileProperties);
+            Properties augmentedProperties = augmentProperties(properties, commandLine);
+            commandLine.setDefaultValueProvider(new CommandLine.PropertiesDefaultProvider(augmentedProperties));
+        }
     }
 
     private static String getProfile(String... args) {
@@ -102,8 +107,8 @@ public final class PropertiesHelper {
     }
 
     private static String generateParameter(String... prefix) {
-        return Arrays.asList(prefix).stream()
-                .filter(s -> s != null)
+        return Arrays.stream(prefix)
+                .filter(Objects::nonNull)
                 .collect(Collectors.joining("."));
     }