]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/OptionalConfiguration.hs
Don't remove imported files by default.
[dead/htsn-import.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 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 ConnectionString ( ConnectionString )
38 import Network.Services.TSN.Report ( report_error )
39
40
41 -- Derive standalone instances of Data and Typeable for Priority. This
42 -- is necessary for OptionalConfiguration (which contains a Maybe
43 -- Priority) to derive Data and Typeable.
44 deriving instance Data Priority
45 deriving instance Typeable Priority
46
47 -- | The same as Configuration, except everything is optional. It's easy to
48 -- merge two of these by simply dropping the Nothings in favor of
49 -- the Justs. The 'feed_hosts' are left un-maybed so that cmdargs
50 -- can parse more than one of them.
51 --
52 data OptionalConfiguration =
53 OptionalConfiguration {
54 backend :: Maybe Backend,
55 connection_string :: Maybe ConnectionString,
56 log_file :: Maybe FilePath,
57 log_level :: Maybe Priority,
58 remove :: Maybe Bool,
59 syslog :: Maybe Bool,
60 xml_files :: [FilePath] }
61 deriving (Show, Data, Typeable)
62
63
64 -- | Combine two Maybes into one, essentially mashing them
65 -- together. We give precedence to the second argument when both are
66 -- Justs.
67 merge_maybes :: (Maybe a) -> (Maybe a) -> (Maybe a)
68 merge_maybes Nothing Nothing = Nothing
69 merge_maybes (Just x) Nothing = Just x
70 merge_maybes Nothing (Just x) = Just x
71 merge_maybes (Just _) (Just y) = Just y
72
73
74 -- | The Monoid instance for these lets us "combine" two
75 -- OptionalConfigurations. The "combine" operation that we'd like to
76 -- perform is, essentially, to mash them together. So if we have two
77 -- OptionalConfigurations, each half full, we could combine them
78 -- into one big one.
79 --
80 -- This is used to merge command-line and config-file settings.
81 --
82 instance Monoid OptionalConfiguration where
83 -- | An empty OptionalConfiguration.
84 mempty = OptionalConfiguration
85 Nothing
86 Nothing
87 Nothing
88 Nothing
89 Nothing
90 Nothing
91 []
92
93
94 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
95 -- XML files can only be specified on the command-line, so we
96 -- just join them together here.
97 cfg1 `mappend` cfg2 =
98 OptionalConfiguration
99 (merge_maybes (backend cfg1) (backend cfg2))
100 (merge_maybes (connection_string cfg1) (connection_string cfg2))
101 (merge_maybes (log_file cfg1) (log_file cfg2))
102 (merge_maybes (log_level cfg1) (log_level cfg2))
103 (merge_maybes (remove cfg1) (remove cfg2))
104 (merge_maybes (syslog cfg1) (syslog cfg2))
105 ((xml_files cfg1) ++ (xml_files cfg2))
106
107 instance DCT.Configured Priority where
108 -- | This allows us to read a Priority level out of a Configurator
109 -- config file. By default Configurator wouldn't know what to do,
110 -- so we have to tell it that we expect one of the valid Priority
111 -- constructors.
112 convert (DCT.String "INFO") = Just INFO
113 convert (DCT.String "WARNING") = Just WARNING
114 convert (DCT.String "ERROR") = Just ERROR
115 convert _ = Nothing
116
117
118 -- | Obtain an OptionalConfiguration from htsn-importrc in either the global
119 -- configuration directory or the user's home directory. The one in
120 -- $HOME is prefixed by a dot so that it is hidden.
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 calls
124 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
125 -- fall back to using the Unix-specific /etc and $HOME.
126 --
127 from_rc :: IO OptionalConfiguration
128 from_rc = do
129 etc <- catchIOError getSysconfDir (\e -> do
130 report_error (show e)
131 return "/etc")
132 home <- catchIOError getHomeDirectory (\e -> do
133 report_error (show e)
134 return "$(HOME)")
135 let global_config_path = etc </> "htsn-importrc"
136 let user_config_path = home </> ".htsn-importrc"
137 cfg <- DC.load [ DC.Optional global_config_path,
138 DC.Optional user_config_path ]
139 cfg_backend <- DC.lookup cfg "backend"
140 cfg_connection_string <- DC.lookup cfg "connection_string"
141 cfg_log_file <- DC.lookup cfg "log_file"
142 cfg_log_level <- DC.lookup cfg "log_level"
143 cfg_remove <- DC.lookup cfg "remove"
144 cfg_syslog <- DC.lookup cfg "syslog"
145 let cfg_xml_files = [] -- This won't be in the config file.
146 return $ OptionalConfiguration
147 (case cfg_backend of -- Try to convert a String to a Backend.
148 Nothing -> Nothing
149 Just s -> readMaybe s)
150 cfg_connection_string
151 cfg_log_file
152 cfg_log_level
153 cfg_remove
154 cfg_syslog
155 cfg_xml_files