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