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