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 2023/11/28 14:45:28 UTC

(camel) branch tm4 created (now f3023e711e5)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch tm4
in repository https://gitbox.apache.org/repos/asf/camel.git


      at f3023e711e5 camel-jbang - Add --output option to debug command.

This branch includes the following new commits:

     new f3023e711e5 camel-jbang - Add --output option to debug command.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(camel) 01/01: camel-jbang - Add --output option to debug command.

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch tm4
in repository https://gitbox.apache.org/repos/asf/camel.git

commit f3023e711e5c3763d6e6338a64fab01d9aaa6946
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 28 15:45:17 2023 +0100

    camel-jbang - Add --output option to debug command.
---
 .../camel/dsl/jbang/core/commands/Debug.java       | 26 ++++++++++++
 .../core/commands/action/MessageTableHelper.java   | 47 +--------------------
 .../dsl/jbang/core/common/CamelCommandHelper.java  | 48 ++++++++++++++++++++++
 3 files changed, 76 insertions(+), 45 deletions(-)

diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
index ad7ccb612e9..3117e2e909f 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Debug.java
@@ -20,6 +20,7 @@ import java.io.BufferedReader;
 import java.io.Console;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.StringWriter;
@@ -31,6 +32,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.camel.dsl.jbang.core.commands.action.MessageTableHelper;
+import org.apache.camel.dsl.jbang.core.common.CamelCommandHelper;
 import org.apache.camel.main.KameletMain;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
@@ -56,6 +58,10 @@ public class Debug extends Run {
                         description = "To set breakpoint at the given node id (Multiple ids can be separated by comma). If no breakpoint is set, then the first route is automatic selected.")
     String breakpoint;
 
+    @CommandLine.Option(names = { "--output" },
+                        description = "File to store the current message body (will override). This allows for manual inspecting the message later.")
+    String output;
+
     @CommandLine.Option(names = { "--stop-on-exit" }, defaultValue = "true",
                         description = "Whether to stop the running Camel on exit")
     boolean stopOnExit = true;
@@ -431,6 +437,26 @@ public class Debug extends Run {
                 this.waitForUser.set(true);
             }
             if (this.waitForUser.get()) {
+                // save current message to file
+                if (output != null && this.suspendedRow != null) {
+                    JsonObject j = this.suspendedRow.message;
+                    if (j != null) {
+                        j = j.getMap("body");
+                        if (j != null) {
+                            String b = j.getString("value");
+                            if (b != null) {
+                                b = CamelCommandHelper.valueAsStringPretty(b, false);
+                                try {
+                                    File f = new File(output);
+                                    IOHelper.writeText(b, f);
+                                } catch (IOException e) {
+                                    // ignore
+                                }
+                            }
+                        }
+                    }
+                }
+
                 String msg = "    Breakpoint suspended. Press ENTER to continue.";
                 if (loggingColor) {
                     AnsiConsole.out().println(Ansi.ansi().a(Ansi.Attribute.INTENSITY_BOLD).a(msg).reset());
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/MessageTableHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/MessageTableHelper.java
index 6c927821e6e..bf2b88dfcc6 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/MessageTableHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/MessageTableHelper.java
@@ -24,8 +24,7 @@ import com.github.freva.asciitable.AsciiTable;
 import com.github.freva.asciitable.Column;
 import com.github.freva.asciitable.HorizontalAlign;
 import com.github.freva.asciitable.OverflowBehaviour;
-import org.apache.camel.dsl.jbang.core.common.JSonHelper;
-import org.apache.camel.dsl.jbang.core.common.XmlHelper;
+import org.apache.camel.dsl.jbang.core.common.CamelCommandHelper;
 import org.apache.camel.util.json.JsonArray;
 import org.apache.camel.util.json.JsonObject;
 import org.apache.camel.util.json.Jsoner;
@@ -260,49 +259,7 @@ public class MessageTableHelper {
         }
 
         String valueAsStringPretty() {
-            if (value == null) {
-                return "null";
-            }
-            boolean json = false;
-            String s = value.toString();
-            if (!s.isEmpty()) {
-                try {
-                    s = Jsoner.unescape(s);
-                    if (loggingColor) {
-                        s = JSonHelper.colorPrint(s, 2, true);
-                    } else {
-                        s = JSonHelper.prettyPrint(s, 2);
-                    }
-                    if (s != null && !s.isEmpty()) {
-                        json = true;
-                    }
-                } catch (Exception e) {
-                    // ignore as not json
-                }
-                if (s == null || s.isEmpty()) {
-                    s = value.toString();
-                }
-                if (!json) {
-                    // try with xml
-                    try {
-                        s = Jsoner.unescape(s);
-                        if (loggingColor) {
-                            s = XmlHelper.colorPrint(s, 2, true);
-                        } else {
-                            s = XmlHelper.prettyPrint(s, 2);
-                        }
-                    } catch (Exception e) {
-                        // ignore as not xml
-                    }
-                }
-                if (s == null || s.isEmpty()) {
-                    s = value.toString();
-                }
-            }
-            if (s == null) {
-                return "null";
-            }
-            return s;
+            return CamelCommandHelper.valueAsStringPretty(value, loggingColor);
         }
 
         String valueAsStringRed() {
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CamelCommandHelper.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CamelCommandHelper.java
index 46eb35423b4..26ba2d85884 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CamelCommandHelper.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/CamelCommandHelper.java
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.dsl.jbang.core.common;
 
+import org.apache.camel.util.json.Jsoner;
+
 public final class CamelCommandHelper {
 
     private CamelCommandHelper() {
@@ -39,4 +41,50 @@ public final class CamelCommandHelper {
         }
     }
 
+    public static String valueAsStringPretty(Object value, boolean loggingColor) {
+        if (value == null) {
+            return "null";
+        }
+        boolean json = false;
+        String s = value.toString();
+        if (!s.isEmpty()) {
+            try {
+                s = Jsoner.unescape(s);
+                if (loggingColor) {
+                    s = JSonHelper.colorPrint(s, 2, true);
+                } else {
+                    s = JSonHelper.prettyPrint(s, 2);
+                }
+                if (s != null && !s.isEmpty()) {
+                    json = true;
+                }
+            } catch (Exception e) {
+                // ignore as not json
+            }
+            if (s == null || s.isEmpty()) {
+                s = value.toString();
+            }
+            if (!json) {
+                // try with xml
+                try {
+                    s = Jsoner.unescape(s);
+                    if (loggingColor) {
+                        s = XmlHelper.colorPrint(s, 2, true);
+                    } else {
+                        s = XmlHelper.prettyPrint(s, 2);
+                    }
+                } catch (Exception e) {
+                    // ignore as not xml
+                }
+            }
+            if (s == null || s.isEmpty()) {
+                s = value.toString();
+            }
+        }
+        if (s == null) {
+            return "null";
+        }
+        return s;
+    }
+
 }