-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ConsoleUIService.java
244 lines (209 loc) · 8.01 KB
/
ConsoleUIService.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package liquibase.ui;
import liquibase.AbstractExtensibleObject;
import liquibase.GlobalConfiguration;
import liquibase.Scope;
import liquibase.configuration.ConfiguredValue;
import liquibase.logging.Logger;
import liquibase.util.StringUtil;
import java.io.*;
/**
* {@link UIService} implementation that sends messages to stdout and stderr.
*/
public class ConsoleUIService extends AbstractExtensibleObject implements UIService {
private PrintStream outputStream = System.out;
private PrintStream errorStream = System.out;
private boolean outputStackTraces = false;
private boolean allowPrompt = false;
private ConsoleWrapper console;
public static final String TERM_PROGRAM = "TERM_PROGRAM";
public static final String MINTTY = "mintty";
public ConsoleUIService() {
}
protected ConsoleUIService(ConsoleWrapper console) {
this.console = console;
}
/**
* Returns {@link liquibase.plugin.Plugin#PRIORITY_NOT_APPLICABLE} because it must be manually configured as needed
*/
@Override
public int getPriority() {
return PRIORITY_NOT_APPLICABLE;
}
@Override
public void sendMessage(String message) {
getOutputStream().println(message);
}
@Override
public void sendErrorMessage(String message) {
getErrorStream().println(message);
}
@Override
public void sendErrorMessage(String message, Throwable exception) {
sendErrorMessage(message);
if (getOutputStackTraces()) {
exception.printStackTrace(getErrorStream());
}
}
@Override
public void setAllowPrompt(boolean allowPrompt) throws IllegalArgumentException {
this.allowPrompt = allowPrompt;
}
@Override
public boolean getAllowPrompt() {
return getConsole().supportsInput() && allowPrompt;
}
@Override
public <T> T prompt(String prompt, T valueIfNoEntry, InputHandler<T> inputHandler, Class<T> type) {
//
// Check the allowPrompt flag
//
Logger log = Scope.getCurrentScope().getLog(getClass());
if (! allowPrompt) {
log.fine("No prompt for input is allowed at this time");
return valueIfNoEntry;
}
final ConsoleWrapper console = getConsole();
if (!console.supportsInput()) {
log.fine("No console attached. Skipping interactive prompt: '" + prompt + "'. Using default value '" + valueIfNoEntry + "'");
return valueIfNoEntry;
}
if (inputHandler == null) {
inputHandler = new DefaultInputHandler<>();
}
String initialMessage = prompt;
if (valueIfNoEntry != null) {
initialMessage += " [" + valueIfNoEntry + "]";
}
this.sendMessage(initialMessage + ": ");
while (true) {
String input = StringUtil.trimToNull(console.readLine());
try {
if (input == null) {
if (inputHandler.shouldAllowEmptyInput()) {
return valueIfNoEntry;
} else {
throw new IllegalArgumentException("Empty values are not permitted.");
}
}
return inputHandler.parseInput(input, type);
} catch (IllegalArgumentException e) {
String message;
if (e.getMessage() != null) {
message = e.getMessage();
} else {
message = "Invalid value: \"" + input + "\"";
}
this.sendMessage(message);
this.sendMessage(initialMessage + ": ");
}
}
}
/**
* Creates the {@link ConsoleWrapper} to use.
*/
protected ConsoleWrapper getConsole() {
if (console == null) {
final ConfiguredValue<Boolean> headlessValue = GlobalConfiguration.HEADLESS.getCurrentConfiguredValue();
boolean headlessConfigValue = headlessValue.getValue();
boolean wasHeadlessOverridden = !headlessValue.wasDefaultValueUsed();
final Logger log = Scope.getCurrentScope().getLog(getClass());
if (headlessConfigValue) {
log.fine("Not prompting for user input because liquibase.headless=true. Set to 'false' in liquibase.properties to change this behavior.");
console = new ConsoleWrapper(null, false);
} else {
//
// If no system console and headless was not overridden as headless=false
// then check the TERM_PROGRAM environment variable setting
// to detect whether we need to read from stdin
//
final Console systemConsole = System.console();
boolean useStdIn = wasHeadlessOverridden;
String minTtyValue = System.getenv(TERM_PROGRAM);
if (systemConsole == null && ! useStdIn && minTtyValue != null) {
useStdIn = minTtyValue.equalsIgnoreCase(MINTTY);
}
this.console = new ConsoleWrapper(systemConsole, useStdIn);
if (systemConsole == null) {
log.fine("No system console detected for user input");
if (useStdIn) {
log.fine("Input will be from stdin");
}
} else {
log.fine("A system console was detected for user input");
}
}
if (!wasHeadlessOverridden) {
log.fine("To override or validate the auto-detected environment for user input, set the liquibase.headless property in liquibase.properties file.");
}
}
return console;
}
@SuppressWarnings("WeakerAccess")
public PrintStream getOutputStream() {
return outputStream;
}
@SuppressWarnings("unused")
public void setOutputStream(PrintStream outputStream) {
this.outputStream = outputStream;
}
@SuppressWarnings("WeakerAccess")
public PrintStream getErrorStream() {
return errorStream;
}
@SuppressWarnings("unused")
public void setErrorStream(PrintStream errorStream) {
this.errorStream = errorStream;
}
@SuppressWarnings("WeakerAccess")
public boolean getOutputStackTraces() {
return outputStackTraces;
}
/**
* Set to true to output stacktraces. Defaults to not outputing them.
*/
@SuppressWarnings("unused")
public void setOutputStackTraces(boolean outputStackTraces) {
this.outputStackTraces = outputStackTraces;
}
/**
* Wrapper around {@link Console} to allow replacements as needed.
* If the passed {@link Console} is null, {@link #supportsInput()} will return false, and {@link #readLine()} will return null.
*/
public static class ConsoleWrapper {
private final Console console;
private final boolean useStdin;
/**
*
* This constructor is used for extensions that provide UIService implementations
*
* @param console the console to use
*
*/
public ConsoleWrapper(Console console) {
this(console, false);
}
public ConsoleWrapper(Console console, boolean useStdInParam) {
this.console = console;
this.useStdin = useStdInParam;
}
public String readLine() {
if (console == null) {
if (! useStdin) {
return "";
}
try {
return new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException ioe) {
//
// Throw an exception if we can't read
//
throw new RuntimeException("Unable to read from the system input stream", ioe);
}
}
return console.readLine();
}
public boolean supportsInput() {
return console != null || useStdin;
}
}
}