]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Configuration.hs
Add more code documentation.
[dead/htsn.git] / src / 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 qualified OptionalConfiguration as OC (
14 OptionalConfiguration(..),
15 merge_maybes )
16 import TSN.FeedHosts (FeedHosts(..))
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 daemonize :: Bool,
23 feed_hosts :: FeedHosts,
24 log_file :: Maybe FilePath,
25 log_level :: Priority,
26 output_directory :: FilePath,
27 password :: String,
28 pidfile :: FilePath,
29 run_as_group :: Maybe String,
30 run_as_user :: Maybe String,
31 syslog :: Bool,
32 username :: String }
33 deriving (Show)
34
35 -- | A Configuration with all of its fields set to their default
36 -- values.
37 instance Default Configuration where
38 def = Configuration {
39 daemonize = def,
40 feed_hosts = def,
41 log_file = def,
42 log_level = INFO,
43 output_directory = ".",
44 password = def,
45 pidfile = "/run/htsn.pid",
46 run_as_group = def,
47 run_as_user = def,
48 syslog = def,
49 username = def }
50
51
52 -- | Merge a Configuration with an OptionalConfiguration. This is more
53 -- or less the Monoid instance for OptionalConfiguration, but since
54 -- the two types are different, we have to repeat ourselves.
55 merge_optional :: Configuration
56 -> OC.OptionalConfiguration
57 -> Configuration
58 merge_optional cfg opt_cfg =
59 Configuration
60 (merge (daemonize cfg) (OC.daemonize opt_cfg))
61 all_feed_hosts
62 (OC.merge_maybes (log_file cfg) (OC.log_file opt_cfg))
63 (merge (log_level cfg) (OC.log_level opt_cfg))
64 (merge (output_directory cfg) (OC.output_directory opt_cfg))
65 (merge (password cfg) (OC.password opt_cfg))
66 (merge (pidfile cfg) (OC.pidfile opt_cfg))
67 (OC.merge_maybes (run_as_group cfg) (OC.run_as_group opt_cfg))
68 (OC.merge_maybes (run_as_user cfg) (OC.run_as_user opt_cfg))
69 (merge (syslog cfg) (OC.syslog opt_cfg))
70 (merge (username cfg) (OC.username opt_cfg))
71 where
72 -- | If the thing on the right is Just something, return that
73 -- something, otherwise return the thing on the left.
74 merge :: a -> Maybe a -> a
75 merge x Nothing = x
76 merge _ (Just y) = y
77
78 -- If there are any optional usernames, use only those.
79 all_feed_hosts = if (null (get_feed_hosts (OC.feed_hosts opt_cfg)))
80 then (feed_hosts cfg)
81 else (OC.feed_hosts opt_cfg)
82