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/17 11:02:44 UTC

[camel] 01/02: CAMEL-18118: camel-jbang - Linux terminal scripting using pipes

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 bcf60eab8409ab70169e12fccfb9e97892469341
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue May 17 10:56:31 2022 +0200

    CAMEL-18118: camel-jbang - Linux terminal scripting using pipes
---
 .../dsl/jbang/core/commands/CamelJBangMain.java    | 17 +++++++++++
 .../apache/camel/dsl/jbang/core/commands/Pipe.java | 35 ++++++++++++++++++++++
 .../apache/camel/dsl/jbang/core/commands/Run.java  | 14 +++++++--
 3 files changed, 64 insertions(+), 2 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 3d6896eb5fb..4df7651cfe6 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
@@ -54,6 +54,23 @@ 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
new file mode 100644
index 00000000000..3051ca18158
--- /dev/null
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Pipe.java
@@ -0,0 +1,35 @@
+/*
+ * 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 picocli.CommandLine;
+
+import java.util.concurrent.Callable;
+
+@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
+    String file;
+
+    @Override
+    public Integer call() throws Exception {
+        Run run = new Run();
+        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 4355f68f809..b6a1affaf49 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
@@ -83,6 +83,7 @@ class Run implements Callable<Integer> {
     private File lockFile;
     private ScheduledExecutorService executor;
     private boolean silentRun;
+    private boolean pipeRun;
 
     @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")
@@ -186,6 +187,12 @@ class Run implements Callable<Integer> {
         return run();
     }
 
+    protected Integer runPipe(String file) throws Exception {
+        this.files = new String[]{file};
+        pipeRun = true;
+        return run();
+    }
+
     private void writeSetting(KameletMain main, Properties existing, String key, String value) {
         String val = existing != null ? existing.getProperty(key, value) : value;
         if (val != null) {
@@ -278,7 +285,7 @@ class Run implements Callable<Integer> {
             } else if (!silentRun && !source.exists()) {
                 System.out.println("Cannot run because application.properties file does not exist");
                 return 1;
-            } else {
+            } else if (silentRun) {
                 // silent-run then auto-detect all files
                 files = new File(".").list();
             }
@@ -332,6 +339,9 @@ class Run implements Callable<Integer> {
             // do not run for very long in silent run
             main.addInitialProperty("camel.main.autoStartup", "false");
             main.addInitialProperty("camel.main.durationMaxSeconds", "1");
+        } else if (pipeRun) {
+            // auto terminate if being idle
+            main.addInitialProperty("camel.main.durationMaxIdleSeconds", "1");
         }
         writeSetting(main, applicationProperties, "camel.main.durationMaxMessages",
                 () -> maxMessages > 0 ? String.valueOf(maxMessages) : null);
@@ -599,7 +609,7 @@ class Run implements Callable<Integer> {
     }
 
     private void configureLogging() throws Exception {
-        if (silentRun) {
+        if (silentRun || pipeRun) {
             RuntimeUtil.configureLog("off", false, false);
         } else if (logging) {
             RuntimeUtil.configureLog(loggingLevel, loggingColor, loggingJson);