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