Search in sources :

Example 1 with MissingRequiredArgumentException

use of liquibase.exception.MissingRequiredArgumentException in project liquibase by liquibase.

the class CommandRunner method call.

@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();
        }
    }
}
Also used : Set(java.util.Set) CommandValidationException(liquibase.exception.CommandValidationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) CommandLine(picocli.CommandLine) MissingRequiredArgumentException(liquibase.exception.MissingRequiredArgumentException) FileOutputStream(java.io.FileOutputStream) CommandScope(liquibase.command.CommandScope) File(java.io.File) CommonArgumentNames(liquibase.command.CommonArgumentNames)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 CommandScope (liquibase.command.CommandScope)1 CommonArgumentNames (liquibase.command.CommonArgumentNames)1 CommandValidationException (liquibase.exception.CommandValidationException)1 MissingRequiredArgumentException (liquibase.exception.MissingRequiredArgumentException)1 CommandLine (picocli.CommandLine)1