From 00384eef820a257e293feeb188a501025bdab7df Mon Sep 17 00:00:00 2001 From: "yanng@vidconnect.cyou" Date: Sun, 16 Nov 2025 14:46:42 +0800 Subject: [PATCH] 1.1.0 Allow the output to be splitted into an array of strings --- jbang/Splicer.java | 80 ---------- pom.xml | 4 +- .../java/cyou/vidconnect/splicer/Main.java | 77 ---------- .../java/cyou/vidconnect/splicer/Splicer.java | 139 ++++++++++++++++++ 4 files changed, 141 insertions(+), 159 deletions(-) delete mode 100644 jbang/Splicer.java delete mode 100644 src/main/java/cyou/vidconnect/splicer/Main.java create mode 100644 src/main/java/cyou/vidconnect/splicer/Splicer.java diff --git a/jbang/Splicer.java b/jbang/Splicer.java deleted file mode 100644 index cdc292c..0000000 --- a/jbang/Splicer.java +++ /dev/null @@ -1,80 +0,0 @@ -///usr/bin/env jbang "$0" "$@" ; exit $? - -//DEPS com.fasterxml.jackson.core:jackson-databind:2.18.4 -//DEPS org.apache.commons:commons-exec:1.5.0 - -import java.io.ByteArrayOutputStream; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -import org.apache.commons.exec.CommandLine; -import org.apache.commons.exec.DefaultExecutor; -import org.apache.commons.exec.PumpStreamHandler; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class Splicer { - - private static final Logger logger = Logger.getLogger(Splicer.class.getName()); - - public static void main(String[] args) throws InterruptedException, JsonProcessingException { - - if (args.length == 0) { - System.out.println("This program allows you to run several commands at the same time." - + "\n\nUsage: Splicer.java cmd1 cmd2 cmd3 ..." - + "\n\nExample: ./Splicer.java \"ls -l\" \"df -kh\"\n"); - return; - } - - ExecutorService executorService = Executors.newFixedThreadPool(args.length); - - List> callables = List.of(args).stream() - .map(command -> new Callable() { - @Override - public ExecutionResult call() throws Exception { - return executeShell(command); - } - }).collect(Collectors.toList()); - - List results = executorService.invokeAll(callables).parallelStream().map(f -> { - try { - return f.get(); - } catch (InterruptedException | ExecutionException e) { - logger.log(Level.SEVERE, "Error found!", e); - } - return null; - }).filter(Objects::nonNull).collect(Collectors.toList()); - - executorService.shutdown(); - - System.out.println(new ObjectMapper().writeValueAsString(Map.of("results", results))); - } - - public static ExecutionResult executeShell(String command) { - ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); - try { - CommandLine cmdLine = CommandLine.parse(command.trim()); - DefaultExecutor executor = DefaultExecutor.builder().get(); - PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); - executor.setStreamHandler(streamHandler); - int exitValue = executor.execute(cmdLine); - return new ExecutionResult(command, exitValue, new String(tempOut.toByteArray())); - } catch (Exception e) { - logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e); - } - return new ExecutionResult(command, -1, new String(tempOut.toByteArray())); - } - - public static record ExecutionResult(String command, Integer exitValue, String output) { - - } -} diff --git a/pom.xml b/pom.xml index 9b3198d..740b0e6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 cyou.vidconnect splicer - 1.0.3 + 1.1.0 splicer Run multiple shell commands concurrently @@ -62,7 +62,7 @@ - cyou.vidconnect.splicer.Main + cyou.vidconnect.splicer.Splicer diff --git a/src/main/java/cyou/vidconnect/splicer/Main.java b/src/main/java/cyou/vidconnect/splicer/Main.java deleted file mode 100644 index 6056f78..0000000 --- a/src/main/java/cyou/vidconnect/splicer/Main.java +++ /dev/null @@ -1,77 +0,0 @@ -package cyou.vidconnect.splicer; - -import java.io.ByteArrayOutputStream; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.logging.Level; -import java.util.logging.Logger; -import java.util.stream.Collectors; - -import org.apache.commons.exec.CommandLine; -import org.apache.commons.exec.DefaultExecutor; -import org.apache.commons.exec.PumpStreamHandler; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -public class Main { - - private static final Logger logger = Logger.getLogger(Main.class.getName()); - - public static void main(String[] args) throws InterruptedException, JsonProcessingException { - - if (args.length == 0) { - System.out.println("This program allows you to run several commands at the same time." - + "\n\nUsage: java -jar splicer.jar cmd1 cmd2 cmd3 ..." - + "\n\nExample: java -jar splicer.jar \"ls -l\" \"df -kh\"\n"); - return; - } - - ExecutorService executorService = Executors.newFixedThreadPool(args.length); - - List> callables = List.of(args).stream() - .map(command -> new Callable() { - @Override - public ExecutionResult call() throws Exception { - return executeShell(command); - } - }).collect(Collectors.toList()); - - List results = executorService.invokeAll(callables).parallelStream().map(f -> { - try { - return f.get(); - } catch (InterruptedException | ExecutionException e) { - logger.log(Level.SEVERE, "Error found!", e); - } - return null; - }).filter(Objects::nonNull).collect(Collectors.toList()); - - executorService.shutdown(); - - System.out.println(new ObjectMapper().writeValueAsString(Map.of("results", results))); - } - - public static ExecutionResult executeShell(String command) { - ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); - try { - CommandLine cmdLine = CommandLine.parse(command.trim()); - DefaultExecutor executor = DefaultExecutor.builder().get(); - PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); - executor.setStreamHandler(streamHandler); - int exitValue = executor.execute(cmdLine); - return new ExecutionResult(command, exitValue, new String(tempOut.toByteArray())); - } catch (Exception e) { - logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e); - } - return new ExecutionResult(command, -1, new String(tempOut.toByteArray())); - } - - public static record ExecutionResult(String command, Integer exitValue, String output) { - - } -} diff --git a/src/main/java/cyou/vidconnect/splicer/Splicer.java b/src/main/java/cyou/vidconnect/splicer/Splicer.java new file mode 100644 index 0000000..1d8d2f9 --- /dev/null +++ b/src/main/java/cyou/vidconnect/splicer/Splicer.java @@ -0,0 +1,139 @@ +///usr/bin/env jbang "$0" "$@" ; exit $? + +//DEPS com.fasterxml.jackson.core:jackson-databind:2.18.4 +//DEPS org.apache.commons:commons-exec:1.5.0 + +package cyou.vidconnect.splicer; + +import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Scanner; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +import org.apache.commons.exec.CommandLine; +import org.apache.commons.exec.DefaultExecutor; +import org.apache.commons.exec.PumpStreamHandler; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class Splicer { + + private static final Logger logger = Logger.getLogger(Splicer.class.getName()); + + public static void main(String[] args) throws InterruptedException, JsonProcessingException { + List commandList = new ArrayList<>(); + Scanner scanner = new Scanner(System.in); + while (scanner.hasNextLine()) { + String command = scanner.nextLine(); + if (command.isBlank()) { + break; + } + commandList.add(command); + } + scanner.close(); + if (commandList.isEmpty()) { + System.out.println("This program allows you to run several commands at the same time." + + "\n\nUsage: echo -e | java -jar splicer.jar (v1|v2|)" + + "\n\nExample: echo -e \"ls -l\\ndf -kh\" | java -jar splicer.jar v2 | jq ."); + return; + } + + if (args.length > 0 && args[0].equals("v2")) { + new Splicer().executeCommandsV2(commandList); + } else { + new Splicer().executeCommands(commandList); + } + } + + public void executeCommands(List commandList) throws InterruptedException, JsonProcessingException { + ExecutorService executorService = Executors.newFixedThreadPool(commandList.size()); + List> callables = commandList.stream().filter(command -> !command.isBlank()) + .map(command -> new Callable() { + @Override + public ExecutionResult call() throws Exception { + return executeShell(command); + } + }).collect(Collectors.toList()); + + List results = executorService.invokeAll(callables).parallelStream().map(f -> { + try { + return f.get(); + } catch (InterruptedException | ExecutionException e) { + logger.log(Level.SEVERE, "Error found!", e); + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toList()); + executorService.shutdown(); + System.out.println(new ObjectMapper().writeValueAsString(Map.of("results", results))); + } + + public void executeCommandsV2(List commandList) throws InterruptedException, JsonProcessingException { + ExecutorService executorService = Executors.newFixedThreadPool(commandList.size()); + List> callables = commandList.stream().filter(command -> !command.isBlank()) + .map(command -> new Callable() { + @Override + public ExecutionResultV2 call() throws Exception { + return executeShellV2(command); + } + }).collect(Collectors.toList()); + + List results = executorService.invokeAll(callables).parallelStream().map(f -> { + try { + return f.get(); + } catch (InterruptedException | ExecutionException e) { + logger.log(Level.SEVERE, "Error found!", e); + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toList()); + executorService.shutdown(); + System.out.println(new ObjectMapper().writeValueAsString(Map.of("results", results))); + } + + public static ExecutionResult executeShell(String command) { + ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); + try { + CommandLine cmdLine = CommandLine.parse(command.trim()); + DefaultExecutor executor = DefaultExecutor.builder().get(); + PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); + executor.setStreamHandler(streamHandler); + int exitValue = executor.execute(cmdLine); + return new ExecutionResult(command, exitValue, new String(tempOut.toByteArray())); + } catch (Exception e) { + logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e); + return new ExecutionResult(command, -1, e.getMessage()); + } + } + + public static ExecutionResultV2 executeShellV2(String command) { + ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); + try { + CommandLine cmdLine = CommandLine.parse(command.trim()); + DefaultExecutor executor = DefaultExecutor.builder().get(); + PumpStreamHandler streamHandler = new PumpStreamHandler(tempOut); + executor.setStreamHandler(streamHandler); + int exitValue = executor.execute(cmdLine); + return new ExecutionResultV2(command, exitValue, List.of(new String(tempOut.toByteArray()).split("\n"))); + } catch (Exception e) { + logger.log(Level.SEVERE, "Execute cmd failed: " + command.trim(), e); + return new ExecutionResultV2(command, -1, List.of(e.getMessage().split("\n"))); + } + } + + public static record ExecutionResult(String command, Integer exitValue, String output) { + + } + + public static record ExecutionResultV2(String command, Integer exitValue, List output) { + + } +}