X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FOptionalConfiguration.hs;h=69ea04176583956f4bac6cf9ce89637bdc7806b7;hb=f6cb0ba712e06e52d080b86e9eba6c3585a7514b;hp=d133432bb3c23280b00690df4c2e774edf9f19ff;hpb=52e788e676ea2a67ad20b23d2e5c5b351f27b834;p=dead%2Fhtsn.git diff --git a/src/OptionalConfiguration.hs b/src/OptionalConfiguration.hs index d133432..69ea041 100644 --- a/src/OptionalConfiguration.hs +++ b/src/OptionalConfiguration.hs @@ -12,7 +12,8 @@ module OptionalConfiguration ( OptionalConfiguration(..), - from_rc ) + from_rc, + merge_maybes ) where import qualified Data.Configurator as DC ( @@ -31,6 +32,7 @@ import System.Directory ( getHomeDirectory ) import System.FilePath ( () ) import System.IO.Error ( catchIOError ) import System.Log ( Priority(..) ) + import Logging ( log_error ) import TSN.FeedHosts ( FeedHosts(..) ) @@ -48,30 +50,42 @@ deriving instance Typeable Priority -- data OptionalConfiguration = OptionalConfiguration { + daemonize :: Maybe Bool, feed_hosts :: FeedHosts, log_file :: Maybe FilePath, log_level :: Maybe Priority, - password :: Maybe String, output_directory :: Maybe FilePath, + password :: Maybe String, + pidfile :: Maybe FilePath, + run_as_group :: Maybe String, + run_as_user :: Maybe String, syslog :: Maybe Bool, username :: Maybe String } deriving (Show, Data, Typeable) +-- | Combine two Maybes into one, essentially mashing them +-- together. We give precedence to the second argument when both are +-- Justs. +merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a) +merge_maybes Nothing Nothing = Nothing +merge_maybes (Just x) Nothing = Just x +merge_maybes Nothing (Just x) = Just x +merge_maybes (Just _) (Just y) = Just y + + -- | The Monoid instance for these lets us "combine" two -- OptionalConfigurations. The "combine" operation that we'd like to -- perform is, essentially, to mash them together. So if we have two -- OptionalConfigurations, each half full, we could combine them -- into one big one. -- --- One of the two must take precedence during this mashing, and we --- choose the second one for no reason. --- -- This is used to merge command-line and config-file settings. -- instance Monoid OptionalConfiguration where -- | An empty OptionalConfiguration. mempty = OptionalConfiguration + Nothing (FeedHosts []) Nothing Nothing @@ -79,25 +93,26 @@ instance Monoid OptionalConfiguration where Nothing Nothing Nothing + Nothing + Nothing + Nothing -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@. cfg1 `mappend` cfg2 = OptionalConfiguration + (merge_maybes (daemonize cfg1) (daemonize cfg2)) all_feed_hosts - (merge (log_file cfg1) (log_file cfg2)) - (merge (log_level cfg1) (log_level cfg2)) - (merge (password cfg1) (password cfg2)) - (merge (output_directory cfg1) (output_directory cfg2)) - (merge (syslog cfg1) (syslog cfg2)) - (merge (username cfg1) (username cfg2)) + (merge_maybes (log_file cfg1) (log_file cfg2)) + (merge_maybes (log_level cfg1) (log_level cfg2)) + (merge_maybes (output_directory cfg1) (output_directory cfg2)) + (merge_maybes (password cfg1) (password cfg2)) + (merge_maybes (pidfile cfg1) (pidfile cfg2)) + (merge_maybes (run_as_group cfg1) (run_as_group cfg2)) + (merge_maybes (run_as_user cfg1) (run_as_user cfg2)) + (merge_maybes (syslog cfg1) (syslog cfg2)) + (merge_maybes (username cfg1) (username cfg2)) where - merge :: (Maybe a) -> (Maybe a) -> (Maybe a) - merge Nothing Nothing = Nothing - merge (Just x) Nothing = Just x - merge Nothing (Just x) = Just x - merge (Just _) (Just y) = Just y - -- Use only the latter feed_hosts if there are any. all_feed_hosts = feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2))) @@ -136,20 +151,27 @@ from_rc = do return "$(HOME)") let user_config_path = home ".htsnrc" cfg <- DC.load [ DC.Optional user_config_path ] + cfg_daemonize <- DC.lookup cfg "daemonize" + cfg_feed_hosts <- DC.lookup cfg "feed_hosts" cfg_log_file <- DC.lookup cfg "log_file" cfg_log_level <- DC.lookup cfg "log_level" - cfg_password <- DC.lookup cfg "password" cfg_output_directory <- DC.lookup cfg "output_directory" + cfg_password <- DC.lookup cfg "password" + cfg_pidfile <- DC.lookup cfg "pidfile" + cfg_run_as_group <- DC.lookup cfg "run_as_group" + cfg_run_as_user <- DC.lookup cfg "run_as_user" cfg_syslog <- DC.lookup cfg "syslog" cfg_username <- DC.lookup cfg "username" - cfg_feed_hosts <- DC.lookup cfg "feed_hosts" return $ OptionalConfiguration + cfg_daemonize (fromMaybe (FeedHosts []) cfg_feed_hosts) cfg_log_file cfg_log_level - cfg_password cfg_output_directory + cfg_password + cfg_pidfile + cfg_run_as_group + cfg_run_as_user cfg_syslog cfg_username -