parent
530909e817
commit
00384eef82
@ -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<Callable<ExecutionResult>> callables = List.of(args).stream()
|
||||
.map(command -> new Callable<ExecutionResult>() {
|
||||
@Override
|
||||
public ExecutionResult call() throws Exception {
|
||||
return executeShell(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResult> 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) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -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<Callable<ExecutionResult>> callables = List.of(args).stream()
|
||||
.map(command -> new Callable<ExecutionResult>() {
|
||||
@Override
|
||||
public ExecutionResult call() throws Exception {
|
||||
return executeShell(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResult> 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) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -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<String> 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 <cmds separated by newline> | 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<String> commandList) throws InterruptedException, JsonProcessingException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(commandList.size());
|
||||
List<Callable<ExecutionResult>> callables = commandList.stream().filter(command -> !command.isBlank())
|
||||
.map(command -> new Callable<ExecutionResult>() {
|
||||
@Override
|
||||
public ExecutionResult call() throws Exception {
|
||||
return executeShell(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResult> 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<String> commandList) throws InterruptedException, JsonProcessingException {
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(commandList.size());
|
||||
List<Callable<ExecutionResultV2>> callables = commandList.stream().filter(command -> !command.isBlank())
|
||||
.map(command -> new Callable<ExecutionResultV2>() {
|
||||
@Override
|
||||
public ExecutionResultV2 call() throws Exception {
|
||||
return executeShellV2(command);
|
||||
}
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<ExecutionResultV2> 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<String> output) {
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue