module Main where import Data.Monoid ( (<>) ) import Database.HDBC.PostgreSQL ( connectPostgreSQL ) import System.Console.CmdArgs ( def ) import CommandLine ( get_args ) import Configuration ( Configuration(..), merge_optional ) import qualified OptionalConfiguration as OC ( from_rc ) import Report ( report ) connection_string :: Configuration -> String connection_string cfg = "host=" ++ (host cfg) ++ " " ++ "port=" ++ (show $ port cfg) ++ " " ++ "user=" ++ (username cfg) ++ " " ++ "password=" ++ (password cfg) ++ " " ++ "dbname=" ++ (database cfg) main :: IO () main = do rc_cfg <- OC.from_rc cmd_cfg <- get_args -- Merge the config file options with the command-line ones, -- prefering the command-line ones. let opt_config = rc_cfg <> cmd_cfg -- Update a default config with any options that have been set in -- either the config file or on the command-line. We initialize -- logging before the missing parameter checks below so that we can -- log the errors. let cfg = (def :: Configuration) `merge_optional` opt_config -- Check the optional config for missing required options. --when (isNothing (OC.password opt_config)) $ do -- report_error "No password supplied." -- exitWith (ExitFailure exit_no_password) --when (isNothing (OC.username opt_config)) $ do -- report_error "No username supplied." --exitWith (ExitFailure exit_no_username) conn <- connectPostgreSQL (connection_string cfg) r <- report conn (both cfg) (detail cfg) putStrLn r -- disconnect conn