Skip to content

Commit

Permalink
Merge pull request #1943 from liquibase/reintroduce-d-arguments-in-cli
Browse files Browse the repository at this point in the history
Reintroduce support for "-D" arguments in CLI
  • Loading branch information
suryaaki2 authored Jul 20, 2021
2 parents 4b4ea6e + da43b5c commit d3f35b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ public int execute(String[] args) {
Scope.getCurrentScope().getUI().sendMessage(Scope.getCurrentScope().getSingleton(LicenseServiceFactory.class).getLicenseService().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()) {
Expand Down Expand Up @@ -688,6 +699,15 @@ private void addSubcommand(CommandDefinition commandDefinition, CommandLine root
}

subCommandSpec.addOption(builder.build());

if (argName.equals("--changelog-file")) {
final CommandLine.Model.OptionSpec.Builder paramBuilder = (CommandLine.Model.OptionSpec.Builder) CommandLine.Model.OptionSpec.builder("-D")
.required(false)
.type(HashMap.class)
.description("Pass a name/value pair for substitution in the changelog(s)\nPass as -D<property.name>=<property.value>\n[deprecated: set changelog properties in defaults file or environment variables]")
.mapFallbackValue("");
subCommandSpec.add(paramBuilder.build());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package liquibase.integration.commandline

import liquibase.command.CommandBuilder
import liquibase.configuration.ConfigurationDefinition
import picocli.CommandLine
import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -54,4 +55,13 @@ class LiquibaseCommandLineTest extends Specification {

["--log-level","DEBUG","--log-file","06V21.txt","--defaultsFile=liquibase.h2-mem.properties","update","--changelog-file","postgres_lbpro_master_changelog.xml","--labels","setup"] | ["--log-level","DEBUG","--log-file","06V21.txt","--defaultsFile=liquibase.h2-mem.properties","update","--changelog-file","postgres_lbpro_master_changelog.xml","--labels","setup"]
}

def "accepts -D subcommand arguments for changelog parameters"() {
when:
def subcommands = new LiquibaseCommandLine().commandLine.getSubcommands()

then:
subcommands["update"].commandSpec.findOption("-D") != null
subcommands["snapshot"].commandSpec.findOption("-D") == null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public class Main {
//set by new CLI to signify it is handling some of the configuration
public static boolean runningFromNewCli;

//temporary work-around to pass -D changelog parameters from new CLI to here
public static Map<String, String> newCliChangelogParameters;

private static PrintStream outputStream = System.out;

private static final String ERRORMSG_UNEXPECTED_PARAMETERS = "unexpected.command.parameters";
Expand Down Expand Up @@ -1674,6 +1677,11 @@ protected void doMigration() throws Exception {
}

Liquibase liquibase = new Liquibase(changeLogFile, fileOpener, database);
if (Main.newCliChangelogParameters != null) {
for (Map.Entry<String, String> param : Main.newCliChangelogParameters.entrySet()) {
liquibase.setChangeLogParameter(param.getKey(), param.getValue());
}
}
try {
if (hubConnectionId != null) {
try {
Expand Down

0 comments on commit d3f35b5

Please sign in to comment.