|
8 | 8 | import java.io.PrintStream; |
9 | 9 | import java.nio.file.Files; |
10 | 10 | import java.nio.file.Path; |
| 11 | +import java.time.Duration; |
11 | 12 | import java.util.ArrayList; |
12 | 13 | import java.util.Arrays; |
13 | 14 | import java.util.Collection; |
@@ -153,23 +154,39 @@ public static void preserveLocalRepoSettings(Collection<String> args) { |
153 | 154 | public static Result executeArbitraryCommand(Path startingDir, String... args) { |
154 | 155 | System.out.println("$ " + String.join(" ", args)); |
155 | 156 |
|
156 | | - StringBuilder errorBuilder = new StringBuilder(); |
157 | 157 | Result result = new Result(); |
158 | 158 |
|
159 | | - String output = io.smallrye.common.process.ProcessBuilder.newBuilder(Path.of(args[0])) |
160 | | - .arguments(Arrays.copyOfRange(args, 1, args.length)) |
161 | | - .exitCodeChecker(ec -> { |
162 | | - result.exitCode = ec; |
163 | | - return true; |
164 | | - }) |
165 | | - .directory(startingDir) |
166 | | - .output().toSingleString(65536) |
167 | | - .error().consumeLinesWith(65536, line -> errorBuilder.append(line).append(System.lineSeparator())) |
168 | | - .run(); |
169 | | - String error = errorBuilder.toString(); |
170 | | - |
171 | | - result.stdout = output; |
172 | | - result.stderr = error; |
| 159 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 160 | + PrintStream outPs = new PrintStream(out); |
| 161 | + System.setOut(outPs); |
| 162 | + |
| 163 | + ByteArrayOutputStream err = new ByteArrayOutputStream(); |
| 164 | + PrintStream errPs = new PrintStream(err); |
| 165 | + System.setErr(errPs); |
| 166 | + |
| 167 | + try { |
| 168 | + io.smallrye.common.process.ProcessBuilder.newBuilder(Path.of(args[0])) |
| 169 | + .arguments(Arrays.copyOfRange(args, 1, args.length)) |
| 170 | + .exitCodeChecker(ec -> { |
| 171 | + result.exitCode = ec; |
| 172 | + return true; |
| 173 | + }) |
| 174 | + .directory(startingDir) |
| 175 | + // since there is no I/O, we need an explicit timeout |
| 176 | + .softExitTimeout(Duration.ofMinutes(5)) |
| 177 | + .hardExitTimeout(Duration.ofMinutes(5)) |
| 178 | + .output().inherited() |
| 179 | + .error().logOnSuccess(false).gatherOnFail(false).inherited() |
| 180 | + .run(); |
| 181 | + } finally { |
| 182 | + outPs.flush(); |
| 183 | + errPs.flush(); |
| 184 | + System.setOut(stdout); |
| 185 | + System.setErr(stderr); |
| 186 | + } |
| 187 | + |
| 188 | + result.stdout = out.toString(); |
| 189 | + result.stderr = err.toString(); |
173 | 190 | return result; |
174 | 191 | } |
175 | 192 |
|
|
0 commit comments