{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} -- | The program will parse ~/.halcyonrc for any available configuration -- directives, resulting in an OptionalCfg. The command-line -- arguments will be used to create another OptionalCfg, and the two -- will be merged. Finally, a default_config will be updated from -- the merged OptionalCfgs. -- module OptionalConfiguration ( OptionalCfg(..), from_rc ) where import qualified Data.Configurator as DC ( Worth (Optional), load, lookup ) import Data.Data ( Data ) import Data.Maybe ( fromMaybe ) import Data.Monoid ( Monoid(..) ) import Data.Typeable ( Typeable ) import Paths_halcyon ( getSysconfDir ) import System.Directory ( getHomeDirectory ) import System.FilePath ( () ) import System.IO ( hPutStrLn, stderr ) import System.IO.Error ( catchIOError ) import Usernames ( Usernames(..) ) -- | The same as Cfg, except everything is optional. It's easy to -- merge two of these by simply dropping the Nothings in favor of -- the Justs. The 'usernames' are left un-maybed so that cmdargs -- can parse more than one of them. -- data OptionalCfg = OptionalCfg { consumer_key :: Maybe String, consumer_secret :: Maybe String, access_token :: Maybe String, access_secret :: Maybe String, heartbeat :: Maybe Int, ignore_replies :: Maybe Bool, ignore_retweets :: Maybe Bool, sendmail_path :: Maybe String, from_address :: Maybe String, to_address :: Maybe String, verbose :: Maybe Bool, usernames :: Usernames } deriving (Show, Data, Typeable) instance Monoid OptionalCfg where mempty = OptionalCfg Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing (Usernames []) cfg1 `mappend` cfg2 = OptionalCfg (merge (consumer_key cfg1) (consumer_key cfg2)) (merge (consumer_secret cfg1) (consumer_secret cfg2)) (merge (access_token cfg1) (access_token cfg2)) (merge (access_secret cfg1) (access_secret cfg2)) (merge (heartbeat cfg1) (heartbeat cfg2)) (merge (ignore_replies cfg1) (ignore_replies cfg2)) (merge (ignore_retweets cfg1) (ignore_retweets cfg2)) (merge (sendmail_path cfg1) (sendmail_path cfg2)) (merge (from_address cfg1) (from_address cfg2)) (merge (to_address cfg1) (to_address cfg2)) (merge (verbose cfg1) (verbose cfg2)) all_usernames 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 usernames if there are any. all_usernames = usernames $ if (null (get_usernames (usernames cfg2))) then cfg1 else cfg2 -- | Obtain an 'OptionalCfg' from halcyonrc in either the global -- configuration directory or the user's home directory. The one in -- $HOME is prefixed by a dot so that it is hidden. -- -- We make an attempt at cross-platform compatibility; we will try -- to find the correct directory even on Windows. But if the calls -- to getHomeDirectory/getSysconfDir fail for whatever reason, we -- fall back to using the Unix-specific /etc and $HOME. -- from_rc :: IO OptionalCfg from_rc = do etc <- catchIOError getSysconfDir (\e -> do hPutStrLn stderr (show e) return "/etc") home <- catchIOError getHomeDirectory (\e -> do hPutStrLn stderr (show e) return "$(HOME)") let global_config_path = etc "halcyonrc" let user_config_path = home ".halcyonrc" cfg <- DC.load [ DC.Optional global_config_path, DC.Optional user_config_path ] cfg_consumer_key <- DC.lookup cfg "consumer-key" cfg_consumer_secret <- DC.lookup cfg "consumer-secret" cfg_access_token <- DC.lookup cfg "access-token" cfg_access_secret <- DC.lookup cfg "access-secret" cfg_heartbeat <- DC.lookup cfg "heartbeat" cfg_ignore_replies <- DC.lookup cfg "ignore-replies" cfg_ignore_retweets <- DC.lookup cfg "ignore-retweets" cfg_sendmail_path <- DC.lookup cfg "sendmail-path" cfg_from_address <- DC.lookup cfg "from" cfg_to_address <- DC.lookup cfg "to" cfg_verbose <- DC.lookup cfg "verbose" cfg_usernames <- DC.lookup cfg "usernames" return $ OptionalCfg cfg_consumer_key cfg_consumer_secret cfg_access_token cfg_access_secret cfg_heartbeat cfg_ignore_replies cfg_ignore_retweets cfg_sendmail_path cfg_from_address cfg_to_address cfg_verbose (fromMaybe (Usernames []) cfg_usernames)