]> gitweb.michael.orlitzky.com - mailbox-count.git/blob - src/OptionalConfiguration.hs
src/OptionalConfiguration.hs: make OptionalConfiguration.hs a Semigroup.
[mailbox-count.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
3
4 -- | An OptionalConfiguration is just like a 'Configuration', except
5 -- all of its fields are optional. The user can set options in two
6 -- places: the command-line, and a configuration file. Obviously if
7 -- a parameter is set in one place, it doesn't need to be set in the
8 -- other. Thus, the latter needs to be optional.
9 --
10
11 module OptionalConfiguration (
12 OptionalConfiguration(..),
13 from_rc,
14 merge_maybes )
15 where
16
17 import qualified Data.Configurator as DC (
18 Worth(Optional),
19 load,
20 lookup )
21 import Data.Data ( Data )
22 import Data.Typeable ( Typeable )
23 import Data.Monoid ( Monoid(..) )
24 import Data.Semigroup ( Semigroup( (<>) ) )
25 import Paths_mailbox_count ( getSysconfDir )
26 import System.Directory ( getHomeDirectory )
27 import System.FilePath ( (</>) )
28 import System.IO.Error ( catchIOError )
29
30
31 -- | The same as Configuration, except everything is optional. It's easy to
32 -- merge two of these by simply dropping the Nothings in favor of
33 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
34 -- can parse more than one of them.
35 --
36 data OptionalConfiguration =
37 OptionalConfiguration {
38 database :: Maybe String,
39 detail :: Maybe Bool,
40 detail_query :: Maybe String,
41 host :: Maybe String,
42 password :: Maybe String,
43 port :: Maybe Int,
44 summary_query :: Maybe String,
45 username :: Maybe String }
46 deriving (Show, Data, Typeable)
47
48
49 -- | Combine two Maybes into one, essentially mashing them
50 -- together. We give precedence to the second argument when both are
51 -- Justs.
52 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
53 merge_maybes Nothing Nothing = Nothing
54 merge_maybes (Just x) Nothing = Just x
55 merge_maybes Nothing (Just x) = Just x
56 merge_maybes (Just _) (Just y) = Just y
57
58
59 -- | The Monoid instance for these lets us "combine" two
60 -- OptionalConfigurations. The "combine" operation that we'd like to
61 -- perform is, essentially, to mash them together. So if we have two
62 -- OptionalConfigurations, each half full, we could combine them
63 -- into one big one.
64 --
65 -- This is used to merge command-line and config-file settings.
66 --
67 instance Monoid OptionalConfiguration where
68 -- | An empty OptionalConfiguration.
69 mempty = OptionalConfiguration
70 Nothing
71 Nothing
72 Nothing
73 Nothing
74 Nothing
75 Nothing
76 Nothing
77 Nothing
78
79 instance Semigroup OptionalConfiguration where
80 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
81 cfg1 <> cfg2 =
82 OptionalConfiguration
83 (merge_maybes (database cfg1) (database cfg2))
84 (merge_maybes (detail cfg1) (detail cfg2))
85 (merge_maybes (detail_query cfg1) (detail_query cfg2))
86 (merge_maybes (host cfg1) (host cfg2))
87 (merge_maybes (password cfg1) (password cfg2))
88 (merge_maybes (port cfg1) (port cfg2))
89 (merge_maybes (summary_query cfg1) (summary_query cfg2))
90 (merge_maybes (username cfg1) (username cfg2))
91
92
93 -- | Obtain an OptionalConfiguration from mailbox-countrc in either
94 -- the global configuration directory or the user's home
95 -- directory. The one in $HOME is prefixed by a dot so that it is
96 -- hidden.
97 --
98 -- We make an attempt at cross-platform compatibility; we will try
99 -- to find the correct directory even on Windows. But if the calls
100 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
101 -- fall back to using the Unix-specific /etc and $HOME.
102 --
103 from_rc :: IO OptionalConfiguration
104 from_rc = do
105 etc <- catchIOError getSysconfDir (\e -> do
106 putStrLn $ "ERROR: " ++ (show e)
107 return "/etc")
108 home <- catchIOError getHomeDirectory (\e -> do
109 putStrLn $ "ERROR: " ++ (show e)
110 return "$(HOME)")
111 let global_config_path = etc </> "mailbox-countrc"
112 let user_config_path = home </> ".mailbox-countrc"
113 cfg <- DC.load [ DC.Optional global_config_path,
114 DC.Optional user_config_path ]
115 cfg_database <- DC.lookup cfg "database"
116 cfg_detail <- DC.lookup cfg "detail"
117 cfg_detail_query <- DC.lookup cfg "detail_query"
118 cfg_host <- DC.lookup cfg "host"
119 cfg_password <- DC.lookup cfg "password"
120 cfg_port <- DC.lookup cfg "port"
121 cfg_summary_query <- DC.lookup cfg "summary_query"
122 cfg_username <- DC.lookup cfg "username"
123
124 return $ OptionalConfiguration
125 cfg_database
126 cfg_detail
127 cfg_detail_query
128 cfg_host
129 cfg_password
130 cfg_port
131 cfg_summary_query
132 cfg_username