|
| 1 | +package com.baeldung.airline; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import javax.inject.Inject; |
| 7 | + |
| 8 | +import com.github.rvesse.airline.HelpOption; |
| 9 | +import com.github.rvesse.airline.annotations.Command; |
| 10 | +import com.github.rvesse.airline.annotations.Option; |
| 11 | +import com.github.rvesse.airline.annotations.OptionType; |
| 12 | +import com.github.rvesse.airline.annotations.restrictions.AllowedRawValues; |
| 13 | +import com.github.rvesse.airline.annotations.restrictions.MutuallyExclusiveWith; |
| 14 | +import com.github.rvesse.airline.annotations.restrictions.Pattern; |
| 15 | +import com.github.rvesse.airline.annotations.restrictions.RequiredOnlyIf; |
| 16 | + |
| 17 | +@Command(name = "setup-db", description = "Setup our database") |
| 18 | +public class DatabaseSetupCommand implements Runnable { |
| 19 | + @Inject |
| 20 | + private HelpOption<DatabaseSetupCommand> help; |
| 21 | + |
| 22 | + @Option(type = OptionType.COMMAND, |
| 23 | + name = {"-d", "--database"}, |
| 24 | + description = "Type of RDBMS.", |
| 25 | + title = "RDBMS type: mysql|postgresql|mongodb") |
| 26 | + @AllowedRawValues(allowedValues = { "mysql", "postgres", "mongodb" }) |
| 27 | + protected String rdbmsMode = "mysql"; |
| 28 | + |
| 29 | + @Option(type = OptionType.COMMAND, |
| 30 | + name = {"--rdbms:url", "--url"}, |
| 31 | + description = "URL to use for connection to RDBMS.", |
| 32 | + title = "RDBMS URL") |
| 33 | + @MutuallyExclusiveWith(tag="mode") |
| 34 | + @Pattern(pattern="^(http://.*):(d*)(.*)u=(.*)&p=(.*)") |
| 35 | + protected String rdbmsUrl = ""; |
| 36 | + |
| 37 | + @Option(type = OptionType.COMMAND, |
| 38 | + name = {"--rdbms:host", "--host"}, |
| 39 | + description = "Host to use for connection to RDBMS.", |
| 40 | + title = "RDBMS host") |
| 41 | + @MutuallyExclusiveWith(tag="mode") |
| 42 | + protected String rdbmsHost = ""; |
| 43 | + |
| 44 | + @RequiredOnlyIf(names={"--rdbms:host", "--host"}) |
| 45 | + @Option(type = OptionType.COMMAND, |
| 46 | + name = {"--rdbms:user", "-u", "--user"}, |
| 47 | + description = "User for login to RDBMS.", |
| 48 | + title = "RDBMS user") |
| 49 | + protected String rdbmsUser; |
| 50 | + |
| 51 | + @RequiredOnlyIf(names={"--rdbms:host", "--host"}) |
| 52 | + @Option(type = OptionType.COMMAND, |
| 53 | + name = {"--rdbms:password", "--password"}, |
| 54 | + description = "Password for login to RDBMS.", |
| 55 | + title = "RDBMS password") |
| 56 | + protected String rdbmsPassword; |
| 57 | + |
| 58 | + @Option(type = OptionType.COMMAND, |
| 59 | + name = {"--driver", "--jars"}, |
| 60 | + description = "List of drivers", |
| 61 | + title = "--driver <PATH_TO_YOUR_JAR> --driver <PATH_TO_YOUR_JAR>") |
| 62 | + protected List<String> jars = new ArrayList<>(); |
| 63 | + |
| 64 | + @Override |
| 65 | + public void run() { |
| 66 | + //skipping store our choices... |
| 67 | + if (!help.showHelpIfRequested()) { |
| 68 | + if(!"".equals(rdbmsHost)) { |
| 69 | + System.out.println("Connecting to database host: " + rdbmsHost); |
| 70 | + System.out.println("Credential: " + rdbmsUser + " / " + rdbmsPassword); |
| 71 | + } else { |
| 72 | + System.out.println("Connecting to database url: " + rdbmsUrl); |
| 73 | + } |
| 74 | + System.out.println(jars.toString()); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments