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 Logging ( log_error ) -- Can't import report_error from Main
38 import Terminal ( display_error ) -- 'cause of circular imports.
39 import TSN.FeedHosts ( FeedHosts(..) )
42 -- Derive standalone instances of Data and Typeable for Priority. This
43 -- is necessary for OptionalConfiguration (which contains a Maybe
44 -- Priority) to derive Data and Typeable.
45 deriving instance Data Priority
46 deriving instance Typeable Priority
48 -- | The same as Configuration, except everything is optional. It's easy to
49 -- merge two of these by simply dropping the Nothings in favor of
50 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
51 -- can parse more than one of them.
53 data OptionalConfiguration =
54 OptionalConfiguration {
55 daemonize :: Maybe Bool,
56 feed_hosts :: FeedHosts,
57 log_file :: Maybe FilePath,
58 log_level :: Maybe Priority,
59 output_directory :: Maybe FilePath,
60 password :: Maybe String,
61 pidfile :: Maybe FilePath,
62 run_as_group :: Maybe String,
63 run_as_user :: Maybe String,
65 username :: Maybe String }
66 deriving (Show, Data, Typeable)
69 -- | Combine two Maybes into one, essentially mashing them
70 -- together. We give precedence to the second argument when both are
72 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
73 merge_maybes Nothing Nothing = Nothing
74 merge_maybes (Just x) Nothing = Just x
75 merge_maybes Nothing (Just x) = Just x
76 merge_maybes (Just _) (Just y) = Just y
79 -- | The Monoid instance for these lets us "combine" two
80 -- OptionalConfigurations. The "combine" operation that we'd like to
81 -- perform is, essentially, to mash them together. So if we have two
82 -- OptionalConfigurations, each half full, we could combine them
85 -- This is used to merge command-line and config-file settings.
87 instance Monoid OptionalConfiguration where
88 -- | An empty OptionalConfiguration.
89 mempty = OptionalConfiguration
103 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
104 cfg1 `mappend` cfg2 =
105 OptionalConfiguration
106 (merge_maybes (daemonize cfg1) (daemonize cfg2))
108 (merge_maybes (log_file cfg1) (log_file cfg2))
109 (merge_maybes (log_level cfg1) (log_level cfg2))
110 (merge_maybes (output_directory cfg1) (output_directory cfg2))
111 (merge_maybes (password cfg1) (password cfg2))
112 (merge_maybes (pidfile cfg1) (pidfile cfg2))
113 (merge_maybes (run_as_group cfg1) (run_as_group cfg2))
114 (merge_maybes (run_as_user cfg1) (run_as_user cfg2))
115 (merge_maybes (syslog cfg1) (syslog cfg2))
116 (merge_maybes (username cfg1) (username cfg2))
118 -- Use only the latter feed_hosts if there are any.
120 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
125 instance DCT.Configured Priority where
126 -- | This allows us to read a Priority level out of a Configurator
127 -- config file. By default Configurator wouldn't know what to do,
128 -- so we have to tell it that we expect one of the valid Priority
130 convert (DCT.String "INFO") = Just INFO
131 convert (DCT.String "WARNING") = Just WARNING
132 convert (DCT.String "ERROR") = Just ERROR
136 -- | Obtain an OptionalConfiguration from htsnrc in either the global
137 -- configuration directory or the user's home directory. The one in
138 -- $HOME is prefixed by a dot so that it is hidden.
140 -- We make an attempt at cross-platform compatibility; we will try
141 -- to find the correct directory even on Windows. But if the calls
142 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
143 -- fall back to using the Unix-specific /etc and $HOME.
145 from_rc :: IO OptionalConfiguration
147 etc <- catchIOError getSysconfDir (\e -> do
148 display_error (show e)
151 home <- catchIOError getHomeDirectory (\e -> do
152 display_error (show e)
155 let global_config_path = etc </> "htsnrc"
156 let user_config_path = home </> ".htsnrc"
157 cfg <- DC.load [ DC.Optional global_config_path,
158 DC.Optional user_config_path ]
159 cfg_daemonize <- DC.lookup cfg "daemonize"
160 cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
161 cfg_log_file <- DC.lookup cfg "log_file"
162 cfg_log_level <- DC.lookup cfg "log_level"
163 cfg_output_directory <- DC.lookup cfg "output_directory"
164 cfg_password <- DC.lookup cfg "password"
165 cfg_pidfile <- DC.lookup cfg "pidfile"
166 cfg_run_as_group <- DC.lookup cfg "run_as_group"
167 cfg_run_as_user <- DC.lookup cfg "run_as_user"
168 cfg_syslog <- DC.lookup cfg "syslog"
169 cfg_username <- DC.lookup cfg "username"
171 return $ OptionalConfiguration
173 (fromMaybe (FeedHosts []) cfg_feed_hosts)