]> gitweb.michael.orlitzky.com - mailbox-count.git/blob - src/Configuration.hs
mailbox-count.cabal: update to v0.0.6.
[mailbox-count.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( def ) )
11
12 import qualified OptionalConfiguration as OC (
13 OptionalConfiguration(..),
14 merge_maybes )
15
16 -- | The main configuration data type. This will be passed to most of
17 -- the important functions once it has been created.
18 --
19 data Configuration =
20 Configuration {
21 database :: Maybe String,
22 detail :: Bool,
23 detail_query :: String,
24 host :: Maybe String,
25 password :: Maybe String,
26 port :: Maybe Int,
27 summary_query :: String,
28 username :: Maybe String }
29 deriving (Show)
30
31
32 -- | A Configuration with all of its fields set to their default
33 -- values.
34 --
35 instance Default Configuration where
36 def = Configuration {
37 database = def,
38 detail = def,
39 detail_query = def_detail_query,
40 host = def,
41 password = def,
42 port = def,
43 summary_query = def_summary_query,
44 username = def }
45 where
46 def_summary_query = "SELECT domain,COUNT(username) " ++
47 "FROM mailbox " ++
48 "GROUP BY domain "++
49 "ORDER BY domain;"
50
51 def_detail_query = "SELECT domain,username " ++
52 "FROM mailbox " ++
53 "ORDER BY domain;"
54
55 -- | Merge a 'Configuration' with an 'OptionalConfiguration'. This is
56 -- more or less the Monoid instance for 'OptionalConfiguration', but
57 -- since the two types are different, we have to repeat ourselves.
58 --
59 merge_optional :: Configuration
60 -> OC.OptionalConfiguration
61 -> Configuration
62 merge_optional cfg opt_cfg =
63 Configuration
64 (OC.merge_maybes (database cfg) (OC.database opt_cfg))
65 (merge (detail cfg) (OC.detail opt_cfg))
66 (merge (detail_query cfg) (OC.detail_query opt_cfg))
67 (OC.merge_maybes (host cfg) (OC.host opt_cfg))
68 (OC.merge_maybes (password cfg) (OC.password opt_cfg))
69 (OC.merge_maybes (port cfg) (OC.port opt_cfg))
70 (merge (summary_query cfg) (OC.summary_query opt_cfg))
71 (OC.merge_maybes (username cfg) (OC.username opt_cfg))
72 where
73 -- | If the thing on the right is Just something, return that
74 -- something, otherwise return the thing on the left.
75 merge :: a -> Maybe a -> a
76 merge x Nothing = x
77 merge _ (Just y) = y