]> gitweb.michael.orlitzky.com - list-remote-forwards.git/blob - src/Configuration.hs
Fix slashes in cabal description.
[list-remote-forwards.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
12 import MxList (MxList(..))
13 import qualified OptionalConfiguration as OC (
14 OptionalConfiguration(..),
15 merge_maybes )
16
17 -- | The main configuration data type. This will be passed to most of
18 -- the important functions once it has been created.
19 --
20 data Configuration =
21 Configuration {
22 database :: Maybe String,
23 domain_query :: String,
24 exclude_mx :: MxList,
25 forward_query :: String,
26 host :: Maybe String,
27 password :: Maybe String,
28 port :: Maybe Int,
29 username :: Maybe String }
30 deriving (Show)
31
32
33 -- | A Configuration with all of its fields set to their default
34 -- values.
35 --
36 instance Default Configuration where
37 def = Configuration {
38 database = def,
39 domain_query = def_domain_query,
40 exclude_mx = def,
41 forward_query = def_forward_query,
42 host = def,
43 password = def,
44 port = def,
45 username = def }
46 where
47 def_domain_query = "SELECT domain " ++
48 "FROM domain " ++
49 "WHERE domain <> 'ALL' " ++
50 "ORDER BY domain;"
51
52 def_forward_query = "SELECT address,goto " ++
53 "FROM alias " ++
54 "ORDER BY address;"
55
56 -- | Merge a 'Configuration' with an 'OptionalConfiguration'. This is
57 -- more or less the Monoid instance for 'OptionalConfiguration', but
58 -- since the two types are different, we have to repeat ourselves.
59 --
60 merge_optional :: Configuration
61 -> OC.OptionalConfiguration
62 -> Configuration
63 merge_optional cfg opt_cfg =
64 Configuration
65 (OC.merge_maybes (database cfg) (OC.database opt_cfg))
66 (merge (domain_query cfg) (OC.domain_query opt_cfg))
67 all_excluded_mx
68 (merge (forward_query cfg) (OC.forward_query opt_cfg))
69 (OC.merge_maybes (host cfg) (OC.host opt_cfg))
70 (OC.merge_maybes (password cfg) (OC.password opt_cfg))
71 (OC.merge_maybes (port cfg) (OC.port opt_cfg))
72 (OC.merge_maybes (username cfg) (OC.username opt_cfg))
73 where
74 -- | If the thing on the right is Just something, return that
75 -- something, otherwise return the thing on the left.
76 merge :: a -> Maybe a -> a
77 merge x Nothing = x
78 merge _ (Just y) = y
79
80 -- If there are any optional usernames, use only those.
81 all_excluded_mx = if (null (get_mxs (OC.exclude_mx opt_cfg)))
82 then (exclude_mx cfg)
83 else (OC.exclude_mx opt_cfg)