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