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 (
22 import Data.Maybe ( fromMaybe )
23 import Data.Data ( Data )
24 import Paths_list_remote_forwards ( getSysconfDir )
25 import System.Directory ( getHomeDirectory )
26 import System.FilePath ( (</>) )
27 import System.IO.Error ( catchIOError )
29 import MxList ( MxList(MxList, get_mxs) )
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.
36 data OptionalConfiguration =
37 OptionalConfiguration {
38 database :: Maybe String,
39 domain_query :: Maybe String,
41 forward_query :: Maybe String,
43 password :: Maybe String,
45 username :: Maybe String }
49 -- | Combine two Maybes into one, essentially mashing them
50 -- together. We give precedence to the second argument when both are
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
59 -- | The Semigroup 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
65 -- This is used to merge command-line and config-file settings.
68 instance Semigroup OptionalConfiguration where
69 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
72 (merge_maybes (database cfg1) (database cfg2))
73 (merge_maybes (domain_query cfg1) (domain_query cfg2))
75 (merge_maybes (forward_query cfg1) (forward_query cfg2))
76 (merge_maybes (host cfg1) (host cfg2))
77 (merge_maybes (password cfg1) (password cfg2))
78 (merge_maybes (port cfg1) (port cfg2))
79 (merge_maybes (username cfg1) (username cfg2))
81 -- Use only the latter exclude_mx if there are any.
83 exclude_mx $ if (null (get_mxs $ exclude_mx cfg2))
87 -- | The Monoid instance essentially only provides the "empty
89 instance Monoid OptionalConfiguration where
90 -- | An empty OptionalConfiguration.
91 mempty = OptionalConfiguration
103 -- | Obtain an OptionalConfiguration from list-remote-forwardsrc in
104 -- either the global configuration directory or the user's home
105 -- directory. The one in $HOME is prefixed by a dot so that it is
108 -- We make an attempt at cross-platform compatibility; we will try
109 -- to find the correct directory even on Windows. But if the calls
110 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
111 -- fall back to using the Unix-specific /etc and $HOME.
113 from_rc :: IO OptionalConfiguration
115 etc <- catchIOError getSysconfDir (\e -> do
116 putStrLn $ "ERROR: " ++ (show e)
118 home <- catchIOError getHomeDirectory (\e -> do
119 putStrLn $ "ERROR: " ++ (show e)
121 let global_config_path = etc </> "list-remote-forwardsrc"
122 let user_config_path = home </> ".list-remote-forwardsrc"
123 cfg <- DC.load [ DC.Optional global_config_path,
124 DC.Optional user_config_path ]
125 cfg_database <- DC.lookup cfg "database"
126 cfg_domain_query <- DC.lookup cfg "domain_query"
127 cfg_exclude_mx <- DC.lookup cfg "exclude_mx"
128 cfg_forward_query <- DC.lookup cfg "forward_query"
129 cfg_host <- DC.lookup cfg "host"
130 cfg_password <- DC.lookup cfg "password"
131 cfg_port <- DC.lookup cfg "port"
132 cfg_username <- DC.lookup cfg "username"
134 return $ OptionalConfiguration
137 (fromMaybe (MxList []) cfg_exclude_mx)