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