if (used) {
throw new IllegalStateException("CLI instance already used");
}
used = true;
CLIParser parser = new CLIParser("ooziedb.sh", HELP_INFO);
parser.addCommand(HELP_CMD, "", "display usage for all commands or specified command", new Options(), false);
parser.addCommand(VERSION_CMD, "", "show Oozie DB version information", new Options(), false);
parser.addCommand(CREATE_CMD, "", "create Oozie DB schema", createUpgradeOptions(), false);
parser.addCommand(UPGRADE_CMD, "", "upgrade Oozie DB", createUpgradeOptions(), false);
parser.addCommand(POST_UPGRADE_CMD, "", "post upgrade Oozie DB", createUpgradeOptions(), false);
try {
System.out.println();
CLIParser.Command command = parser.parse(args);
if (command.getName().equals(HELP_CMD)) {
parser.showHelp(command.getCommandLine());
}
else if (command.getName().equals(VERSION_CMD)) {
showVersion();
}
else {
if (!command.getCommandLine().hasOption(SQL_FILE_OPT) &&
!command.getCommandLine().hasOption(RUN_OPT)) {
throw new Exception("'-sqlfile <FILE>' or '-run' options must be specified");
}
CommandLine commandLine = command.getCommandLine();
String sqlFile = (commandLine.hasOption(SQL_FILE_OPT))
? commandLine.getOptionValue(SQL_FILE_OPT)
: File.createTempFile("ooziedb-", ".sql").getAbsolutePath();
boolean run = commandLine.hasOption(RUN_OPT);
if (command.getName().equals(CREATE_CMD)) {
createDB(sqlFile, run);
}
if (command.getName().equals(UPGRADE_CMD)) {
upgradeDB(sqlFile, run);
}
if (command.getName().equals(POST_UPGRADE_CMD)) {
postUpgradeDB(sqlFile, run);
}
System.out.println();
System.out.println("The SQL commands have been written to: " + sqlFile);
if (!run) {
System.out.println();
System.out.println("WARN: The SQL commands have NOT been executed, you must use the '-run' option");
System.out.println();
}
}
return 0;
}
catch (ParseException ex) {
System.err.println("Invalid sub-command: " + ex.getMessage());
System.err.println();
System.err.println(parser.shortHelp());
return 1;
}
catch (Exception ex) {
System.err.println();
System.err.println("Error: " + ex.getMessage());