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 130ffa34301 CAMEL-18118: camel-jbang - Linux terminal scripting using
pipes
130ffa34301 is described below
commit 130ffa3430136f7e8501a68b066513a749159a6e
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue May 17 19:34:33 2022 +0200
CAMEL-18118: camel-jbang - Linux terminal scripting using pipes
---
.../dsl/jbang/core/commands/AbstractSearch.java | 2 +-
.../dsl/jbang/core/commands/CamelJBangMain.java | 18 +-----
.../apache/camel/dsl/jbang/core/commands/Pipe.java | 35 +++++++++++-
.../apache/camel/dsl/jbang/core/commands/Run.java | 64 +++++++++++-----------
.../camel/dsl/jbang/core/common/RuntimeUtil.java | 6 +-
.../src/main/resources/log4j2-pipe.properties | 28 ++++++++++
6 files changed, 100 insertions(+), 53 deletions(-)
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
index 642b3c0d5f2..6aab0a48181 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/AbstractSearch.java
@@ -55,7 +55,7 @@ public abstract class AbstractSearch {
protected void downloadResource(File indexFile) throws
ResourceDoesNotExist, IOException {
// turn off logging as we use camel to download
- RuntimeUtil.configureLog("off", true, false);
+ RuntimeUtil.configureLog("off", true, false, false);
KameletMain main = new KameletMain();
main.start();
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 4df7651cfe6..85752728754 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
@@ -35,6 +35,7 @@ public class CamelJBangMain implements Callable<Integer> {
.addSubcommand("run", new CommandLine(new Run()))
.addSubcommand("init", new CommandLine(new Init()))
.addSubcommand("bind", new CommandLine(new Bind()))
+ .addSubcommand("pipe", new CommandLine(new Pipe()))
.addSubcommand("package", new CommandLine(new Package())
.addSubcommand("uber-jar", new UberJar()))
.addSubcommand("generate", new CommandLine(new CodeGenerator())
@@ -54,23 +55,6 @@ public class CamelJBangMain implements Callable<Integer> {
.addSubcommand("project", new Project()));
*/
- // special for pipe mode
- if (args.length == 2 && "pipe".equals(args[0])) {
- Pipe pipe = new Pipe();
- pipe.file = args[1];
- if (pipe.file.startsWith("./")) {
- pipe.file = pipe.file.substring(2);
- }
- int exitCode;
- try {
- exitCode = pipe.call();
- } catch (Exception e) {
- e.printStackTrace();
- exitCode = 1;
- }
- System.exit(exitCode);
- }
-
commandLine.getCommandSpec().versionProvider(() -> {
CamelCatalog catalog = new DefaultCamelCatalog();
String v = catalog.getCatalogVersion();
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
index c6234f3a6e2..f9f9114458e 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
@@ -23,12 +23,45 @@ import picocli.CommandLine;
@CommandLine.Command(name = "pipe", description = "Run Camel in pipe and
filters mode for terminal scripting")
class Pipe implements Callable<Integer> {
- // the name of the file itself
+ @CommandLine.Parameters(description = "Name of file", arity = "1")
String file;
+ //CHECKSTYLE:OFF
+ @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true,
description = "Display the help and sub-commands")
+ boolean helpRequested;
+ //CHECKSTYLE:ON
+
+ @CommandLine.Option(names = { "--max-messages" }, defaultValue = "0",
+ description = "Max number of messages to process
before stopping")
+ int maxMessages;
+
+ @CommandLine.Option(names = { "--max-seconds" }, defaultValue = "0",
description = "Max seconds to run before stopping")
+ int maxSeconds;
+
+ @CommandLine.Option(names = { "--max-idle-seconds" }, defaultValue = "1",
+ description = "For how long time in seconds Camel can
be idle before stopping")
+ int maxIdleSeconds;
+
+ @CommandLine.Option(names = { "--logging" }, defaultValue = "true",
description = "Can be used to turn off logging")
+ boolean logging = true;
+
+ @CommandLine.Option(names = { "--logging-level" }, defaultValue = "info",
description = "Logging level")
+ String loggingLevel;
+
@Override
public Integer call() throws Exception {
+ // remove leading ./ when calling a script in pipe mode
+ if (file != null && file.startsWith("./")) {
+ file = file.substring(2);
+ }
+
Run run = new Run();
+ run.logging = logging;
+ run.loggingLevel = loggingLevel;
+ run.loggingColor = false;
+ run.maxSeconds = maxSeconds;
+ run.maxMessages = maxMessages;
+ run.maxIdleSeconds = maxIdleSeconds;
return run.runPipe(file);
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
index 61cd977eb48..c28f6793aa7 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java
@@ -85,91 +85,91 @@ class Run implements Callable<Integer> {
private boolean silentRun;
private boolean pipeRun;
+ //CHECKSTYLE:OFF
@Parameters(description = "The Camel file(s) to run. If no files specified
then application.properties is used as source for which files to run.",
arity = "0..9")
- private String[] files;
+ String[] files;
- //CHECKSTYLE:OFF
@Option(names = {"-h", "--help"}, usageHelp = true, description = "Display
the help and sub-commands")
- private boolean helpRequested;
- //CHECKSTYLE:ON
+ boolean helpRequested;
@Option(names = {
"--dep", "--deps" }, description = "Add additional dependencies
(Use commas to separate them).")
- private String dependencies;
+ String dependencies;
@Option(names = { "--name" }, defaultValue = "CamelJBang", description =
"The name of the Camel application")
- private String name;
+ String name;
@Option(names = { "--logging" }, defaultValue = "true", description = "Can
be used to turn off logging")
- private boolean logging = true;
+ boolean logging = true;
@Option(names = { "--logging-level" }, defaultValue = "info", description
= "Logging level")
- private String loggingLevel;
+ String loggingLevel;
- @Option(names = { "--logging-color" }, defaultValue = "true", description
= "Use colored loggging")
- private boolean loggingColor = true;
+ @Option(names = { "--logging-color" }, defaultValue = "true", description
= "Use colored logging")
+ boolean loggingColor = true;
@Option(names = { "--logging-json" }, description = "Use JSON logging (ECS
Layout)")
- private boolean loggingJson;
+ boolean loggingJson;
@Option(names = { "--stop" }, description = "Stop all running instances of
Camel JBang")
- private boolean stopRequested;
+ boolean stopRequested;
@Option(names = { "--max-messages" }, defaultValue = "0", description =
"Max number of messages to process before stopping")
- private int maxMessages;
+ int maxMessages;
@Option(names = { "--max-seconds" }, defaultValue = "0", description =
"Max seconds to run before stopping")
- private int maxSeconds;
+ int maxSeconds;
@Option(names = { "--max-idle-seconds" }, defaultValue = "0",
description = "For how long time in seconds Camel can be idle
before stopping")
- private int maxIdleSeconds;
+ int maxIdleSeconds;
@Option(names = { "--reload", "--dev" },
description = "Enables dev mode (live reload when source files are
updated and saved)")
- private boolean dev;
+ boolean dev;
@Option(names = { "--trace" }, description = "Enables trace logging of the
routed messages")
- private boolean trace;
+ boolean trace;
@Option(names = { "--properties" },
description = "Load properties file for route placeholders (ex.
/path/to/file.properties")
- private String propertiesFiles;
+ String propertiesFiles;
@Option(names = { "-p", "--prop", "--property" }, description =
"Additional properties (override existing)", arity = "0")
- private String[] property;
+ String[] property;
@Option(names = { "--file-lock" },
description = "Whether to create a temporary file lock, which upon
deleting triggers this process to terminate")
- private boolean fileLock;
+ boolean fileLock;
@Option(names = { "--jfr" },
description = "Enables Java Flight Recorder saving recording to
disk on exit")
- private boolean jfr;
+ boolean jfr;
@Option(names = { "--jfr-profile" },
description = "Java Flight Recorder profile to use (such as
default or profile)")
- private String jfrProfile;
+ String jfrProfile;
@Option(names = { "--local-kamelet-dir" },
description = "Local directory for loading Kamelets (takes
precedence)")
- private String localKameletDir;
+ String localKameletDir;
@Option(names = { "--port" }, description = "Embeds a local HTTP server on
this port")
- private int port;
+ int port;
@Option(names = { "--console" }, description = "Developer console at
/q/dev on local HTTP server (port 8080 by default)")
- private boolean console;
+ boolean console;
@Option(names = { "--health" }, description = "Health check at /q/health
on local HTTP server (port 8080 by default)")
- private boolean health;
+ boolean health;
@Option(names = { "--modeline" }, defaultValue = "true", description =
"Enables Camel-K style modeline")
- private boolean modeline = true;
+ boolean modeline = true;
@Option(names = { "--open-api" }, description = "Add an OpenAPI spec from
the given file")
- private String openapi;
+ String openapi;
+ //CHECKSTYLE:ON
@Override
public Integer call() throws Exception {
@@ -609,15 +609,15 @@ class Run implements Callable<Integer> {
}
private void configureLogging() throws Exception {
- if (silentRun || pipeRun) {
- RuntimeUtil.configureLog("off", false, false);
+ if (silentRun) {
+ RuntimeUtil.configureLog("off", false, false, false);
} else if (logging) {
- RuntimeUtil.configureLog(loggingLevel, loggingColor, loggingJson);
+ RuntimeUtil.configureLog(loggingLevel, loggingColor, loggingJson,
pipeRun);
writeSettings("loggingLevel", loggingLevel);
writeSettings("loggingColor", loggingColor ? "true" : "false");
writeSettings("loggingJson", loggingJson ? "true" : "false");
} else {
- RuntimeUtil.configureLog("off", false, false);
+ RuntimeUtil.configureLog("off", false, false, false);
writeSettings("loggingLevel", "off");
}
}
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
index 4203e9e7c3d..7425c3d25e5 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java
@@ -28,9 +28,11 @@ public final class RuntimeUtil {
private RuntimeUtil() {
}
- public static void configureLog(String level, boolean color, boolean json)
{
+ public static void configureLog(String level, boolean color, boolean json,
boolean pipe) {
if (INIT_DONE.compareAndSet(false, true)) {
- if (json) {
+ if (pipe) {
+ Configurator.initialize("CamelJBang",
"log4j2-pipe.properties");
+ } else if (json) {
Configurator.initialize("CamelJBang",
"log4j2-json.properties");
} else if (color) {
Configurator.initialize("CamelJBang", "log4j2.properties");
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-pipe.properties
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-pipe.properties
new file mode 100644
index 00000000000..53a57c61108
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/log4j2-pipe.properties
@@ -0,0 +1,28 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+appender.file.type = File
+appender.file.name = file
+appender.file.fileName = .camel-jbang/camel-pipe.log
+appender.file.layout.type = PatternLayout
+
+# logging style that is similar to spring boot (no color)
+appender.file.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %5p %pid ---
[%15.15t] %-40.40c : %m%n
+
+rootLogger.level = INFO
+rootLogger.appenderRef.file.ref = file
+