/
CommandArgumentDefinition.java
261 lines (219 loc) · 8.55 KB
/
CommandArgumentDefinition.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package liquibase.command;
import liquibase.Scope;
import liquibase.configuration.ConfigurationValueConverter;
import liquibase.configuration.ConfigurationValueObfuscator;
import liquibase.exception.CommandValidationException;
import liquibase.exception.MissingRequiredArgumentException;
import liquibase.integration.commandline.LiquibaseCommandLineConfiguration;
import liquibase.util.ObjectUtil;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* Defines a known, type-safe argument for a specific {@link CommandStep}.
* Includes metadata about the argument such as a description, if it is required, a default value, etc.
* <p>
* Because this definition is tied to a specific step, multiple steps in a pipeline can define arguments of the same name.
*
* @see CommandBuilder#argument(String, Class) for constructing new instances.
*/
public class CommandArgumentDefinition<DataType> implements Comparable<CommandArgumentDefinition<?>> {
private static final Pattern ALLOWED_ARGUMENT_PATTERN = Pattern.compile("[a-zA-Z0-9]+");
private final String name;
private final Class<DataType> dataType;
private String description;
private boolean required;
private boolean hidden;
private DataType defaultValue;
private String defaultValueDescription;
private ConfigurationValueConverter<DataType> valueConverter;
private ConfigurationValueObfuscator<DataType> valueObfuscator;
protected CommandArgumentDefinition(String name, Class<DataType> type) {
this.name = name;
this.dataType = type;
this.valueConverter = value -> ObjectUtil.convert(value, type);
}
/**
* The name of the argument. Must be camelCase alphanumeric.
*/
public String getName() {
return name;
}
/**
* The description of the argument. Used in generated help documentation.
*/
public String getDescription() {
return description;
}
/**
* The datatype this argument will return.
*/
public Class<DataType> getDataType() {
return dataType;
}
/**
* Whether this argument is required. Exposed as a separate setting for help doc purposes.
* {@link #validate(CommandScope)} will ensure required values are set.
*/
public boolean isRequired() {
return required;
}
/**
* Hidden arguments are ones that can be called via integrations, but should not be normally shown in help to users.
*/
public boolean getHidden() {
return hidden;
}
/**
* The default value to use for this argument
*/
public DataType getDefaultValue() {
return defaultValue;
}
/**
* A description of the default value. Defaults to {@link String#valueOf(Object)} of {@link #getDefaultValue()} but
* can be explicitly with {@link Building#defaultValue(Object, String)}.
*/
public String getDefaultValueDescription() {
return defaultValueDescription;
}
/**
* Function for converting values set in underlying {@link liquibase.configuration.ConfigurationValueProvider}s into the
* type needed for this command.
*/
public ConfigurationValueConverter<DataType> getValueConverter() {
return valueConverter;
}
/**
* Used when sending the value to user output to protect secure values.
*/
public ConfigurationValueObfuscator<DataType> getValueObfuscator() {
return valueObfuscator;
}
/**
* Validates that the value stored in the given {@link CommandScope} is valid.
*
* @throws CommandValidationException if the stored value is not valid.
*/
public void validate(CommandScope commandScope) throws CommandValidationException {
final DataType currentValue = commandScope.getArgumentValue(this);
if (this.isRequired() && currentValue == null) {
throw new CommandValidationException(LiquibaseCommandLineConfiguration.ARGUMENT_CONVERTER.getCurrentValue().convert(this.getName()), "missing required argument", new MissingRequiredArgumentException(this.getName()));
}
}
@Override
public int compareTo(CommandArgumentDefinition<?> o) {
return this.getName().compareTo(o.getName());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CommandArgumentDefinition that = (CommandArgumentDefinition) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public String toString() {
String returnString = getName();
if (required) {
returnString += " (required)";
}
return returnString;
}
/**
* A new {@link CommandArgumentDefinition} under construction from {@link CommandBuilder}
*/
public static class Building<DataType> {
private final String[][] commandNames;
private final CommandArgumentDefinition<DataType> newCommandArgument;
Building(String[][] commandNames, CommandArgumentDefinition<DataType> newCommandArgument) {
this.commandNames = commandNames;
this.newCommandArgument = newCommandArgument;
}
/**
* Mark argument as required.
* @see #optional()
*/
public Building<DataType> required() {
this.newCommandArgument.required = true;
return this;
}
/**
* Mark argument as optional.
* @see #required()
*/
public Building<DataType> optional() {
this.newCommandArgument.required = false;
return this;
}
/**
* Mark argument as hidden.
*/
public Building<DataType> hidden() {
this.newCommandArgument.hidden = true;
return this;
}
/**
* Add a description
*/
public Building<DataType> description(String description) {
this.newCommandArgument.description = description;
return this;
}
/**
* Set the default value for this argument as well as the description of the default value.
*/
public Building<DataType> defaultValue(DataType defaultValue, String description) {
newCommandArgument.defaultValue = defaultValue;
newCommandArgument.defaultValueDescription = description;
return this;
}
/**
* Convenience version of {@link #defaultValue(Object, String)} but using {@link String#valueOf(Object)} for the description.
*/
public Building<DataType> defaultValue(DataType defaultValue) {
String description = null;
if (defaultValue != null) {
description = String.valueOf(defaultValue);
}
return this.defaultValue(defaultValue, description);
}
/**
* Set the {@link #getValueConverter()} to use.
*/
public Building<DataType> setValueHandler(ConfigurationValueConverter<DataType> valueHandler) {
newCommandArgument.valueConverter = valueHandler;
return this;
}
/**
* Set the {@link #getValueObfuscator()} to use.
*/
public Building<DataType> setValueObfuscator(ConfigurationValueObfuscator<DataType> valueObfuscator) {
newCommandArgument.valueObfuscator = valueObfuscator;
return this;
}
/**
* Complete construction and register the definition with the rest of the system.
*
* @throws IllegalArgumentException is an invalid configuration was specified
*/
public CommandArgumentDefinition<DataType> build() throws IllegalArgumentException {
if (!ALLOWED_ARGUMENT_PATTERN.matcher(newCommandArgument.name).matches()) {
throw new IllegalArgumentException("Invalid argument format: " + newCommandArgument.name);
}
for (String[] commandName : commandNames) {
try {
Scope.getCurrentScope().getSingleton(CommandFactory.class).register(commandName, newCommandArgument);
} catch (IllegalArgumentException iae) {
Scope.getCurrentScope().getLog(CommandArgumentDefinition.class).warning(
"Unable to register command '" + commandName + "' argument '" + newCommandArgument.getName() + "': " + iae.getMessage());
throw iae;
}
}
return newCommandArgument;
}
}
}