]> gitweb.michael.orlitzky.com - list-remote-forwards.git/blob - src/OptionalConfiguration.hs
list-remote-forwards.cabal: delete the redundant description
[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.Data ( Data )
24 import Paths_list_remote_forwards ( getSysconfDir )
25 import System.Directory ( getHomeDirectory )
26 import System.FilePath ( (</>) )
27 import System.IO.Error ( catchIOError )
28
29 import MxList ( MxList(MxList, get_mxs) )
30
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.
35 --
36 data OptionalConfiguration =
37 OptionalConfiguration {
38 database :: Maybe String,
39 domain_query :: Maybe String,
40 exclude_mx :: MxList,
41 forward_query :: Maybe String,
42 host :: Maybe String,
43 password :: Maybe String,
44 port :: Maybe Int,
45 username :: Maybe String }
46 deriving (Show, Data)
47
48
49 -- | Combine two Maybes into one, essentially mashing them
50 -- together. We give precedence to the second argument when both are
51 -- Justs.
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
57
58
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
63 -- into one big one.
64 --
65 -- This is used to merge command-line and config-file settings.
66 --
67
68 instance Semigroup OptionalConfiguration where
69 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
70 cfg1 <> cfg2 =
71 OptionalConfiguration
72 (merge_maybes (database cfg1) (database cfg2))
73 (merge_maybes (domain_query cfg1) (domain_query cfg2))
74 all_excluded_mx
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))
80 where
81 -- Use only the latter exclude_mx if there are any.
82 all_excluded_mx =
83 exclude_mx $ if (null (get_mxs $ exclude_mx cfg2))
84 then cfg1
85 else cfg2
86
87 -- | The Monoid instance essentially only provides the "empty
88 -- configuration."
89 instance Monoid OptionalConfiguration where
90 -- | An empty OptionalConfiguration.
91 mempty = OptionalConfiguration
92 Nothing
93 Nothing
94 (MxList [])
95 Nothing
96 Nothing
97 Nothing
98 Nothing
99 Nothing
100 mappend = (<>)
101
102
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
106 -- hidden.
107 --
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.
112 --
113 from_rc :: IO OptionalConfiguration
114 from_rc = do
115 etc <- catchIOError getSysconfDir (\e -> do
116 putStrLn $ "ERROR: " ++ (show e)
117 return "/etc")
118 home <- catchIOError getHomeDirectory (\e -> do
119 putStrLn $ "ERROR: " ++ (show e)
120 return "$(HOME)")
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"
133
134 return $ OptionalConfiguration
135 cfg_database
136 cfg_domain_query
137 (fromMaybe (MxList []) cfg_exclude_mx)
138 cfg_forward_query
139 cfg_host
140 cfg_password
141 cfg_port
142 cfg_username