-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathProcessExecutor.java
More file actions
66 lines (46 loc) · 1.74 KB
/
Copy pathProcessExecutor.java
File metadata and controls
66 lines (46 loc) · 1.74 KB
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
package com.rundeck.plugins.ansible.util;
import lombok.Builder;
import java.io.*;
import java.util.List;
import java.util.Map;
@Builder
public class ProcessExecutor {
private List<String> procArgs;
private File baseDirectory;
private Map<String, String> environmentVariables;
private List<String> stdinVariables;
private boolean redirectErrorStream;
public Process run() throws IOException {
ProcessBuilder processBuilder = new ProcessBuilder()
.command(procArgs)
.redirectErrorStream(redirectErrorStream);
if(baseDirectory!=null){
processBuilder.directory(baseDirectory);
}
if(environmentVariables!=null){
Map<String, String> processEnvironment = processBuilder.environment();
for (Map.Entry<String, String> entry : environmentVariables.entrySet()) {
processEnvironment.put(entry.getKey(), entry.getValue());
}
}
Process proc = processBuilder.start();
if (stdinVariables != null && !stdinVariables.isEmpty()){
OutputStream stdin = proc.getOutputStream();
OutputStreamWriter stdinw = new OutputStreamWriter(stdin);
BufferedWriter writer = new BufferedWriter(stdinw);
try {
for (String stdinVariable : stdinVariables) {
writer.write(stdinVariable);
writer.newLine();
writer.flush();
}
} catch (Exception e) {
System.err.println("error encryptFileAnsibleVault file " + e.getMessage());
}
writer.close();
stdinw.close();
stdin.close();
}
return proc;
}
}