]> gitweb.michael.orlitzky.com - dead/htsn.git/blobdiff - src/Configuration.hs
Add a bunch of new options allowing htsn to daemonize.
[dead/htsn.git] / src / Configuration.hs
index 6234821c698f9de444101c5dd210a6a28db15223..68751d6015784242e7ecdb89fcf2ee568ef383da 100644 (file)
@@ -10,16 +10,22 @@ where
 import System.Console.CmdArgs.Default ( Default(..) )
 import System.Log ( Priority( INFO ) )
 
-import qualified OptionalConfiguration as OC (OptionalConfiguration(..))
+import qualified OptionalConfiguration as OC (
+  OptionalConfiguration(..),
+  merge_maybes )
 import TSN.FeedHosts (FeedHosts(..))
 
 data Configuration =
   Configuration {
+    daemonize        :: Bool,
     feed_hosts       :: FeedHosts,
-    log_file         :: FilePath,
+    log_file         :: Maybe FilePath,
     log_level        :: Priority,
-    password         :: String,
     output_directory :: FilePath,
+    password         :: String,
+    pidfile          :: FilePath,
+    run_as_group     :: Maybe String,
+    run_as_user      :: Maybe String,
     syslog           :: Bool,
     username         :: String }
     deriving (Show)
@@ -27,7 +33,18 @@ data Configuration =
 -- | A Configuration with all of its fields set to their default
 --   values.
 instance Default Configuration where
-  def = Configuration def "htsn.log" INFO def "." True def
+  def = Configuration {
+          daemonize        = def,
+          feed_hosts       = def,
+          log_file         = def,
+          log_level        = INFO,
+          output_directory = ".",
+          password         = def,
+          pidfile          = "/run/htsn.pid",
+          run_as_group     = def,
+          run_as_user      = def,
+          syslog           = def,
+          username         = def }
 
 
 -- | Merge a Configuration with an OptionalConfiguration. This is more
@@ -38,20 +55,26 @@ merge_optional :: Configuration
                -> Configuration
 merge_optional cfg opt_cfg =
   Configuration
+    (merge (daemonize cfg) (OC.daemonize opt_cfg))
     all_feed_hosts
-    (merge (log_file cfg) (OC.log_file opt_cfg))
+    (OC.merge_maybes (log_file cfg) (OC.log_file opt_cfg))
     (merge (log_level cfg) (OC.log_level opt_cfg))
-    (merge (password cfg) (OC.password opt_cfg))
     (merge (output_directory cfg) (OC.output_directory opt_cfg))
+    (merge (password cfg) (OC.password opt_cfg))
+    (merge (pidfile cfg) (OC.pidfile opt_cfg))
+    (OC.merge_maybes (run_as_group cfg) (OC.run_as_group opt_cfg))
+    (OC.merge_maybes (run_as_user cfg) (OC.run_as_user opt_cfg))
     (merge (syslog cfg) (OC.syslog opt_cfg))
     (merge (username cfg) (OC.username opt_cfg))
   where
+    -- | If the thing on the right is Just something, return that
+    --   something, otherwise return the thing on the left.
     merge :: a -> Maybe a -> a
     merge x Nothing  = x
     merge _ (Just y) = y
 
     -- If there are any optional usernames, use only those.
     all_feed_hosts = if (null (get_feed_hosts (OC.feed_hosts opt_cfg)))
-                    then (feed_hosts cfg)
-                    else (OC.feed_hosts opt_cfg)
+                     then (feed_hosts cfg)
+                     else (OC.feed_hosts opt_cfg)