]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - OptionalConfiguration.hs
ce99f5f9517beeb2e4907da77e7a32ea00cdd378
[dead/htsn-import.git] / 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 module OptionalConfiguration (
13 OptionalConfiguration(..),
14 from_rc,
15 merge_maybes )
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.Monoid ( Monoid(..) )
28 import Data.Typeable ( Typeable )
29 import Paths_htsn_import ( getSysconfDir )
30 import System.Directory ( getHomeDirectory )
31 import System.FilePath ( (</>) )
32 import System.IO.Error ( catchIOError )
33 import System.Log ( Priority(..) )
34 import Text.Read ( readMaybe )
35
36 import Backend ( Backend(..) )
37 import Network.Services.TSN.Report ( report_error )
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 backend :: Maybe Backend,
54 connection_string :: Maybe String,
55 log_file :: Maybe FilePath,
56 log_level :: Maybe Priority,
57 syslog :: Maybe Bool,
58 xml_files :: [FilePath] }
59 deriving (Show, Data, Typeable)
60
61
62 -- | Combine two Maybes into one, essentially mashing them
63 -- together. We give precedence to the second argument when both are
64 -- Justs.
65 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
66 merge_maybes Nothing Nothing = Nothing
67 merge_maybes (Just x) Nothing = Just x
68 merge_maybes Nothing (Just x) = Just x
69 merge_maybes (Just _) (Just y) = Just y
70
71
72 -- | The Monoid instance for these lets us "combine" two
73 -- OptionalConfigurations. The "combine" operation that we'd like to
74 -- perform is, essentially, to mash them together. So if we have two
75 -- OptionalConfigurations, each half full, we could combine them
76 -- into one big one.
77 --
78 -- This is used to merge command-line and config-file settings.
79 --
80 instance Monoid OptionalConfiguration where
81 -- | An empty OptionalConfiguration.
82 mempty = OptionalConfiguration Nothing Nothing Nothing Nothing Nothing []
83
84
85 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
86 -- XML files can only be specified on the command-line, so we
87 -- just join them together here.
88 cfg1 `mappend` cfg2 =
89 OptionalConfiguration
90 (merge_maybes (backend cfg1) (backend cfg2))
91 (merge_maybes (connection_string cfg1) (connection_string cfg2))
92 (merge_maybes (log_file cfg1) (log_file cfg2))
93 (merge_maybes (log_level cfg1) (log_level cfg2))
94 (merge_maybes (syslog cfg1) (syslog cfg2))
95 ((xml_files cfg1) ++ (xml_files cfg2))
96
97 instance DCT.Configured Priority where
98 -- | This allows us to read a Priority level out of a Configurator
99 -- config file. By default Configurator wouldn't know what to do,
100 -- so we have to tell it that we expect one of the valid Priority
101 -- constructors.
102 convert (DCT.String "INFO") = Just INFO
103 convert (DCT.String "WARNING") = Just WARNING
104 convert (DCT.String "ERROR") = Just ERROR
105 convert _ = Nothing
106
107
108 -- | Obtain an OptionalConfiguration from htsn-importrc in either the global
109 -- configuration directory or the user's home directory. The one in
110 -- $HOME is prefixed by a dot so that it is hidden.
111 --
112 -- We make an attempt at cross-platform compatibility; we will try
113 -- to find the correct directory even on Windows. But if the calls
114 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
115 -- fall back to using the Unix-specific /etc and $HOME.
116 --
117 from_rc :: IO OptionalConfiguration
118 from_rc = do
119 etc <- catchIOError getSysconfDir (\e -> do
120 report_error (show e)
121 return "/etc")
122 home <- catchIOError getHomeDirectory (\e -> do
123 report_error (show e)
124 return "$(HOME)")
125 let global_config_path = etc </> "htsn-importrc"
126 let user_config_path = home </> ".htsn-importrc"
127 cfg <- DC.load [ DC.Optional global_config_path,
128 DC.Optional user_config_path ]
129 cfg_backend <- DC.lookup cfg "backend"
130 cfg_connection_string <- DC.lookup cfg "connection_string"
131 cfg_log_file <- DC.lookup cfg "log_file"
132 cfg_log_level <- DC.lookup cfg "log_level"
133 cfg_syslog <- DC.lookup cfg "syslog"
134 let cfg_xml_files = [] -- This won't be in the config file.
135 return $ OptionalConfiguration
136 (case cfg_backend of -- Try to convert a String to a Backend.
137 Nothing -> Nothing
138 Just s -> readMaybe s)
139 cfg_connection_string
140 cfg_log_file
141 cfg_log_level
142 cfg_syslog
143 cfg_xml_files