forked from devonfw/IDEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessResultImpl.java
More file actions
147 lines (118 loc) · 3.73 KB
/
ProcessResultImpl.java
File metadata and controls
147 lines (118 loc) · 3.73 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package com.devonfw.tools.ide.process;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import com.devonfw.tools.ide.cli.CliProcessException;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.log.IdeSubLogger;
/**
* Implementation of {@link ProcessResult}.
*/
public class ProcessResultImpl implements ProcessResult {
private final String executable;
private final String command;
private final int exitCode;
private final List<OutputMessage> outputMessages;
/**
* The constructor.
*
* @param executable the {@link #getExecutable() executable}.
* @param command the {@link #getCommand() command}.
* @param exitCode the {@link #getExitCode() exit code}.
* @param outputMessages {@link #getOutputMessages() output Messages}.
*/
public ProcessResultImpl(String executable, String command, int exitCode, List<OutputMessage> outputMessages) {
super();
this.executable = executable;
this.command = command;
this.exitCode = exitCode;
this.outputMessages = Objects.requireNonNullElse(outputMessages, Collections.emptyList());
}
@Override
public String getExecutable() {
return this.executable;
}
@Override
public String getCommand() {
return this.command;
}
@Override
public int getExitCode() {
return this.exitCode;
}
@Override
public String getSingleOutput(IdeSubLogger logger) throws IllegalStateException {
String errorMessage;
if (this.isSuccessful()) {
List<String> out = this.getOut();
int size = out.size();
if (size == 1) {
return out.getFirst();
} else if (size == 0) {
errorMessage = "No output received from " + this.getCommand();
} else {
StringBuilder sb = new StringBuilder();
sb.append("Expected single line of output but received ");
sb.append(size);
sb.append(" lines from ");
sb.append(this.getCommand());
sb.append(":");
for (String line : out) {
sb.append("\n");
sb.append(line);
}
errorMessage = sb.toString();
}
} else {
errorMessage = "Command " + this.getCommand() + " failed with exit code " + this.getExitCode();
}
if (logger == null) {
throw new IllegalStateException(errorMessage);
} else {
logger.log(errorMessage);
return null;
}
}
@Override
public List<String> getOut() {
return this.outputMessages.stream().filter(outputMessage -> !outputMessage.error()).map(OutputMessage::message).toList();
}
@Override
public List<String> getErr() {
return this.outputMessages.stream().filter(OutputMessage::error).map(OutputMessage::message).toList();
}
@Override
public List<OutputMessage> getOutputMessages() {
return this.outputMessages;
}
@Override
public void log(IdeLogLevel level, IdeContext context) {
log(level, context, level);
}
@Override
public void log(IdeLogLevel outLevel, IdeContext context, IdeLogLevel errorLevel) {
if (!this.outputMessages.isEmpty()) {
for (OutputMessage outputMessage : this.outputMessages) {
if (outputMessage.error()) {
doLog(errorLevel, outputMessage.message(), context);
} else {
doLog(outLevel, outputMessage.message(), context);
}
}
}
}
private void doLog(IdeLogLevel level, String message, IdeContext context) {
// remove !MESSAGE from log message
if (message.startsWith("!MESSAGE ")) {
message = message.substring(9);
}
context.level(level).log(message);
}
@Override
public void failOnError() throws CliProcessException {
if (!isSuccessful()) {
throw new CliProcessException(this);
}
}
}