{-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE StandaloneDeriving #-} -- | 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, merge_maybes ) where -- System imports. import qualified Data.Configurator as DC ( Worth(Optional), load, lookup ) import qualified Data.Configurator.Types as DCT ( Configured, Value( String ), convert ) import Data.Data ( Data ) import Data.Monoid ( Monoid(..) ) import Data.Typeable ( Typeable ) import Paths_htsn_import ( getSysconfDir ) import System.Directory ( getHomeDirectory ) import System.FilePath ( () ) import System.IO.Error ( catchIOError ) import System.Log ( Priority(..) ) import Text.Read ( readMaybe ) -- Local imports. import Backend ( Backend(..) ) import ConnectionString ( ConnectionString ) import Network.Services.TSN.Report ( report_error ) -- Derive standalone instances of Data and Typeable for Priority. This -- is necessary for OptionalConfiguration (which contains a Maybe -- Priority) to derive Data and Typeable. deriving instance Data Priority deriving instance Typeable Priority -- | The same as 'Configuration', except everything is optional. It's -- easy to merge two of these by simply dropping the 'Nothing's in -- favor of the 'Just's. The 'xml_files' are left un-maybed so that -- cmdargs can parse more than one of them. -- data OptionalConfiguration = OptionalConfiguration { backend :: Maybe Backend, connection_string :: Maybe ConnectionString, log_file :: Maybe FilePath, log_level :: Maybe Priority, remove :: Maybe Bool, syslog :: Maybe Bool, xml_files :: [FilePath] } 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. -- -- This is used to merge command-line and config-file settings. -- instance Monoid OptionalConfiguration where -- | An empty OptionalConfiguration. mempty = OptionalConfiguration Nothing Nothing Nothing Nothing Nothing Nothing [] -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@. -- XML files can only be specified on the command-line, so we -- just join them together here. cfg1 `mappend` cfg2 = OptionalConfiguration (merge_maybes (backend cfg1) (backend cfg2)) (merge_maybes (connection_string cfg1) (connection_string cfg2)) (merge_maybes (log_file cfg1) (log_file cfg2)) (merge_maybes (log_level cfg1) (log_level cfg2)) (merge_maybes (remove cfg1) (remove cfg2)) (merge_maybes (syslog cfg1) (syslog cfg2)) ((xml_files cfg1) ++ (xml_files cfg2)) instance DCT.Configured Priority where -- | This allows us to read a Priority level out of a Configurator -- config file. By default Configurator wouldn't know what to do, -- so we have to tell it that we expect one of the valid Priority -- constructors. convert (DCT.String "INFO") = Just INFO convert (DCT.String "WARNING") = Just WARNING convert (DCT.String "ERROR") = Just ERROR convert _ = Nothing -- | Obtain an OptionalConfiguration from htsn-importrc 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 OptionalConfiguration from_rc = do etc <- catchIOError getSysconfDir (\e -> do report_error (show e) return "/etc") home <- catchIOError getHomeDirectory (\e -> do report_error (show e) return "$(HOME)") let global_config_path = etc "htsn-importrc" let user_config_path = home ".htsn-importrc" cfg <- DC.load [ DC.Optional global_config_path, DC.Optional user_config_path ] cfg_backend <- DC.lookup cfg "backend" cfg_connection_string <- DC.lookup cfg "connection_string" cfg_log_file <- DC.lookup cfg "log_file" cfg_log_level <- DC.lookup cfg "log_level" cfg_remove <- DC.lookup cfg "remove" cfg_syslog <- DC.lookup cfg "syslog" let cfg_xml_files = [] -- This won't be in the config file. return $ OptionalConfiguration (case cfg_backend of -- Try to convert a String to a Backend. Nothing -> Nothing Just s -> readMaybe s) cfg_connection_string cfg_log_file cfg_log_level cfg_remove cfg_syslog cfg_xml_files