]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - Configuration.hs
d56cc936e6fc6983ae94ea78cc57885de27cbe54
[dead/htsn-import.git] / Configuration.hs
1 -- | This module defines the 'Configuration' type, which is just a
2 -- wrapper around all of the configuration options we accept on the
3 -- command line.
4 --
5 module Configuration (
6 Configuration(..),
7 merge_optional )
8 where
9
10 import System.Console.CmdArgs.Default ( Default(..) )
11 import System.Log ( Priority( INFO ) )
12
13 import Backend ( Backend(..) )
14 import qualified OptionalConfiguration as OC (
15 OptionalConfiguration(..),
16 merge_maybes )
17
18 -- | The main configuration data type. This will be passed to most of
19 -- the important functions once it has been created.
20 data Configuration =
21 Configuration {
22 backend :: Backend,
23 connection_string :: String,
24 log_file :: Maybe FilePath,
25 log_level :: Priority,
26 syslog :: Bool }
27 deriving (Show)
28
29 -- | A Configuration with all of its fields set to their default
30 -- values.
31 instance Default Configuration where
32 def = Configuration {
33 backend = def,
34 connection_string = def,
35 log_file = def,
36 log_level = INFO,
37 syslog = def }
38
39
40 -- | Merge a Configuration with an OptionalConfiguration. This is more
41 -- or less the Monoid instance for OptionalConfiguration, but since
42 -- the two types are different, we have to repeat ourselves.
43 merge_optional :: Configuration
44 -> OC.OptionalConfiguration
45 -> Configuration
46 merge_optional cfg opt_cfg =
47 Configuration
48 (merge (backend cfg) (OC.backend opt_cfg))
49 (merge (connection_string cfg) (OC.connection_string opt_cfg))
50 (OC.merge_maybes (log_file cfg) (OC.log_file opt_cfg))
51 (merge (log_level cfg) (OC.log_level opt_cfg))
52 (merge (syslog cfg) (OC.syslog opt_cfg))
53 where
54 -- | If the thing on the right is Just something, return that
55 -- something, otherwise return the thing on the left.
56 merge :: a -> Maybe a -> a
57 merge x Nothing = x
58 merge _ (Just y) = y
59