Commands.java

  1. /*
  2.  *    Copyright 2010-2023 the original author or authors.
  3.  *
  4.  *    Licensed under the Apache License, Version 2.0 (the "License");
  5.  *    you may not use this file except in compliance with the License.
  6.  *    You may obtain a copy of the License at
  7.  *
  8.  *       https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  *    Unless required by applicable law or agreed to in writing, software
  11.  *    distributed under the License is distributed on an "AS IS" BASIS,
  12.  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  *    See the License for the specific language governing permissions and
  14.  *    limitations under the License.
  15.  */
  16. package org.apache.ibatis.migration.commands;

  17. import org.apache.ibatis.migration.MigrationException;
  18. import org.apache.ibatis.migration.options.SelectedOptions;

  19. public enum Commands {
  20.   INFO,

  21.   INIT,

  22.   BOOTSTRAP,

  23.   NEW,

  24.   UP,

  25.   DOWN,

  26.   PENDING,

  27.   SCRIPT,

  28.   VERSION,

  29.   STATUS,

  30.   REDO;

  31.   public static Command resolveCommand(String commandString, SelectedOptions selectedOptions) {
  32.     final String upperCasedStr = commandString.toUpperCase();
  33.     for (Commands command : values()) {
  34.       if (command.name().startsWith(upperCasedStr)) {
  35.         return createCommand(command, selectedOptions);
  36.       }
  37.     }

  38.     throw new MigrationException("Attempt to execute unknown command: " + commandString);
  39.   }

  40.   private static Command createCommand(Commands aResolvedCommand, SelectedOptions selectedOptions) {
  41.     switch (aResolvedCommand) {
  42.       case INFO:
  43.         return new InfoCommand(System.out);
  44.       case INIT:
  45.         return new InitializeCommand(selectedOptions);
  46.       case BOOTSTRAP:
  47.         return new BootstrapCommand(selectedOptions);
  48.       case NEW:
  49.         return new NewCommand(selectedOptions);
  50.       case UP:
  51.         return new UpCommand(selectedOptions);
  52.       case DOWN:
  53.         return new DownCommand(selectedOptions);
  54.       case PENDING:
  55.         return new PendingCommand(selectedOptions);
  56.       case SCRIPT:
  57.         return new ScriptCommand(selectedOptions);
  58.       case VERSION:
  59.         return new VersionCommand(selectedOptions);
  60.       case STATUS:
  61.         return new StatusCommand(selectedOptions);
  62.       case REDO:
  63.         return new RedoCommand(selectedOptions);
  64.       default:
  65.         return params -> System.out.println("unknown command");
  66.     }
  67.   }
  68. }