]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/OptionalConfiguration.hs
33ca3d9d82e1061c467b3f25eee9e16745c5af4d
[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 Logging ( log_error ) -- Can't import report_error from Main
39 import Terminal ( display_error ) -- 'cause of circular imports.
40
41
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
47
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.
52 --
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,
64 syslog :: Maybe Bool,
65 username :: Maybe String }
66 deriving (Show, Data, Typeable)
67
68
69 -- | Combine two Maybes into one, essentially mashing them
70 -- together. We give precedence to the second argument when both are
71 -- Justs.
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
77
78
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
83 -- into one big one.
84 --
85 -- This is used to merge command-line and config-file settings.
86 --
87 instance Monoid OptionalConfiguration where
88 -- | An empty OptionalConfiguration.
89 mempty = OptionalConfiguration
90 Nothing
91 (FeedHosts [])
92 Nothing
93 Nothing
94 Nothing
95 Nothing
96 Nothing
97 Nothing
98 Nothing
99 Nothing
100 Nothing
101
102
103 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
104 cfg1 `mappend` cfg2 =
105 OptionalConfiguration
106 (merge_maybes (daemonize cfg1) (daemonize cfg2))
107 all_feed_hosts
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))
117 where
118 -- Use only the latter feed_hosts if there are any.
119 all_feed_hosts =
120 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
121 then cfg1
122 else cfg2
123
124
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
129 -- constructors.
130 convert (DCT.String "INFO") = Just INFO
131 convert (DCT.String "WARNING") = Just WARNING
132 convert (DCT.String "ERROR") = Just ERROR
133 convert _ = Nothing
134
135
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.
139 --
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.
144 --
145 from_rc :: IO OptionalConfiguration
146 from_rc = do
147 etc <- catchIOError getSysconfDir (\e -> do
148 display_error (show e)
149 log_error (show e)
150 return "/etc")
151 home <- catchIOError getHomeDirectory (\e -> do
152 display_error (show e)
153 log_error (show e)
154 return "$(HOME)")
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"
170
171 return $ OptionalConfiguration
172 cfg_daemonize
173 (fromMaybe (FeedHosts []) cfg_feed_hosts)
174 cfg_log_file
175 cfg_log_level
176 cfg_output_directory
177 cfg_password
178 cfg_pidfile
179 cfg_run_as_group
180 cfg_run_as_user
181 cfg_syslog
182 cfg_username