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