⬆️ Update a bunch of dependencies
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / engine / Starter.java
index 4c3b4db..2908ddd 100644 (file)
 
 package net.pterodactylus.rhynodge.engine;
 
+import java.io.IOException;
+import java.util.Arrays;
+
+import net.pterodactylus.rhynodge.actions.EmailAction;
 import net.pterodactylus.rhynodge.loader.ChainWatcher;
-import net.pterodactylus.rhynodge.states.StateManager;
+import net.pterodactylus.rhynodge.loader.ChainWatcher.ChainDirectory;
+import net.pterodactylus.rhynodge.states.StateManager.StateDirectory;
+import net.pterodactylus.util.envopt.Parser;
+import net.pterodactylus.util.inject.ObjectBinding;
 
-import com.lexicalscope.jewel.cli.CliFactory;
-import com.lexicalscope.jewel.cli.Option;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
 
 /**
  * Rhynodge main starter class.
@@ -36,45 +43,24 @@ public class Starter {
         * @param arguments
         *            Command-line arguments
         */
-       public static void main(String... arguments) {
-
-               /* parse command line. */
-               Parameters parameters = CliFactory.parseArguments(Parameters.class, arguments);
-
-               /* create the state manager. */
-               StateManager stateManager = new StateManager(parameters.getStateDirectory());
+       public static void main(String... arguments) throws IOException {
+               Options options = Parser.fromSystemEnvironment().parseEnvironment(Options::new);
+               EmailAction errorEmailAction =
+                               createErrorEmailAction(options.smtpHostname, options.errorEmailSender, options.errorEmailRecipient);
 
-               /* create the engine. */
-               Engine engine = new Engine(stateManager);
+               Injector injector = Guice.createInjector(Arrays.asList(
+                               ObjectBinding.forClass(StateDirectory.class).is(StateDirectory.of(options.stateDirectory)),
+                               ObjectBinding.forClass(ChainDirectory.class).is(ChainDirectory.of(options.chainDirectory)),
+                               ObjectBinding.forClass(EmailAction.class).is(errorEmailAction)
+               ));
 
                /* start a watcher. */
-               ChainWatcher chainWatcher = new ChainWatcher(engine, parameters.getChainDirectory());
-               chainWatcher.start();
+               ChainWatcher chainWatcher = injector.getInstance(ChainWatcher.class);
+               chainWatcher.startAsync().awaitTerminated();
        }
 
-       /**
-        * Definition of the command-line parameters.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private static interface Parameters {
-
-               /**
-                * Returns the directory to watch for chains.
-                *
-                * @return The chain directory
-                */
-               @Option(defaultValue = "chains", longName = "chains", shortName = "c", description = "The directory to watch for chains")
-               String getChainDirectory();
-
-               /**
-                * Returns the directory to store states in.
-                *
-                * @return The states directory
-                */
-               @Option(defaultValue = "states", longName = "states", shortName = "s", description = "The directory to store states in")
-               String getStateDirectory();
-
+       private static EmailAction createErrorEmailAction(String smtpHostname, String errorEmailSender, String errorEmailRecipient) {
+               return new EmailAction(smtpHostname, errorEmailSender, errorEmailRecipient);
        }
 
 }