{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} -- | An OptionalConfiguration is just like a 'Configuration', except -- all of its fields are optional. The user can set options in two -- places: the command-line, and a configuration file. Obviously if -- a parameter is set in one place, it doesn't need to be set in the -- other. Thus, the latter needs to be optional. -- module OptionalConfiguration ( OptionalConfiguration(..), 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 System.Directory (getHomeDirectory) import System.FilePath ( () ) import System.IO.Error (catchIOError) import Terminal (report_error) import TSN.FeedHosts (FeedHosts(..)) -- | The same as Configuration, except everything is optional. It's easy to -- merge two of these by simply dropping the Nothings in favor of -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs -- can parse more than one of them. -- data OptionalConfiguration = OptionalConfiguration { feed_hosts :: FeedHosts, password :: Maybe String, output_directory :: Maybe FilePath, username :: Maybe String } deriving (Show, Data, Typeable) -- | 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 (FeedHosts []) Nothing Nothing Nothing -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@. cfg1 `mappend` cfg2 = OptionalConfiguration all_feed_hosts (merge (password cfg1) (password cfg2)) (merge (output_directory cfg1) (output_directory cfg2)) (merge (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))) then cfg1 else cfg2 -- | Obtain an OptionalConfiguration from the file ".htsnrc" in the -- user's home directory. -- -- We make an attempt at cross-platform compatibility; we will try -- to find the correct directory even on Windows. But if the call -- to getHomeDirectory fails for whatever reason, we fall back to -- using the environment variable $HOME. -- from_rc :: IO OptionalConfiguration from_rc = do -- After some thought, the "global" /etc/htsnrc configuration file -- was left out. Since each config file needs a password, and this -- should be run by a dedicated user anyway, the global file does -- not serve much purpose. It could also be a security risk (visible -- password) if the admin screws up. home <- catchIOError getHomeDirectory (\e -> do report_error (show e) return "$(HOME)") let user_config_path = home ".htsnrc" cfg <- DC.load [ DC.Optional user_config_path ] cfg_password <- DC.lookup cfg "password" cfg_output_directory <- DC.lookup cfg "output_directory" cfg_username <- DC.lookup cfg "username" cfg_feed_hosts <- DC.lookup cfg "feed_hosts" return $ OptionalConfiguration (fromMaybe (FeedHosts []) cfg_feed_hosts) cfg_password cfg_output_directory cfg_username