]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/OptionalConfiguration.hs
Disable building with the threaded runtime.
[dead/htsn.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5
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.
11 --
12
13 module OptionalConfiguration (
14 OptionalConfiguration(..),
15 from_rc,
16 merge_maybes )
17 where
18
19 import qualified Data.Configurator as DC (
20 Worth(Optional),
21 load,
22 lookup )
23 import qualified Data.Configurator.Types as DCT (
24 Configured,
25 Value( String ),
26 convert )
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(..) )
36
37 import FeedHosts ( FeedHosts(..) )
38 import Network.Services.TSN.Report ( report_error )
39
40
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
46
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.
51 --
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,
63 syslog :: Maybe Bool,
64 username :: Maybe String }
65 deriving (Show, Data, Typeable)
66
67
68 -- | Combine two Maybes into one, essentially mashing them
69 -- together. We give precedence to the second argument when both are
70 -- Justs.
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
76
77
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
82 -- into one big one.
83 --
84 -- This is used to merge command-line and config-file settings.
85 --
86 instance Monoid OptionalConfiguration where
87 -- | An empty OptionalConfiguration.
88 mempty = OptionalConfiguration
89 Nothing
90 (FeedHosts [])
91 Nothing
92 Nothing
93 Nothing
94 Nothing
95 Nothing
96 Nothing
97 Nothing
98 Nothing
99 Nothing
100
101
102 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
103 cfg1 `mappend` cfg2 =
104 OptionalConfiguration
105 (merge_maybes (daemonize cfg1) (daemonize cfg2))
106 all_feed_hosts
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))
116 where
117 -- Use only the latter feed_hosts if there are any.
118 all_feed_hosts =
119 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
120 then cfg1
121 else cfg2
122
123
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
128 -- constructors.
129 convert (DCT.String "INFO") = Just INFO
130 convert (DCT.String "WARNING") = Just WARNING
131 convert (DCT.String "ERROR") = Just ERROR
132 convert _ = Nothing
133
134
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.
138 --
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.
143 --
144 from_rc :: IO OptionalConfiguration
145 from_rc = do
146 etc <- catchIOError getSysconfDir (\e -> do
147 report_error (show e)
148 return "/etc")
149 home <- catchIOError getHomeDirectory (\e -> do
150 report_error (show e)
151 return "$(HOME)")
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"
167
168 return $ OptionalConfiguration
169 cfg_daemonize
170 (fromMaybe (FeedHosts []) cfg_feed_hosts)
171 cfg_log_file
172 cfg_log_level
173 cfg_output_directory
174 cfg_password
175 cfg_pidfile
176 cfg_run_as_group
177 cfg_run_as_user
178 cfg_syslog
179 cfg_username