package com.perforce.hws.core.util;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* Created by tjuricek on 2/4/16.
*/
public interface ProcessHelpers extends StreamUtils {
default String execOutOrFail(String... command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder();
pb.redirectErrorStream();
pb.command(command);
Process p = pb.start();
StreamReaderThread stdout = new StreamReaderThread(p.getInputStream());
stdout.start();
p.waitFor();
stdout.join();
if (p.exitValue() == 0) {
return stdout.getStringBuffer().toString();
} else {
String cmd = Arrays.stream(command).collect(Collectors.joining(" "));
throw new IllegalStateException(cmd + " failed with code " +
p.exitValue() + "\n" + stdout.getStringBuffer().toString());
}
}
default String execOutOrFailInDir(String cwd, String... command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder();
pb.redirectErrorStream();
pb.command(command);
pb.directory(new File(cwd));
Process p = pb.start();
StreamReaderThread stdout = new StreamReaderThread(p.getInputStream());
stdout.start();
p.waitFor();
stdout.join();
if (p.exitValue() == 0) {
return stdout.getStringBuffer().toString();
} else {
String cmd = Arrays.stream(command).collect(Collectors.joining(" "));
throw new IllegalStateException(cmd + " failed with code " +
p.exitValue() + "\n" + stdout.getStringBuffer().toString());
}
}
default void execOrFail(String... command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder();
pb.command(command);
pb.redirectErrorStream(true);
Process p = pb.start();
StreamReaderThread stdout = new StreamReaderThread(p.getInputStream());
stdout.start();
p.waitFor();
stdout.join();
if (p.exitValue() != 0) {
String cmd = Arrays.stream(command).collect(Collectors.joining(" "));
throw new IllegalStateException(cmd + " failed with code " +
p.exitValue() + "\n" + stdout.getStringBuffer().toString());
}
}
}
# |
Change |
User |
Description |
Committed |
|
#1
|
20138 |
Paul Allen |
Refactor module :api to :core |
|
|
//guest/paul_allen/hws/dev/api/src/main/java/com/perforce/hws/api/util/ProcessHelpers.java |
#1
|
19986 |
Paul Allen |
Fix imports and package structure. |
|
|