1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE OverloadedStrings #-}
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.
11 module OptionalConfiguration (
12 OptionalConfiguration(..),
17 import qualified Data.Configurator as DC (
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 )
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.
35 data OptionalConfiguration =
36 OptionalConfiguration {
37 database :: Maybe String,
39 detail_query :: Maybe String,
41 password :: Maybe String,
43 summary_query :: Maybe String,
44 username :: Maybe String }
48 -- | Combine two Maybes into one, essentially mashing them
49 -- together. We give precedence to the second argument when both are
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
58 -- | The Semigroup instance for these lets us "combine" two
59 -- configurations. The "combine" operation that we'd like to perform
60 -- is, essentially, to mash them together. So if we have two
61 -- OptionalConfigurations, each half full, we could combine them
64 -- This is used to merge command-line and config-file settings.
66 instance Semigroup OptionalConfiguration where
67 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
70 (merge_maybes (database cfg1) (database cfg2))
71 (merge_maybes (detail cfg1) (detail cfg2))
72 (merge_maybes (detail_query cfg1) (detail_query cfg2))
73 (merge_maybes (host cfg1) (host cfg2))
74 (merge_maybes (password cfg1) (password cfg2))
75 (merge_maybes (port cfg1) (port cfg2))
76 (merge_maybes (summary_query cfg1) (summary_query cfg2))
77 (merge_maybes (username cfg1) (username cfg2))
80 -- | The Monoid instance essentially only provides the "empty
82 instance Monoid OptionalConfiguration where
83 -- | An empty OptionalConfiguration.
84 mempty = OptionalConfiguration
96 -- | Obtain an OptionalConfiguration from mailbox-countrc in either
97 -- the global configuration directory or the user's home
98 -- directory. The one in $HOME is prefixed by a dot so that it is
101 -- We make an attempt at cross-platform compatibility; we will try
102 -- to find the correct directory even on Windows. But if the calls
103 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
104 -- fall back to using the Unix-specific /etc and $HOME.
106 from_rc :: IO OptionalConfiguration
108 etc <- catchIOError getSysconfDir (\e -> do
109 putStrLn $ "ERROR: " ++ (show e)
111 home <- catchIOError getHomeDirectory (\e -> do
112 putStrLn $ "ERROR: " ++ (show e)
114 let global_config_path = etc </> "mailbox-countrc"
115 let user_config_path = home </> ".mailbox-countrc"
116 cfg <- DC.load [ DC.Optional global_config_path,
117 DC.Optional user_config_path ]
118 cfg_database <- DC.lookup cfg "database"
119 cfg_detail <- DC.lookup cfg "detail"
120 cfg_detail_query <- DC.lookup cfg "detail_query"
121 cfg_host <- DC.lookup cfg "host"
122 cfg_password <- DC.lookup cfg "password"
123 cfg_port <- DC.lookup cfg "port"
124 cfg_summary_query <- DC.lookup cfg "summary_query"
125 cfg_username <- DC.lookup cfg "username"
127 return $ OptionalConfiguration