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 f0d8cebd362 Profile option for *.properties (#7614)
f0d8cebd362 is described below
commit f0d8cebd3621a9763d35b66912afb60b52dec549
Author: Marat Gubaidullin <[email protected]>
AuthorDate: Fri May 13 11:48:57 2022 -0400
Profile option for *.properties (#7614)
---
.../dsl/jbang/core/commands/CamelJBangMain.java | 5 ++-
.../camel/dsl/jbang/core/commands/Profile.java | 37 ++++++++++++++++++++++
.../dsl/jbang/core/commands/PropertiesHelper.java | 28 ++++++++++++++--
3 files changed, 66 insertions(+), 4 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 a038891c954..47d3ad10bdb 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
@@ -26,6 +26,9 @@ import picocli.CommandLine.Command;
@Command(name = "camel", description = "Apache Camel CLI",
mixinStandardHelpOptions = true)
public class CamelJBangMain implements Callable<Integer> {
private static CommandLine commandLine;
+ @CommandLine.Option(names = { "--profile" }, scope =
CommandLine.ScopeType.INHERIT, defaultValue = "application",
+ description = "Profile")
+ private String profile;
public static void run(String... args) {
commandLine = new CommandLine(new CamelJBangMain())
@@ -55,7 +58,7 @@ public class CamelJBangMain implements Callable<Integer> {
return new String[] { v };
});
- PropertiesHelper.augmentWithProperties(commandLine);
+ PropertiesHelper.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
new file mode 100644
index 00000000000..92c12948ff2
--- /dev/null
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Profile.java
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import picocli.CommandLine;
+
[email protected]()
+public class Profile implements Callable<Integer> {
+
+ @CommandLine.Option(names = { "--profile" }, scope =
CommandLine.ScopeType.INHERIT, defaultValue = "application",
+ description = "Profile")
+ private String profile;
+ @CommandLine.Unmatched
+ private List<String> unmatched;
+
+ @Override
+ public Integer call() throws Exception {
+ return 0;
+ }
+}
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/PropertiesHelper.java
index 4accbe99a78..51af152decc 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/PropertiesHelper.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Properties;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -30,22 +31,43 @@ import java.util.stream.Collectors;
import picocli.CommandLine;
public final class PropertiesHelper {
- private static final String APPLICATION_PROPERTIES_FILE =
"application.properties";
+ private static final String PROPERTIES_FILE_EXTENSION = ".properties";
+ private static final String DEFAULT_PROFILE = "application";
+ private static final String PROFILE = "profile";
private static final String PROPERTY_PREFIX = "camel.jbang";
private static final String COMMAND_PREFIX = "camel";
private static final String COMMON_PREFIX = COMMAND_PREFIX + ".project.";
private static final List<String> COMMON_ARGUMENTS = List.of("namespace",
"name", "version");
+ private static String propertiesFilename = "application.properties";
private PropertiesHelper() {
}
- public static void augmentWithProperties(CommandLine commandLine) {
+ public static void augmentWithProperties(CommandLine commandLine,
String... args) {
+
+ String profile = getProfile(args);
+ System.out.println("Augmenting properties with profile " + profile);
+ if (!Objects.equals(profile, DEFAULT_PROFILE)) {
+ propertiesFilename = profile + PROPERTIES_FILE_EXTENSION;
+ }
+
Properties fileProperties = readProperties();
Properties properties = replacePrefix(fileProperties);
Properties augmentedProperties = augmentProperties(properties,
commandLine);
commandLine.setDefaultValueProvider(new
CommandLine.PropertiesDefaultProvider(augmentedProperties));
}
+ private static String getProfile(String... args) {
+ CommandLine.ParseResult results = new CommandLine(new Profile())
+ .setStopAtUnmatched(false)
+ .setStopAtPositional(false).parseArgs(args);
+ if (results.hasMatchedOption(PROFILE)) {
+ return results.matchedOption(PROFILE).getValue().toString();
+ } else {
+ return DEFAULT_PROFILE;
+ }
+ }
+
private static Properties augmentProperties(Properties properties, final
CommandLine commandLine) {
List<String> commonArgumentList
= commonArgumentList(new ArrayList<>(),
commandLine.getSubcommands(), commandLine.getCommandName());
@@ -95,7 +117,7 @@ public final class PropertiesHelper {
}
private static Properties readProperties() {
- File defaultsFile = new File(APPLICATION_PROPERTIES_FILE);
+ File defaultsFile = new File(propertiesFilename);
Properties properties = new Properties();
if (defaultsFile.exists()) {
try (FileInputStream fis = new FileInputStream(defaultsFile)) {