-
Notifications
You must be signed in to change notification settings - Fork 2
/
Input.kt
282 lines (253 loc) · 9.63 KB
/
Input.kt
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package wemi
import org.jline.reader.EndOfFileException
import org.jline.reader.UserInterruptException
import org.slf4j.LoggerFactory
import wemi.boot.CLI
import wemi.boot.WemiBuildFolder
import wemi.boot.WemiRunningInInteractiveMode
import wemi.util.*
import wemi.util.CliStatusDisplay.Companion.withStatus
import java.nio.file.NoSuchFileException
import java.util.*
import java.util.regex.Pattern
private val LOG = LoggerFactory.getLogger("Input")
/**
* Read a [V] from the input.
* The value is first searched for using the [key] from explicit input pairs.
* Then, free input strings (without explicit [key]s) are considered. Both are considered from top
* (added last) to bottom, and only if they are accepted by the [validator].
* As a last resort, if interactive, user is asked. Otherwise, the query fails.
*
* @param key simple, non-user-readable key (case-insensitive, converted to lowercase)
* @param description displayed to the user, if asked interactively
* @param validator to use for validation and conversion of found string
* @param ask if value is not already specified, ask the user
* @return found value or null if validator fails on all possible values
*/
fun <V> CommandBindingHolder.read(key: String, description: String, validator: Validator<V>, ask:Boolean = true, acceptFree:Boolean = false): V? {
val input = this.input
// Search in prepared by key
for ((preparedKey, preparedValue) in input) {
if (!preparedKey.equals(key, ignoreCase = true)) {
continue
}
validator(preparedValue).use<Unit>({
SimpleHistory.getHistory(SimpleHistory.inputHistoryName(key)).add(preparedValue)
return it
}, {
LOG.info("Can't use '{}' for input key '{}': {}", preparedValue, key, it)
})
}
// Search in prepared for free
if (acceptFree) {
for ((preparedKey, preparedValue) in input) {
if (preparedKey.isNotEmpty()) {
continue
}
validator(preparedValue).use({
// We will use this free input
SimpleHistory.getHistory(SimpleHistory.inputHistoryName(key)).add(preparedValue)
SimpleHistory.getHistory(SimpleHistory.inputHistoryName(null)).add(preparedValue)
return it
}, {
LOG.info("Can't use free '{}' for input key '{}': {}", preparedValue, key, it)
})
}
}
if (!ask) {
return null
}
// Still no hit, read interactively
if (!WemiRunningInInteractiveMode) {
LOG.info("Not asking for {} - '{}', not interactive", key, description)
return null
}
try {
while (true) {
val line = CLI.InputLineReader.run {
val previousHistory = history
try {
history = SimpleHistory.getHistory(SimpleHistory.inputHistoryName(key))
CLI.MessageDisplay.withStatus(false) {
readLine("${format(description, format = Format.Bold)} (${format(key, Color.White)}): ")
}
} finally {
history = previousHistory
}
}
val value = validator(line)
value.use({
return it
}, {
print(format("Invalid input: ", format = Format.Bold))
println(format(it, foreground = Color.Red))
})
}
} catch (e: UserInterruptException) {
return null
} catch (e: EndOfFileException) {
return null
}
}
private val SECRET_KEY_VALUE_REGEX = Pattern.compile("^([a-zA-Z0-9-_.]+)[\\s]*:[\\s]*(.*?)[\\s]*\$")
/**
* Read a secret key. This is different from normal [read] in several ways:
* 1. Read token is not saved to autocomplete history and there is no autocomplete
* 2. Before asking, secret is first searched for in a "build/wemi-secrets.txt" file, which is an UTF-8 encoded file,
* where each line that matches regex ^([a-zA-Z0-9-_.]+)[\s]*:[\s]*(.*?)[\s]*$ is considered a key-value pair.
* For example, one such line may look like this:
* my-secret: 1234567890
* If you do have such file, remember to not push it to version control system!
* Keys taken from file are not case sensitive.
*/
fun <V> CommandBindingHolder.readSecret(key:String, description:String, validator:Validator<V>, acceptFree:Boolean = false):V? {
val input = this.input
// Search in prepared by key
for ((preparedKey, preparedValue) in input) {
if (!preparedKey.equals(key, ignoreCase = true)) {
continue
}
validator(preparedValue).use<Unit>({
return it
}, {
LOG.info("Can't use specified value for input key '{}': {}", key, it.replace(preparedValue, "<secret>"))
})
}
// Search in prepared for free
if (acceptFree) {
for ((preparedKey, preparedValue) in input) {
if (preparedKey.isNotEmpty()) {
continue
}
validator(preparedValue).use({
// We will use this free input
return it
}, {
LOG.info("Can't use specified free input value for input key '{}'", key)
})
}
}
// Read file
readSecretFromFile(key, validator)?.let { return it }
// Still no hit, read interactively
if (!WemiRunningInInteractiveMode) {
LOG.info("Not asking for secret {} - '{}', not interactive", key, description)
return null
}
try {
while (true) {
val line = CLI.InputLineReader.run {
val previousHistory = history
try {
history = SimpleHistory(null)
CLI.MessageDisplay.withStatus(false) {
readLine("${format(description, format = Format.Bold)} (${format(key, Color.White)}): ")
}
} finally {
history = previousHistory
}
}
val value = validator(line)
value.use({
return it
}, {
print(format("Invalid input: ", format = Format.Bold))
println(format(it, foreground = Color.Red))
})
}
} catch (e: UserInterruptException) {
return null
} catch (e: EndOfFileException) {
return null
}
}
fun <V> readSecretFromFile(key:String, validator:Validator<V>):V? {
// Read file
val secretsFile = WemiBuildFolder / "wemi-secrets.txt"
val secretFromFile = try {
Files.lines(secretsFile).map { line ->
val matcher = SECRET_KEY_VALUE_REGEX.matcher(line)
if (matcher.matches() && matcher.group(1).equals(key, ignoreCase = true)) {
matcher.group(2)
} else null
}.filter { it != null }.findFirst().orElse(null)
} catch (e:NoSuchFileException) {
LOG.info("Not reading secret {} from {} - file does not exist")
null
} catch (e:Exception) {
LOG.warn("Failed to read {} to obtain {}", secretsFile, key, e)
null
}
if (secretFromFile != null) {
validator(secretFromFile).use({ success ->
return success
}, {
LOG.warn("Can't use value of secret key {} from {}, because the value does not pass validation: {}", key, secretsFile, it.replace(secretFromFile, "<secret>"))
})
}
return null
}
/**
* A function that validates given string input and converts it to the desirable type.
* Should be pure. Returns [Failable], with the validated/converted value or with an error string,
* that will be printed for the user verbatim.
*/
typealias Validator<V> = (String) -> Failable<V, String>
/**
* Default validator, always succeeds and returns the entered string, no matter what it is.
*/
val StringValidator: Validator<String> = { Failable.success(it) }
/** Integer validator, accepts integer numbers in decimal system and MIN/MAX for Int limits */
@Suppress("unused")
val IntValidator: Validator<Int> = {
val trimmed = it.trim()
if (trimmed.equals("min", ignoreCase=true)) {
Failable.success(Int.MIN_VALUE)
} else if (trimmed.equals("max", ignoreCase=true)) {
Failable.success(Int.MAX_VALUE)
} else {
Failable.failNull(trimmed.toIntOrNull(), "Integer expected")
}
}
/**
* Boolean validator, treats true, yes, 1 and y as true, false, no, 0 and n as false.
*/
@Suppress("unused")
val BooleanValidator: Validator<Boolean> = {
when (it.toLowerCase(Locale.ROOT).trim()) {
"true", "yes", "1", "y", "on" ->
Failable.success(true)
"false", "no", "0", "n", "off" ->
Failable.success(false)
else ->
Failable.failure("Boolean expected")
}
}
/**
* String validator, succeeds only if string is a valid class name with package.
* (For example "wemi.Input")
*/
val ClassNameValidator: Validator<String> = validator@ {
val className = it.trim()
var firstLetter = true
for (c in className) {
if (firstLetter) {
if (!c.isJavaIdentifierStart()) {
return@validator Failable.failure("Invalid character '$c' - class name expected")
}
firstLetter = false
} else {
if (!c.isJavaIdentifierPart()) {
if (c == '.') {
firstLetter = true
} else {
return@validator Failable.failure("Invalid character '$c' - class name expected")
}
}
}
}
if (firstLetter) {
return@validator Failable.failure("Class name is incomplete")
}
Failable.success(className)
}