]> gitweb.michael.orlitzky.com - list-remote-forwards.git/blob - src/OptionalConfiguration.hs
src/OptionalConfiguration.hs: add Semigroup instance.
[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 Semigroup 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
70 instance Semigroup OptionalConfiguration where
71 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
72 cfg1 <> cfg2 =
73 OptionalConfiguration
74 (merge_maybes (database cfg1) (database cfg2))
75 (merge_maybes (domain_query cfg1) (domain_query cfg2))
76 all_excluded_mx
77 (merge_maybes (forward_query cfg1) (forward_query cfg2))
78 (merge_maybes (host cfg1) (host cfg2))
79 (merge_maybes (password cfg1) (password cfg2))
80 (merge_maybes (port cfg1) (port cfg2))
81 (merge_maybes (username cfg1) (username cfg2))
82 where
83 -- Use only the latter exclude_mx if there are any.
84 all_excluded_mx =
85 exclude_mx $ if (null (get_mxs $ exclude_mx cfg2))
86 then cfg1
87 else cfg2
88
89 -- | The Monoid instance essentially only provides the "empty
90 -- configuration."
91 instance Monoid OptionalConfiguration where
92 -- | An empty OptionalConfiguration.
93 mempty = OptionalConfiguration
94 Nothing
95 Nothing
96 (MxList [])
97 Nothing
98 Nothing
99 Nothing
100 Nothing
101 Nothing
102 mappend = (<>)
103
104
105 -- | Obtain an OptionalConfiguration from list-remote-forwardsrc in
106 -- either the global configuration directory or the user's home
107 -- directory. The one in $HOME is prefixed by a dot so that it is
108 -- hidden.
109 --
110 -- We make an attempt at cross-platform compatibility; we will try
111 -- to find the correct directory even on Windows. But if the calls
112 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
113 -- fall back to using the Unix-specific /etc and $HOME.
114 --
115 from_rc :: IO OptionalConfiguration
116 from_rc = do
117 etc <- catchIOError getSysconfDir (\e -> do
118 putStrLn $ "ERROR: " ++ (show e)
119 return "/etc")
120 home <- catchIOError getHomeDirectory (\e -> do
121 putStrLn $ "ERROR: " ++ (show e)
122 return "$(HOME)")
123 let global_config_path = etc </> "list-remote-forwardsrc"
124 let user_config_path = home </> ".list-remote-forwardsrc"
125 cfg <- DC.load [ DC.Optional global_config_path,
126 DC.Optional user_config_path ]
127 cfg_database <- DC.lookup cfg "database"
128 cfg_domain_query <- DC.lookup cfg "domain_query"
129 cfg_exclude_mx <- DC.lookup cfg "exclude_mx"
130 cfg_forward_query <- DC.lookup cfg "forward_query"
131 cfg_host <- DC.lookup cfg "host"
132 cfg_password <- DC.lookup cfg "password"
133 cfg_port <- DC.lookup cfg "port"
134 cfg_username <- DC.lookup cfg "username"
135
136 return $ OptionalConfiguration
137 cfg_database
138 cfg_domain_query
139 (fromMaybe (MxList []) cfg_exclude_mx)
140 cfg_forward_query
141 cfg_host
142 cfg_password
143 cfg_port
144 cfg_username