|
| 1 | +package net.jlmorton.tableau; |
| 2 | + |
| 3 | +import org.apache.commons.cli.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | + |
| 7 | +class CommandLinePropertySource { |
| 8 | + private final String[] args; |
| 9 | + |
| 10 | + CommandLinePropertySource(String... args) { |
| 11 | + this.args = args; |
| 12 | + } |
| 13 | + |
| 14 | + Properties getProperties() { |
| 15 | + CommandLine commandLine = parseCommandLineOptions(); |
| 16 | + |
| 17 | + return new Properties() { |
| 18 | + @Override |
| 19 | + public String getSchemaPath() { |
| 20 | + return commandLine.getOptionValue("schema"); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public File getCsvFile() { |
| 25 | + return new File(commandLine.getOptionValue("file")); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getExtractFilePath() { |
| 30 | + return commandLine.getOptionValue("extract"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public String getTableauSiteName() { |
| 35 | + return commandLine.getOptionValue("site"); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getTableauProjectName() { |
| 40 | + return commandLine.getOptionValue("project"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getTableauDatasourceName() { |
| 45 | + return commandLine.getOptionValue("datasource"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getTableauServerUrl() { |
| 50 | + return commandLine.getOptionValue("url"); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String getTableauServerUsername() { |
| 55 | + return commandLine.getOptionValue("username"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getTableauServerPassword() { |
| 60 | + return commandLine.getOptionValue("password"); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int getNumberOfThreads() { |
| 65 | + if (commandLine.hasOption("threads")) { |
| 66 | + return Integer.valueOf(commandLine.getOptionValue("threads")); |
| 67 | + } |
| 68 | + |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean isPublish() { |
| 74 | + return commandLine.hasOption("publish"); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean isExtract() { |
| 79 | + return commandLine.hasOption("extract"); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean isAppend() { |
| 84 | + return commandLine.hasOption("append"); |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + private CommandLine parseCommandLineOptions() { |
| 90 | + CommandLineParser parser = new DefaultParser(); |
| 91 | + |
| 92 | + try { |
| 93 | + CommandLine commandLine = parser.parse(getOptions(), args); |
| 94 | + if (commandLine.hasOption("help")) { |
| 95 | + printHelp(); |
| 96 | + } |
| 97 | + |
| 98 | + return commandLine; |
| 99 | + } catch (ParseException e) { |
| 100 | + throw new RuntimeException("Could not parse command line options", e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static Options getOptions() { |
| 105 | + Options options = new Options(); |
| 106 | + options.addOption("s", "schema", true, "Schema file for extract"); |
| 107 | + options.addOption("f", "file", true, "CSV file to import"); |
| 108 | + options.addOption("a", "append", false, "Append to existing extract"); |
| 109 | + options.addOption("o", "output", true, "Output file name, or name of existing extract in append mode"); |
| 110 | + options.addOption("t", "threads", true, "Number of threads (default: 1)"); |
| 111 | + options.addOption("p", "publish", false, "Publish an extract to Tableau (requires --extract, --site, --project, --datasource, --username --password, and --url,"); |
| 112 | + options.addOption("s", "site", true, "Tableau site name to publish"); |
| 113 | + options.addOption("c", "project", true, "Project name to publish to"); |
| 114 | + options.addOption("e", "extract", true, "Filename of extract to publish"); |
| 115 | + options.addOption("d", "datasource", true, "Name of datasource to publish"); |
| 116 | + options.addOption("u", "url", true, "Tableau Server URL for publishing"); |
| 117 | + options.addOption("n", "username", true, "Tableau Server username for publishing"); |
| 118 | + options.addOption("x", "password", true, "Tableau Server password for publishing"); |
| 119 | + options.addOption("h", "help", false, ""); |
| 120 | + |
| 121 | + return options; |
| 122 | + } |
| 123 | + |
| 124 | + static void printHelp() { |
| 125 | + HelpFormatter helpFormatter = new HelpFormatter(); |
| 126 | + helpFormatter.printHelp("java -jar tableau.jar", getOptions()); |
| 127 | + System.exit(1); |
| 128 | + } |
| 129 | +} |
0 commit comments