diff --git a/pom.xml b/pom.xml index bb6877b..362a7b9 100644 --- a/pom.xml +++ b/pom.xml @@ -42,6 +42,11 @@ + + commons-cli + commons-cli + 1.3.1 + org.tomlj tomlj diff --git a/src/main/java/tech/bitgoblin/App.java b/src/main/java/tech/bitgoblin/App.java index 2d2aa97..ecd1137 100644 --- a/src/main/java/tech/bitgoblin/App.java +++ b/src/main/java/tech/bitgoblin/App.java @@ -1,5 +1,7 @@ package tech.bitgoblin; +import org.apache.commons.cli.ParseException; +import tech.bitgoblin.config.Cmd; import tech.bitgoblin.config.Config; import tech.bitgoblin.transcoder.RunTranscoderTask; import tech.bitgoblin.transcoder.Transcoder; @@ -12,13 +14,15 @@ import java.util.Timer; */ public class App { - private static final String configFile = "~/.config/dragoon.toml"; - private static final int msToMinutes = 60 * 1000; - public static void main(String[] args) { + public static void main(String[] args) throws ParseException { + // parse command-line options + Cmd cmd = new Cmd(args); + // read our config file - Config c = new Config(configFile); + Config c = new Config(cmd.getConfigPath()); + // create new Transcoder object and start the service Transcoder t = new Transcoder(c); Timer timer = new Timer(); diff --git a/src/main/java/tech/bitgoblin/config/Cmd.java b/src/main/java/tech/bitgoblin/config/Cmd.java new file mode 100644 index 0000000..4431d49 --- /dev/null +++ b/src/main/java/tech/bitgoblin/config/Cmd.java @@ -0,0 +1,30 @@ +package tech.bitgoblin.config; + +import org.apache.commons.cli.*; + +public class Cmd { + + private String configPath = "/etc/dragoon/config.toml"; + + public Cmd(String[] args) throws ParseException { + Options options = new Options(); + + Option configPath = new Option("c", "configPath", true, "configuration file path (defaults to /etc/dragoon/config.toml)"); + configPath.setRequired(false); + options.addOption(configPath); + + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + CommandLine cmd = parser.parse(options, args); + + // set the configPath variable if the option was passed to the program + if (cmd.hasOption("configPath")) { + this.configPath = cmd.getOptionValue("configPath"); + } + } + + public String getConfigPath() { + return this.configPath; + } + +} diff --git a/src/test/java/tech/bitgoblin/config/CmdTest.java b/src/test/java/tech/bitgoblin/config/CmdTest.java new file mode 100644 index 0000000..fee6a6a --- /dev/null +++ b/src/test/java/tech/bitgoblin/config/CmdTest.java @@ -0,0 +1,17 @@ +package tech.bitgoblin.config; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import org.apache.commons.cli.ParseException; + +public class CmdTest { + + @Test + public void shouldDefaultToEtc() throws ParseException { + Cmd cmd = new Cmd(new String[]{}); + assertTrue(cmd.getConfigPath().equals("/etc/dragoon/config.toml")); + } + +}