This is an automated email from the ASF dual-hosted git repository. marat 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 ef24ea4fdf6 CAMEL-18023. camel-jbang command options from application.properties (#7511) ef24ea4fdf6 is described below commit ef24ea4fdf6233a20c20953f0800efc6bb6f8927 Author: Marat Gubaidullin <marat.gubaidul...@gmail.com> AuthorDate: Thu Apr 28 08:42:30 2022 -0400 CAMEL-18023. camel-jbang command options from application.properties (#7511) * Options from properties * Checkstyle * Logging improvement --- .../dsl/jbang/core/commands/CamelJBangMain.java | 1 + .../dsl/jbang/core/commands/PropertiesHelper.java | 109 +++++++++++++++++++++ 2 files changed, 110 insertions(+) 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 551917ccc79..00c3b149cb4 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 @@ -48,6 +48,7 @@ public class CamelJBangMain implements Callable<Integer> { .addSubcommand("create", new CommandLine(new Create()) .addSubcommand("project", new Project())); + PropertiesHelper.augmentWithProperties(commandLine); 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/PropertiesHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java new file mode 100644 index 00000000000..03505bedfd7 --- /dev/null +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/PropertiesHelper.java @@ -0,0 +1,109 @@ +/* + * 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.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import picocli.CommandLine; + +public final class PropertiesHelper { + private static final Logger LOG = LoggerFactory.getLogger(CamelJBangMain.class); + private static final String APPLICATION_PROPERTIES_FILE = "application.properties"; + private static final String PROPERTY_PREFIX = "camel-jbang"; + private static final String COMMAND_PREFIX = "CamelJBang"; + private static final String COMMON_PREFIX = COMMAND_PREFIX + ".project."; + private static final List<String> COMMON_ARGUMENTS = List.of("namespace", "name", "version"); + + private PropertiesHelper() { + } + + public static void augmentWithProperties(CommandLine commandLine) { + Properties fileProperties = readProperties(); + Properties properties = replacePrefix(fileProperties); + Properties augmentedProperties = augmentProperties(properties, commandLine); + commandLine.setDefaultValueProvider(new CommandLine.PropertiesDefaultProvider(augmentedProperties)); + } + + private static Properties augmentProperties(Properties properties, final CommandLine commandLine) { + List<String> commonArgumentList = commonArgumentList(new ArrayList<>(), commandLine.getSubcommands(), commandLine.getCommandName()); + commonArgumentList.forEach(key -> { + if (!properties.contains(key)) { + String[] parts = key.split(Pattern.quote(".")); + String arg = parts[parts.length - 1]; + if (COMMON_ARGUMENTS.contains(arg) && properties.containsKey(COMMON_PREFIX + arg)) { + String value = properties.getProperty(COMMON_PREFIX + arg); + properties.put(key, value); + } + } + }); + return properties; + } + + private static List<String> commonArgumentList(List<String> list, Map<String, CommandLine> commandLines, String parentPrefix) { + commandLines.forEach((name, subCommand) -> { + subCommand.getCommandSpec().args().stream() + .filter(arg -> arg instanceof CommandLine.Model.OptionSpec) + .map(arg -> (CommandLine.Model.OptionSpec) arg) + .filter(arg -> COMMON_ARGUMENTS.contains(arg.longestName().replace("--", ""))) + .map(arg -> arg.longestName().replace("--", "")) + .forEach(arg -> list.add(generateParameter(parentPrefix, subCommand.getCommandName(), arg))); + + list.addAll(commonArgumentList(new ArrayList<>(), subCommand.getSubcommands(), generateParameter(parentPrefix, name))); + }); + return list; + } + + private static String generateParameter(String... prefix) { + return Arrays.asList(prefix).stream() + .filter(s -> s != null) + .collect(Collectors.joining(".")); + } + + private static Properties replacePrefix(Properties properties) { + Properties result = new Properties(); + properties.forEach((key, value) -> { + if (key.toString().startsWith(PROPERTY_PREFIX)) { + result.put(key.toString().replace(PROPERTY_PREFIX, COMMAND_PREFIX), value); + } + }); + return result; + } + + private static Properties readProperties() { + File defaultsFile = new File(APPLICATION_PROPERTIES_FILE); + Properties properties = new Properties(); + if (defaultsFile.exists()) { + try (FileInputStream fis = new FileInputStream(defaultsFile)) { + properties.load(fis); + } catch (IOException e) { + LOG.error("Error reading defaults file: {}", e.getMessage(), e); + } + } + return properties; + } +}