-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultCommandRunner.java
104 lines (86 loc) · 3.11 KB
/
DefaultCommandRunner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.github.swissquote.carnotzet.core.runtime;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import com.github.swissquote.carnotzet.core.CarnotzetDefinitionException;
import com.google.common.base.Joiner;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
public final class DefaultCommandRunner implements CommandRunner {
public static final DefaultCommandRunner INSTANCE = new DefaultCommandRunner();
private DefaultCommandRunner() {
}
public int runCommand(String... command) {
return runCommand(new File("/"), command);
}
public int runCommand(Boolean inheritIo, String... command) {
return runCommand(inheritIo, new File("/"), command);
}
public int runCommand(File directoryForRunning, String... command) {
return runCommand(true, directoryForRunning, command);
}
public int runCommand(Boolean inheritIo, File directoryForRunning, String... command) {
log.debug("Running command [{}]", Joiner.on(" ").join(command));
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(directoryForRunning);
if (inheritIo) {
pb.inheritIO();
} else {
File sink = getOsSpecificSink();
pb.redirectError(sink);
pb.redirectOutput(sink);
}
try {
Process p = pb.start();
p.waitFor();
log.debug("Command completed : [{}]", Joiner.on(" ").join(command));
return p.exitValue();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CarnotzetDefinitionException(e);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private File getOsSpecificSink() {
if (SystemUtils.IS_OS_WINDOWS) {
return new File("NUL");
}
return new File("/dev/null");
}
public String runCommandAndCaptureOutput(String... command) {
return runCommandAndCaptureOutput(new File("/"), command);
}
public String runCommandAndCaptureOutput(File directoryForRunning, String... command) {
log.debug("Running command [{}]", Joiner.on(" ").join(command));
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(directoryForRunning);
try {
// we use a temp file to avoid having to manage a thread to read the process output.
// reading the output while the process is running is mandatory in cases where
// it outputs more than 4k of data (OS buffer size for pipes). Otherwise the process will freeze.
// http://java-monitor.com/forum/showthread.php?t=4067
final File tmp = File.createTempFile("carnotzet-cmd-out", null);
tmp.deleteOnExit();
pb.redirectErrorStream(true).redirectOutput(tmp);
Process p = pb.start();
p.waitFor();
String output = FileUtils.readFileToString(tmp);
output = output.trim();
if (p.exitValue() != 0) {
throw new RuntimeException("External command [" + Joiner.on(" ").join(command) + "] exited with [" + p.exitValue()
+ "], output: " + output);
}
return output;
}
catch (InterruptedException | IOException e) {
throw new RuntimeException(e);
}
}
}