]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/OptionalConfiguration.hs
Provide a default connection string (for sqlite).
[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 syslog :: Maybe Bool,
59 xml_files :: [FilePath] }
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 Nothing Nothing Nothing Nothing Nothing []
84
85
86 -- | Combine @cfg1@ and @cfg2@, giving precedence to @cfg2@.
87 -- XML files can only be specified on the command-line, so we
88 -- just join them together here.
89 cfg1 `mappend` cfg2 =
90 OptionalConfiguration
91 (merge_maybes (backend cfg1) (backend cfg2))
92 (merge_maybes (connection_string cfg1) (connection_string cfg2))
93 (merge_maybes (log_file cfg1) (log_file cfg2))
94 (merge_maybes (log_level cfg1) (log_level cfg2))
95 (merge_maybes (syslog cfg1) (syslog cfg2))
96 ((xml_files cfg1) ++ (xml_files cfg2))
97
98 instance DCT.Configured Priority where
99 -- | This allows us to read a Priority level out of a Configurator
100 -- config file. By default Configurator wouldn't know what to do,
101 -- so we have to tell it that we expect one of the valid Priority
102 -- constructors.
103 convert (DCT.String "INFO") = Just INFO
104 convert (DCT.String "WARNING") = Just WARNING
105 convert (DCT.String "ERROR") = Just ERROR
106 convert _ = Nothing
107
108
109 -- | Obtain an OptionalConfiguration from htsn-importrc in either the global
110 -- configuration directory or the user's home directory. The one in
111 -- $HOME is prefixed by a dot so that it is hidden.
112 --
113 -- We make an attempt at cross-platform compatibility; we will try
114 -- to find the correct directory even on Windows. But if the calls
115 -- to getHomeDirectory/getSysconfDir fail for whatever reason, we
116 -- fall back to using the Unix-specific /etc and $HOME.
117 --
118 from_rc :: IO OptionalConfiguration
119 from_rc = do
120 etc <- catchIOError getSysconfDir (\e -> do
121 report_error (show e)
122 return "/etc")
123 home <- catchIOError getHomeDirectory (\e -> do
124 report_error (show e)
125 return "$(HOME)")
126 let global_config_path = etc </> "htsn-importrc"
127 let user_config_path = home </> ".htsn-importrc"
128 cfg <- DC.load [ DC.Optional global_config_path,
129 DC.Optional user_config_path ]
130 cfg_backend <- DC.lookup cfg "backend"
131 cfg_connection_string <- DC.lookup cfg "connection_string"
132 cfg_log_file <- DC.lookup cfg "log_file"
133 cfg_log_level <- DC.lookup cfg "log_level"
134 cfg_syslog <- DC.lookup cfg "syslog"
135 let cfg_xml_files = [] -- This won't be in the config file.
136 return $ OptionalConfiguration
137 (case cfg_backend of -- Try to convert a String to a Backend.
138 Nothing -> Nothing
139 Just s -> readMaybe s)
140 cfg_connection_string
141 cfg_log_file
142 cfg_log_level
143 cfg_syslog
144 cfg_xml_files