All Downloads are FREE. Search and download functionalities are using the official Maven repository.

in.cleartax.dropwizard.sharding.migrations.DbStatusCommand Maven / Gradle / Ivy

There is a newer version: 0.2.198
Show newest version
package in.cleartax.dropwizard.sharding.migrations;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import io.dropwizard.Configuration;
import liquibase.Liquibase;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;

import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class DbStatusCommand extends AbstractLiquibaseCommand {

    private PrintStream outputStream = System.out;

    public DbStatusCommand(MultiTenantDatabaseConfiguration strategy, Class configurationClass, String migrationsFileName) {
        super("status", "Check for pending change sets.", strategy, configurationClass, migrationsFileName);
    }

    @VisibleForTesting
    void setOutputStream(PrintStream outputStream) {
        this.outputStream = outputStream;
    }

    @Override
    public void configure(Subparser subparser) {
        super.configure(subparser);

        subparser.addArgument("-v", "--verbose")
                .action(Arguments.storeTrue())
                .dest("verbose")
                .help("Output verbose information");
        subparser.addArgument("-i", "--include")
                .action(Arguments.append())
                .dest("contexts")
                .help("include change sets from the given context");
    }

    @Override
    @SuppressWarnings("UseOfSystemOutOrSystemErr")
    public void run(Namespace namespace, Liquibase liquibase) throws Exception {
        liquibase.reportStatus(MoreObjects.firstNonNull(namespace.getBoolean("verbose"), false),
                getContext(namespace),
                new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
    }

    private String getContext(Namespace namespace) {
        final List contexts = namespace.getList("contexts");
        if (contexts == null) {
            return "";
        }
        return Joiner.on(',').join(contexts);
    }
}