diff --git a/Partner-Core/src/main/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionService.java b/Partner-Core/src/main/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionService.java index 16b2dde6..ef3eb070 100644 --- a/Partner-Core/src/main/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionService.java +++ b/Partner-Core/src/main/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionService.java @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import java.io.File; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -37,8 +38,8 @@ public class CommandExecutionService { public Result exec(WrappedLaunchSpec launchSpec) { Result result = new Result(); - List output = new ArrayList<>(); - List error = new ArrayList<>(); + List output = Collections.synchronizedList(new ArrayList<>()); + List error = Collections.synchronizedList(new ArrayList<>()); try { Process process = startProcess(launchSpec); @@ -68,11 +69,18 @@ public class CommandExecutionService { stderrThread.join(); result.setOk(exitCode == 0); - result.setResultList(output.isEmpty() ? error : output); - result.setTotal(String.join("\n", output.isEmpty() ? error : output)); + List stdoutLines = List.copyOf(output); + List stderrLines = List.copyOf(error); + result.setStdoutLines(stdoutLines); + result.setStderrLines(stderrLines); + result.setResultList(stdoutLines.isEmpty() ? stderrLines : stdoutLines); + result.setTotal(buildDisplayText(stdoutLines, stderrLines)); } catch (Exception e) { result.setOk(false); result.setTotal(e.getMessage()); + result.setStdoutLines(List.of()); + result.setStderrLines(List.of(e.getMessage())); + result.setResultList(result.getStderrLines()); } return result; @@ -149,11 +157,23 @@ public class CommandExecutionService { ); } + private String buildDisplayText(List stdoutLines, List stderrLines) { + if (stdoutLines.isEmpty()) { + return String.join("\n", stderrLines); + } + if (stderrLines.isEmpty()) { + return String.join("\n", stdoutLines); + } + return String.join("\n", stdoutLines) + "\n" + String.join("\n", stderrLines); + } + @Data public static class Result { private boolean ok; private String total; private List resultList; + private List stdoutLines; + private List stderrLines; } @Data diff --git a/Partner-Core/src/test/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionServiceTest.java b/Partner-Core/src/test/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionServiceTest.java index d90bcf06..021617b0 100644 --- a/Partner-Core/src/test/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionServiceTest.java +++ b/Partner-Core/src/test/java/work/slhaf/partner/core/action/runner/execution/CommandExecutionServiceTest.java @@ -56,6 +56,8 @@ class CommandExecutionServiceTest { Assertions.assertTrue(result.isOk()); Assertions.assertEquals(List.of("hello", "world"), result.getResultList()); + Assertions.assertEquals(List.of("hello", "world"), result.getStdoutLines()); + Assertions.assertEquals(List.of(), result.getStderrLines()); Assertions.assertEquals("hello\nworld", result.getTotal()); } @@ -67,6 +69,8 @@ class CommandExecutionServiceTest { Assertions.assertTrue(result.isOk()); Assertions.assertEquals(List.of("ok"), result.getResultList()); + Assertions.assertEquals(List.of("ok"), result.getStdoutLines()); + Assertions.assertEquals(List.of(), result.getStderrLines()); Assertions.assertEquals("ok", result.getTotal()); } @@ -78,6 +82,8 @@ class CommandExecutionServiceTest { Assertions.assertFalse(result.isOk()); Assertions.assertEquals(List.of("fail"), result.getResultList()); + Assertions.assertEquals(List.of(), result.getStdoutLines()); + Assertions.assertEquals(List.of("fail"), result.getStderrLines()); Assertions.assertEquals("fail", result.getTotal()); } @@ -89,7 +95,9 @@ class CommandExecutionServiceTest { Assertions.assertTrue(result.isOk()); Assertions.assertEquals(List.of("out"), result.getResultList()); - Assertions.assertEquals("out", result.getTotal()); + Assertions.assertEquals(List.of("out"), result.getStdoutLines()); + Assertions.assertEquals(List.of("err"), result.getStderrLines()); + Assertions.assertEquals("out\nerr", result.getTotal()); } @Test