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