1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StandaloneDeriving #-}
6 -- | An OptionalConfiguration is just like a 'Configuration', except
7 -- all of its fields are optional. The user can set options in two
8 -- places: the command-line, and a configuration file. Obviously if
9 -- a parameter is set in one place, it doesn't need to be set in the
10 -- other. Thus, the latter needs to be optional.
13 module OptionalConfiguration (
14 OptionalConfiguration(..),
19 import qualified Data.Configurator as DC (
23 import qualified Data.Configurator.Types as DCT (
27 import Data.Data ( Data )
28 import Data.Maybe ( fromMaybe )
29 import Data.Monoid ( Monoid(..) )
30 import Data.Typeable ( Typeable )
31 import Paths_htsn ( getSysconfDir )
32 import System.Directory ( getHomeDirectory )
33 import System.FilePath ( (</>) )
34 import System.IO.Error ( catchIOError )
35 import System.Log ( Priority(..) )
37 import FeedHosts ( FeedHosts(..) )
38 import Network.Services.TSN.Report ( report_error )
41 -- Derive standalone instances of Data and Typeable for Priority. This
42 -- is necessary for OptionalConfiguration (which contains a Maybe
43 -- Priority) to derive Data and Typeable.
44 deriving instance Data Priority
45 deriving instance Typeable Priority
47 -- | The same as Configuration, except everything is optional. It's easy to
48 -- merge two of these by simply dropping the Nothings in favor of
49 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
50 -- can parse more than one of them.
52 data OptionalConfiguration =
53 OptionalConfiguration {
54 daemonize :: Maybe Bool,
55 feed_hosts :: FeedHosts,
56 log_file :: Maybe FilePath,
57 log_level :: Maybe Priority,
58 output_directory :: Maybe FilePath,
59 password :: Maybe String,
60 pidfile :: Maybe FilePath,
61 run_as_group :: Maybe String,
62 run_as_user :: Maybe String,
64 username :: Maybe String }
65 deriving (Show, Data, Typeable)
68 -- | Combine two Maybes into one, essentially mashing them
69 -- together. We give precedence to the second argument when both are
71 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
72 merge_maybes Nothing Nothing = Nothing
73 merge_maybes (Just x) Nothing = Just x
74 merge_maybes Nothing (Just x) = Just x
75 merge_maybes (Just _) (Just y) = Just y
78 -- | The Monoid instance for these lets us "combine" two
79 -- OptionalConfigurations. The "combine" operation that we'd like to
80 -- perform is, essentially, to mash them together. So if we have two
81 -- OptionalConfigurations, each half full, we could combine them
84 -- This is used to merge command-line and config-file settings.
86 instance Monoid OptionalConfiguration where
87 -- | An empty OptionalConfiguration.
88 mempty = OptionalConfiguration
102 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
103 cfg1 `mappend` cfg2 =
104 OptionalConfiguration
105 (merge_maybes (daemonize cfg1) (daemonize cfg2))
107 (merge_maybes (log_file cfg1) (log_file cfg2))
108 (merge_maybes (log_level cfg1) (log_level cfg2))
109 (merge_maybes (output_directory cfg1) (output_directory cfg2))
110 (merge_maybes (password cfg1) (password cfg2))
111 (merge_maybes (pidfile cfg1) (pidfile cfg2))
112 (merge_maybes (run_as_group cfg1) (run_as_group cfg2))
113 (merge_maybes (run_as_user cfg1) (run_as_user cfg2))
114 (merge_maybes (syslog cfg1) (syslog cfg2))
115 (merge_maybes (username cfg1) (username cfg2))
117 -- Use only the latter feed_hosts if there are any.
119 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
124 instance DCT.Configured Priority where
125 -- | This allows us to read a Priority level out of a Configurator
126 -- config file. By default Configurator wouldn't know what to do,
127 -- so we have to tell it that we expect one of the valid Priority
129 convert (DCT.String "INFO") = Just INFO
130 convert (DCT.String "WARNING") = Just WARNING
131 convert (DCT.String "ERROR") = Just ERROR
135 -- | Obtain an OptionalConfiguration from htsnrc in either the global
136 -- configuration directory or the user's home directory. The one in
137 -- $HOME is prefixed by a dot so that it is hidden.
139 -- We make an attempt at cross-platform compatibility; we will try
140 -- to find the correct directory even on Windows. But if the calls
141 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
142 -- fall back to using the Unix-specific /etc and $HOME.
144 from_rc :: IO OptionalConfiguration
146 etc <- catchIOError getSysconfDir (\e -> do
147 report_error (show e)
149 home <- catchIOError getHomeDirectory (\e -> do
150 report_error (show e)
152 let global_config_path = etc </> "htsnrc"
153 let user_config_path = home </> ".htsnrc"
154 cfg <- DC.load [ DC.Optional global_config_path,
155 DC.Optional user_config_path ]
156 cfg_daemonize <- DC.lookup cfg "daemonize"
157 cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
158 cfg_log_file <- DC.lookup cfg "log_file"
159 cfg_log_level <- DC.lookup cfg "log_level"
160 cfg_output_directory <- DC.lookup cfg "output_directory"
161 cfg_password <- DC.lookup cfg "password"
162 cfg_pidfile <- DC.lookup cfg "pidfile"
163 cfg_run_as_group <- DC.lookup cfg "run_as_group"
164 cfg_run_as_user <- DC.lookup cfg "run_as_user"
165 cfg_syslog <- DC.lookup cfg "syslog"
166 cfg_username <- DC.lookup cfg "username"
168 return $ OptionalConfiguration
170 (fromMaybe (FeedHosts []) cfg_feed_hosts)