]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/OptionalConfiguration.hs
0913864e0bb064cfd61884e25940a1e326eda771
[dead/htsn.git] / src / OptionalConfiguration.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5
6 -- | An OptionalConfiguration is just like a 'Configuration', except
7 -- all of its fields are optional. The user can set options in two
8 -- places: the command-line, and a configuration file. Obviously if
9 -- a parameter is set in one place, it doesn't need to be set in the
10 -- other. Thus, the latter needs to be optional.
11 --
12
13 module OptionalConfiguration (
14 OptionalConfiguration(..),
15 from_rc,
16 merge_maybes )
17 where
18
19 import qualified Data.Configurator as DC (
20 Worth(Optional),
21 load,
22 lookup )
23 import qualified Data.Configurator.Types as DCT (
24 Configured,
25 Value( String ),
26 convert )
27 import Data.Data ( Data )
28 import Data.Maybe ( fromMaybe )
29 import Data.Monoid ( Monoid(..) )
30 import Data.Typeable ( Typeable )
31 import System.Directory ( getHomeDirectory )
32 import System.FilePath ( (</>) )
33 import System.IO.Error ( catchIOError )
34 import System.Log ( Priority(..) )
35
36 import Logging ( log_error )
37 import TSN.FeedHosts ( FeedHosts(..) )
38
39
40 -- Derive standalone instances of Data and Typeable for Priority. This
41 -- is necessary for OptionalConfiguration (which contains a Maybe
42 -- Priority) to derive Data and Typeable.
43 deriving instance Data Priority
44 deriving instance Typeable Priority
45
46 -- | The same as Configuration, except everything is optional. It's easy to
47 -- merge two of these by simply dropping the Nothings in favor of
48 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
49 -- can parse more than one of them.
50 --
51 data OptionalConfiguration =
52 OptionalConfiguration {
53 feed_hosts :: FeedHosts,
54 log_file :: Maybe FilePath,
55 log_level :: Maybe Priority,
56 password :: Maybe String,
57 output_directory :: Maybe FilePath,
58 syslog :: Maybe Bool,
59 username :: Maybe String }
60 deriving (Show, Data, Typeable)
61
62
63 -- | Combine two Maybes into one, essentially mashing them
64 -- together. We give precedence to the second argument when both are
65 -- Justs.
66 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
67 merge_maybes Nothing Nothing = Nothing
68 merge_maybes (Just x) Nothing = Just x
69 merge_maybes Nothing (Just x) = Just x
70 merge_maybes (Just _) (Just y) = Just y
71
72
73 -- | The Monoid instance for these lets us "combine" two
74 -- OptionalConfigurations. The "combine" operation that we'd like to
75 -- perform is, essentially, to mash them together. So if we have two
76 -- OptionalConfigurations, each half full, we could combine them
77 -- into one big one.
78 --
79 -- This is used to merge command-line and config-file settings.
80 --
81 instance Monoid OptionalConfiguration where
82 -- | An empty OptionalConfiguration.
83 mempty = OptionalConfiguration
84 (FeedHosts [])
85 Nothing
86 Nothing
87 Nothing
88 Nothing
89 Nothing
90 Nothing
91
92
93 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
94 cfg1 `mappend` cfg2 =
95 OptionalConfiguration
96 all_feed_hosts
97 (merge_maybes (log_file cfg1) (log_file cfg2))
98 (merge_maybes (log_level cfg1) (log_level cfg2))
99 (merge_maybes (password cfg1) (password cfg2))
100 (merge_maybes (output_directory cfg1) (output_directory cfg2))
101 (merge_maybes (syslog cfg1) (syslog cfg2))
102 (merge_maybes (username cfg1) (username cfg2))
103 where
104 -- Use only the latter feed_hosts if there are any.
105 all_feed_hosts =
106 feed_hosts $ if (null (get_feed_hosts (feed_hosts cfg2)))
107 then cfg1
108 else cfg2
109
110
111 instance DCT.Configured Priority where
112 -- | This allows us to read a Priority level out of a Configurator
113 -- config file. By default Configurator wouldn't know what to do,
114 -- so we have to tell it that we expect one of the valid Priority
115 -- constructors.
116 convert (DCT.String "INFO") = Just INFO
117 convert (DCT.String "WARNING") = Just WARNING
118 convert (DCT.String "ERROR") = Just ERROR
119 convert _ = Nothing
120
121
122 -- | Obtain an OptionalConfiguration from the file ".htsnrc" in the
123 -- user's home directory.
124 --
125 -- We make an attempt at cross-platform compatibility; we will try
126 -- to find the correct directory even on Windows. But if the call
127 -- to getHomeDirectory fails for whatever reason, we fall back to
128 -- using the environment variable $HOME.
129 --
130 from_rc :: IO OptionalConfiguration
131 from_rc = do
132 -- After some thought, the "global" /etc/htsnrc configuration file
133 -- was left out. Since each config file needs a password, and this
134 -- should be run by a dedicated user anyway, the global file does
135 -- not serve much purpose. It could also be a security risk (visible
136 -- password) if the admin screws up.
137 home <- catchIOError getHomeDirectory (\e -> do
138 log_error (show e)
139 return "$(HOME)")
140 let user_config_path = home </> ".htsnrc"
141 cfg <- DC.load [ DC.Optional user_config_path ]
142 cfg_log_file <- DC.lookup cfg "log_file"
143 cfg_log_level <- DC.lookup cfg "log_level"
144 cfg_password <- DC.lookup cfg "password"
145 cfg_output_directory <- DC.lookup cfg "output_directory"
146 cfg_syslog <- DC.lookup cfg "syslog"
147 cfg_username <- DC.lookup cfg "username"
148 cfg_feed_hosts <- DC.lookup cfg "feed_hosts"
149
150 return $ OptionalConfiguration
151 (fromMaybe (FeedHosts []) cfg_feed_hosts)
152 cfg_log_file
153 cfg_log_level
154 cfg_password
155 cfg_output_directory
156 cfg_syslog
157 cfg_username
158