/
CommandRunner.java
74 lines (63 loc) · 3.13 KB
/
CommandRunner.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
package liquibase.integration.commandline;
import liquibase.command.CommandResults;
import liquibase.command.CommandScope;
import liquibase.command.CommonArgumentNames;
import liquibase.exception.CommandValidationException;
import liquibase.exception.MissingRequiredArgumentException;
import liquibase.util.StringUtil;
import picocli.CommandLine;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class CommandRunner implements Callable<CommandResults> {
private CommandLine.Model.CommandSpec spec;
@Override
public CommandResults call() throws Exception {
List<String> command = new ArrayList<>();
command.add(spec.commandLine().getCommandName());
CommandLine parentCommand = spec.commandLine().getParent();
while (!parentCommand.getCommandName().equals("liquibase")) {
command.add(0, parentCommand.getCommandName());
parentCommand = parentCommand.getParent();
}
final String[] commandName = LiquibaseCommandLine.getCommandNames(spec.commandLine());
for (int i=0; i<commandName.length; i++) {
commandName[i] = StringUtil.toCamelCase(commandName[i]);
}
final CommandScope commandScope = new CommandScope(commandName);
final File outputFile = LiquibaseCommandLineConfiguration.OUTPUT_FILE.getCurrentValue();
OutputStream outputStream = null;
try {
if (outputFile != null) {
outputStream = new FileOutputStream(outputFile);
commandScope.setOutput(outputStream);
}
return commandScope.execute();
} catch (CommandValidationException cve) {
Throwable cause = cve.getCause();
if (cause instanceof MissingRequiredArgumentException) {
// This is a list of the arguments which the init project command supports. The thinking here is that if the user
// forgets to supply one of these arguments, we're going to remind them about the init project command, which
// can help them figure out what they should be providing here.
final Set<String> initProjectArguments = Stream.of(CommonArgumentNames.CHANGELOG_FILE, CommonArgumentNames.URL, CommonArgumentNames.USERNAME, CommonArgumentNames.PASSWORD).map(CommonArgumentNames::getArgumentName).collect(Collectors.toSet());
throw new CommandValidationException(cve.getMessage() + (initProjectArguments.contains(((MissingRequiredArgumentException) cause).getArgumentName()) ? ". If you need to configure new liquibase project files and arguments, run the 'liquibase init project' command." : ""));
} else {
throw cve;
}
} finally {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
}
}
public void setSpec(CommandLine.Model.CommandSpec spec) {
this.spec = spec;
}
}