Search in sources :

Example 1 with ConfigurationValueProvider

use of liquibase.configuration.ConfigurationValueProvider in project liquibase by liquibase.

the class LiquibaseCommandLine method registerValueProviders.

private List<ConfigurationValueProvider> registerValueProviders(String[] args) throws IOException {
    final LiquibaseConfiguration liquibaseConfiguration = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
    List<ConfigurationValueProvider> returnList = new ArrayList<>();
    final CommandLineArgumentValueProvider argumentProvider = new CommandLineArgumentValueProvider(commandLine.parseArgs(args));
    liquibaseConfiguration.registerProvider(argumentProvider);
    returnList.add(argumentProvider);
    final ConfiguredValue<String> defaultsFileConfig = LiquibaseCommandLineConfiguration.DEFAULTS_FILE.getCurrentConfiguredValue();
    final File defaultsFile = new File(defaultsFileConfig.getValue());
    if (defaultsFile.exists()) {
        final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(defaultsFile);
        liquibaseConfiguration.registerProvider(fileProvider);
        returnList.add(fileProvider);
    } else {
        final InputStream defaultsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(defaultsFileConfig.getValue());
        if (defaultsStream == null) {
            Scope.getCurrentScope().getLog(getClass()).fine("Cannot find defaultsFile " + defaultsFile.getAbsolutePath());
            if (!defaultsFileConfig.wasDefaultValueUsed()) {
                // can't use UI since it's not configured correctly yet
                System.err.println("Could not find defaults file " + defaultsFileConfig.getValue());
            }
        } else {
            final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(defaultsStream, "File in classpath " + defaultsFileConfig.getValue());
            liquibaseConfiguration.registerProvider(fileProvider);
            returnList.add(fileProvider);
        }
    }
    File localDefaultsFile = new File(defaultsFile.getAbsolutePath().replaceFirst(".properties$", ".local.properties"));
    if (localDefaultsFile.exists()) {
        final DefaultsFileValueProvider fileProvider = new DefaultsFileValueProvider(localDefaultsFile) {

            @Override
            public int getPrecedence() {
                return super.getPrecedence() + 1;
            }
        };
        liquibaseConfiguration.registerProvider(fileProvider);
        returnList.add(fileProvider);
    } else {
        Scope.getCurrentScope().getLog(getClass()).fine("Cannot find local defaultsFile " + defaultsFile.getAbsolutePath());
    }
    return returnList;
}
Also used : ConfigurationValueProvider(liquibase.configuration.ConfigurationValueProvider) DefaultsFileValueProvider(liquibase.configuration.core.DefaultsFileValueProvider) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration)

Example 2 with ConfigurationValueProvider

use of liquibase.configuration.ConfigurationValueProvider in project liquibase by liquibase.

the class LiquibaseCommandLine method execute.

public int execute(String[] args) {
    try {
        final String[] finalArgs = adjustLegacyArgs(args);
        configureLogging(Level.OFF, null);
        Main.runningFromNewCli = true;
        final List<ConfigurationValueProvider> valueProviders = registerValueProviders(finalArgs);
        try {
            return Scope.child(configureScope(), () -> {
                if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) {
                    Scope.getCurrentScope().getUI().sendErrorMessage((String.format(coreBundle.getString("did.not.run.because.param.was.set.to.false"), LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentConfiguredValue().getProvidedValue().getActualKey())));
                    return 0;
                }
                configureVersionInfo();
                if (!wasHelpOrVersionRequested()) {
                    Scope.getCurrentScope().getUI().sendMessage(CommandLineUtils.getBanner());
                    Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("version.number"), LiquibaseUtil.getBuildVersionInfo()));
                    final LicenseService licenseService = Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class).getLicenseService();
                    if (licenseService == null) {
                        Scope.getCurrentScope().getUI().sendMessage("WARNING: License service not loaded, cannot determine Liquibase Pro license status. Please consider re-installing Liquibase to include all dependencies. Continuing operation without Pro license.");
                    } else {
                        Scope.getCurrentScope().getUI().sendMessage(licenseService.getLicenseInfo());
                    }
                }
                CommandLine.ParseResult subcommandParseResult = commandLine.getParseResult();
                while (subcommandParseResult.hasSubcommand()) {
                    subcommandParseResult = subcommandParseResult.subcommand();
                }
                Map<String, String> changelogParameters = subcommandParseResult.matchedOptionValue("-D", new HashMap<>());
                if (changelogParameters.size() != 0) {
                    Main.newCliChangelogParameters = changelogParameters;
                }
                int response = commandLine.execute(finalArgs);
                if (!wasHelpOrVersionRequested()) {
                    final ConfiguredValue<File> logFile = LiquibaseCommandLineConfiguration.LOG_FILE.getCurrentConfiguredValue();
                    if (logFile.found()) {
                        Scope.getCurrentScope().getUI().sendMessage("Logs saved to " + logFile.getValue().getAbsolutePath());
                    }
                    final ConfiguredValue<File> outputFile = LiquibaseCommandLineConfiguration.OUTPUT_FILE.getCurrentConfiguredValue();
                    if (outputFile.found()) {
                        Scope.getCurrentScope().getUI().sendMessage("Output saved to " + outputFile.getValue().getAbsolutePath());
                    }
                    if (response == 0) {
                        final List<CommandLine> commandList = commandLine.getParseResult().asCommandLineList();
                        final String commandName = StringUtil.join(getCommandNames(commandList.get(commandList.size() - 1)), " ");
                        Scope.getCurrentScope().getUI().sendMessage("Liquibase command '" + commandName + "' was executed successfully.");
                    }
                }
                return response;
            });
        } finally {
            final LiquibaseConfiguration liquibaseConfiguration = Scope.getCurrentScope().getSingleton(LiquibaseConfiguration.class);
            for (ConfigurationValueProvider provider : valueProviders) {
                liquibaseConfiguration.unregisterProvider(provider);
            }
        }
    } catch (Throwable e) {
        handleException(e);
        return 1;
    } finally {
        cleanup();
    }
}
Also used : ConfigurationValueProvider(liquibase.configuration.ConfigurationValueProvider) CommandLine(picocli.CommandLine) LicenseService(liquibase.license.LicenseService) LicenseServiceFactory(liquibase.license.LicenseServiceFactory) LiquibaseConfiguration(liquibase.configuration.LiquibaseConfiguration)

Aggregations

ConfigurationValueProvider (liquibase.configuration.ConfigurationValueProvider)2 LiquibaseConfiguration (liquibase.configuration.LiquibaseConfiguration)2 DefaultsFileValueProvider (liquibase.configuration.core.DefaultsFileValueProvider)1 LicenseService (liquibase.license.LicenseService)1 LicenseServiceFactory (liquibase.license.LicenseServiceFactory)1 CommandLine (picocli.CommandLine)1