X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fhalcyon.git;a=blobdiff_plain;f=src%2FConfiguration.hs;h=c2136431577a574cd2ace86a67e93dc7d3747e2d;hp=b4b29f3687bb7e9429ab0959368841e208fc9ea1;hb=1b72ed45ef890ed1329a32457b4d7f3a7fb37788;hpb=26718edaad5cd7921d957a1f0972fd9f5cd5b645 diff --git a/src/Configuration.hs b/src/Configuration.hs index b4b29f3..c213643 100644 --- a/src/Configuration.hs +++ b/src/Configuration.hs @@ -4,12 +4,19 @@ -- module Configuration ( - Cfg(..) -) + Cfg(..), + merge_optional ) where -import qualified OptionalConfiguration as OC +import System.Console.CmdArgs.Default ( Default(..) ) +import qualified OptionalConfiguration as OC ( OptionalCfg(..) ) +import Usernames ( Usernames(..) ) + + +-- | The main configuration data type. It contains all options that +-- can be set in a config file or on the command line. +-- data Cfg = Cfg { consumer_key :: String, consumer_secret :: String, @@ -18,27 +25,35 @@ data Cfg = heartbeat :: Int, ignore_replies :: Bool, ignore_retweets :: Bool, - sendmail_path :: String, + sendmail_path :: FilePath, from_address :: Maybe String, to_address :: Maybe String, - verbose :: Bool } + verbose :: Bool, + usernames :: Usernames } + deriving (Show) +instance Default Cfg where + -- | A 'Cfg' with all of its fields set to their default values. + -- + def = Cfg { consumer_key = def, + consumer_secret = def, + access_token = def, + access_secret = def, + heartbeat = 600, + ignore_replies = def, + ignore_retweets = def, + sendmail_path = "/usr/sbin/sendmail", + from_address = def, + to_address = def, + verbose = def, + usernames = def } -default_config :: Cfg -default_config = - Cfg { consumer_key = "", - consumer_secret = "", - access_token = "", - access_secret = "", - heartbeat = 600, - ignore_replies = False, - ignore_retweets = False, - sendmail_path = "/usr/sbin/sendmail", - from_address = Nothing, - to_address = Nothing, - verbose = False } +-- | Merge a 'Cfg' with an 'OptionalCfg'. This is more or less the +-- Monoid instance for 'OptionalCfg', but since the two types are +-- different, we have to repeat ourselves. +-- merge_optional :: Cfg -> OC.OptionalCfg -> Cfg merge_optional cfg opt_cfg = Cfg @@ -53,6 +68,7 @@ merge_optional cfg opt_cfg = (merge' (from_address cfg) (OC.from_address opt_cfg)) (merge' (to_address cfg) (OC.to_address opt_cfg)) (merge (verbose cfg) (OC.verbose opt_cfg)) + all_usernames where merge :: a -> Maybe a -> a merge x Nothing = x @@ -64,3 +80,8 @@ merge_optional cfg opt_cfg = merge' (Just x) Nothing = Just x merge' Nothing (Just x) = Just x merge' (Just _) (Just y) = Just y + + -- If there are any optional usernames, use only those. + all_usernames = if (null (get_usernames (OC.usernames opt_cfg))) + then (usernames cfg) + else (OC.usernames opt_cfg)