]> gitweb.michael.orlitzky.com - mailbox-count.git/blob - src/Main.hs
b52bf504c02da2592670be05e44c1102a04dd413
[mailbox-count.git] / src / Main.hs
1 module Main
2 where
3
4 import Data.Monoid ( (<>) )
5 import Database.HDBC.PostgreSQL ( connectPostgreSQL )
6 import System.Console.CmdArgs ( def )
7
8 import CommandLine ( get_args )
9 import Configuration ( Configuration(..), merge_optional )
10 import qualified OptionalConfiguration as OC ( from_rc )
11 import Report ( report )
12
13 connection_string :: Configuration -> String
14 connection_string cfg =
15 "host=" ++ (host cfg) ++ " " ++
16 "port=" ++ (show $ port cfg) ++ " " ++
17 "user=" ++ (username cfg) ++ " " ++
18 "password=" ++ (password cfg) ++ " " ++
19 "dbname=" ++ (database cfg)
20
21 main :: IO ()
22 main = do
23 rc_cfg <- OC.from_rc
24 cmd_cfg <- get_args
25
26 -- Merge the config file options with the command-line ones,
27 -- prefering the command-line ones.
28 let opt_config = rc_cfg <> cmd_cfg
29
30 -- Update a default config with any options that have been set in
31 -- either the config file or on the command-line. We initialize
32 -- logging before the missing parameter checks below so that we can
33 -- log the errors.
34 let cfg = (def :: Configuration) `merge_optional` opt_config
35
36 -- Check the optional config for missing required options.
37 --when (isNothing (OC.password opt_config)) $ do
38 -- report_error "No password supplied."
39 -- exitWith (ExitFailure exit_no_password)
40
41 --when (isNothing (OC.username opt_config)) $ do
42 -- report_error "No username supplied."
43 --exitWith (ExitFailure exit_no_username)
44
45 conn <- connectPostgreSQL (connection_string cfg)
46 r <- report conn (both cfg) (detail cfg)
47 putStrLn r
48 -- disconnect conn