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

org.apache.ibatis.migration.CommandLine Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
package org.apache.ibatis.migration;

import org.apache.ibatis.migration.commands.Command;
import org.apache.ibatis.migration.options.SelectedOptions;

import java.io.File;
import java.io.PrintStream;
import java.util.Date;

import static org.apache.ibatis.migration.commands.Commands.resolveCommand;
import static org.apache.ibatis.migration.options.OptionsParser.parse;

public class CommandLine {
  private final PrintStream console = System.out;
  private final String[] args;

  public CommandLine(String[] args) {
    this.args = args;
  }

  public void execute() {
    final SelectedOptions selectedOptions = parse(args);
    try {
      if (!validOptions(selectedOptions) || selectedOptions.needsHelp()) {
        printUsage();
      } else {
        runCommand(selectedOptions);
      }
    } catch (Exception e) {
      console.printf("\nERROR: %s", e.getMessage());
      if (selectedOptions.isTrace()) {
        e.printStackTrace();
      }
      System.exit(1); // Issue 730
    }
  }

  private void runCommand(SelectedOptions selectedOptions) {
    final String commandString = selectedOptions.getCommand();

    console.printf("------------------------------------------------------------------------%n");
    console.printf("-- MyBatis Migrations - %s%n", commandString);
    console.printf("------------------------------------------------------------------------%n");

    long start = System.currentTimeMillis();
    boolean exceptionCaught = false;

    try {
      final Command command = resolveCommand(commandString.toUpperCase(), selectedOptions);
      command.execute(selectedOptions.getParams());
    } catch (Throwable t) {
      exceptionCaught = true;
      if (t instanceof MigrationException) {
        throw (MigrationException) t;
      } else {
        throw new MigrationException(t);
      }
    } finally {
      console.printf("------------------------------------------------------------------------%n");
      console.printf("-- MyBatis Migrations %s%n", (exceptionCaught) ? "FAILURE" : "SUCCESS");
      console.printf("-- Total time: %ss%n", ((System.currentTimeMillis() - start) / 1000));
      console.printf("-- Finished at: %s%n", new Date());
      printMemoryUsage();
      console.printf("------------------------------------------------------------------------%n");
    }
  }

  private void printMemoryUsage() {
    final Runtime runtime = Runtime.getRuntime();
    final int megaUnit = 1024 * 1024;
    final long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / megaUnit;
    final long totalMemory = runtime.totalMemory() / megaUnit;

    console.printf("-- Final Memory: %sM/%sM%n", usedMemory, totalMemory);
  }

  private boolean validOptions(SelectedOptions selectedOptions) {
    if (!selectedOptions.needsHelp() && selectedOptions.getCommand() == null) {
      console.printf("No command specified.%n");
      return false;
    }

    return validBasePath(selectedOptions.getPaths().getBasePath());
  }

  private boolean validBasePath(File basePath) {
    final boolean validDirectory = basePath.exists() && basePath.isDirectory();

    if (!validDirectory) {
      console.printf("Migrations path must be a directory: %s%n", basePath.getAbsolutePath());
    }

    return validDirectory;
  }

  private void printUsage() {
    console.printf(
        "%nUsage: migrate command [parameter] [--path=] [--env=] [--template=]%n%n");
    console.printf("--path=   Path to repository.  Default current working directory.%n");
    console.printf("--env=  Environment to configure. Default environment is 'development'.%n");
    console.printf("--template=