]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/Configuration.hs
a90af38435cbcd3df1fefd767f401203a4bfa169
[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 feed_hosts :: FeedHosts,
21 log_file :: Maybe FilePath,
22 log_level :: Priority,
23 password :: String,
24 output_directory :: FilePath,
25 syslog :: Bool,
26 username :: String }
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 def def INFO def "." def def
33
34
35 -- | Merge a Configuration with an OptionalConfiguration. This is more
36 -- or less the Monoid instance for OptionalConfiguration, but since
37 -- the two types are different, we have to repeat ourselves.
38 merge_optional :: Configuration
39 -> OC.OptionalConfiguration
40 -> Configuration
41 merge_optional cfg opt_cfg =
42 Configuration
43 all_feed_hosts
44 (OC.merge_maybes (log_file cfg) (OC.log_file opt_cfg))
45 (merge (log_level cfg) (OC.log_level opt_cfg))
46 (merge (password cfg) (OC.password opt_cfg))
47 (merge (output_directory cfg) (OC.output_directory opt_cfg))
48 (merge (syslog cfg) (OC.syslog opt_cfg))
49 (merge (username cfg) (OC.username opt_cfg))
50 where
51 -- | If the thing on the right is Just something, return that
52 -- something, otherwise return the thing on the left.
53 merge :: a -> Maybe a -> a
54 merge x Nothing = x
55 merge _ (Just y) = y
56
57 -- If there are any optional usernames, use only those.
58 all_feed_hosts = if (null (get_feed_hosts (OC.feed_hosts opt_cfg)))
59 then (feed_hosts cfg)
60 else (OC.feed_hosts opt_cfg)
61