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

gfournier 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 a2eff029e3c CAMEL-21563 : camel-bbang ps option output in JSON
a2eff029e3c is described below

commit a2eff029e3c263e941843e450865c187d0d61af3
Author: Gaelle Fournier <[email protected]>
AuthorDate: Fri Dec 20 16:54:49 2024 +0100

    CAMEL-21563 : camel-bbang ps option output in JSON
---
 .../jbang/core/commands/process/ListProcess.java   | 64 ++++++++++++++++------
 1 file changed, 46 insertions(+), 18 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
index db486d1ea33..0218c3a494f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/ListProcess.java
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import com.github.freva.asciitable.AsciiTable;
 import com.github.freva.asciitable.Column;
@@ -30,6 +31,7 @@ import 
org.apache.camel.dsl.jbang.core.common.PidNameAgeCompletionCandidates;
 import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
 import org.apache.camel.util.TimeUtils;
 import org.apache.camel.util.json.JsonObject;
+import org.apache.camel.util.json.Jsoner;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
 
@@ -46,6 +48,10 @@ public class ListProcess extends ProcessWatchCommand {
                         description = "List only pid in the output")
     boolean pid;
 
+    @CommandLine.Option(names = { "--json" },
+                        description = "Output in JSON Format")
+    boolean jsonOutput;
+
     public ListProcess(CamelJBangMain main) {
         super(main);
     }
@@ -112,27 +118,49 @@ public class ListProcess extends ProcessWatchCommand {
         // sort rows
         rows.sort(this::sortRow);
 
+        // print rows
         if (!rows.isEmpty()) {
             if (pid) {
-                rows.forEach(r -> printer().println(r.pid));
+                if (jsonOutput) {
+                    printer().println(
+                            Jsoner.serialize(
+                                    rows.stream().map(row -> 
row.pid).collect(Collectors.toList())));
+                } else {
+                    rows.forEach(r -> printer().println(r.pid));
+                }
             } else {
-                printer().println(AsciiTable.getTable(AsciiTable.NO_BORDERS, 
rows, Arrays.asList(
-                        new 
Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(r -> r.pid),
-                        new 
Column().header("NAME").dataAlign(HorizontalAlign.LEFT)
-                                .maxWidth(40, OverflowBehaviour.ELLIPSIS_RIGHT)
-                                .with(r -> r.name),
-                        new 
Column().header("READY").dataAlign(HorizontalAlign.CENTER).with(r -> r.ready),
-                        new 
Column().header("STATUS").headerAlign(HorizontalAlign.CENTER)
-                                .with(this::getStatus),
-                        new 
Column().header("AGE").headerAlign(HorizontalAlign.CENTER).with(r -> r.ago),
-                        new Column().header("TOTAL").with(this::getTotal),
-                        new 
Column().header("REMOTE").with(this::getTotalRemote),
-                        new Column().header("FAIL").with(this::getFailed),
-                        new 
Column().header("INFLIGHT").with(this::getInflight),
-                        new Column().header("") // empty header as we only 
show info when there is an error
-                                
.headerAlign(HorizontalAlign.LEFT).dataAlign(HorizontalAlign.LEFT)
-                                .maxWidth(70, OverflowBehaviour.NEWLINE)
-                                .with(this::getDescription))));
+                if (jsonOutput) {
+                    printer().println(
+                            Jsoner.serialize(
+                                    rows.stream().map(row -> Map.of(
+                                            "pid", row.pid,
+                                            "name", row.name,
+                                            "ready", row.ready,
+                                            "status", getStatus(row),
+                                            "age", row.ago,
+                                            "total", getTotal(row),
+                                            "remote", getTotalRemote(row),
+                                            "fail", getFailed(row),
+                                            "inflight", 
getInflight(row))).collect(Collectors.toList())));
+                } else {
+                    
printer().println(AsciiTable.getTable(AsciiTable.NO_BORDERS, rows, 
Arrays.asList(
+                            new 
Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(r -> r.pid),
+                            new 
Column().header("NAME").dataAlign(HorizontalAlign.LEFT)
+                                    .maxWidth(40, 
OverflowBehaviour.ELLIPSIS_RIGHT)
+                                    .with(r -> r.name),
+                            new 
Column().header("READY").dataAlign(HorizontalAlign.CENTER).with(r -> r.ready),
+                            new 
Column().header("STATUS").headerAlign(HorizontalAlign.CENTER)
+                                    .with(this::getStatus),
+                            new 
Column().header("AGE").headerAlign(HorizontalAlign.CENTER).with(r -> r.ago),
+                            new Column().header("TOTAL").with(this::getTotal),
+                            new 
Column().header("REMOTE").with(this::getTotalRemote),
+                            new Column().header("FAIL").with(this::getFailed),
+                            new 
Column().header("INFLIGHT").with(this::getInflight),
+                            new Column().header("") // empty header as we only 
show info when there is an error
+                                    
.headerAlign(HorizontalAlign.LEFT).dataAlign(HorizontalAlign.LEFT)
+                                    .maxWidth(70, OverflowBehaviour.NEWLINE)
+                                    .with(this::getDescription))));
+                }
             }
         }
 

Reply via email to