View Javadoc
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  
18  import org.apache.ibatis.migration.MigrationException;
19  import org.apache.ibatis.migration.options.SelectedOptions;
20  
21  public enum Commands {
22    INFO,
23  
24    INIT,
25  
26    BOOTSTRAP,
27  
28    NEW,
29  
30    UP,
31  
32    DOWN,
33  
34    PENDING,
35  
36    SCRIPT,
37  
38    VERSION,
39  
40    STATUS,
41  
42    REDO;
43  
44    public static Command resolveCommand(String commandString, SelectedOptions selectedOptions) {
45      final String upperCasedStr = commandString.toUpperCase();
46      for (Commands command : values()) {
47        if (command.name().startsWith(upperCasedStr)) {
48          return createCommand(command, selectedOptions);
49        }
50      }
51  
52      throw new MigrationException("Attempt to execute unknown command: " + commandString);
53    }
54  
55    private static Command createCommand(Commands aResolvedCommand, SelectedOptions selectedOptions) {
56      switch (aResolvedCommand) {
57        case INFO:
58          return new InfoCommand(System.out);
59        case INIT:
60          return new InitializeCommand(selectedOptions);
61        case BOOTSTRAP:
62          return new BootstrapCommand(selectedOptions);
63        case NEW:
64          return new NewCommand(selectedOptions);
65        case UP:
66          return new UpCommand(selectedOptions);
67        case DOWN:
68          return new DownCommand(selectedOptions);
69        case PENDING:
70          return new PendingCommand(selectedOptions);
71        case SCRIPT:
72          return new ScriptCommand(selectedOptions);
73        case VERSION:
74          return new VersionCommand(selectedOptions);
75        case STATUS:
76          return new StatusCommand(selectedOptions);
77        case REDO:
78          return new RedoCommand(selectedOptions);
79        default:
80          return params -> System.out.println("unknown command");
81      }
82    }
83  }