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