import System.Console.CmdArgs ( def )
import System.Directory ( doesFileExist )
import System.IO ( hPutStrLn, stderr )
+
import CommandLine ( get_args )
import Configuration ( Configuration(..), merge_optional )
import qualified OptionalConfiguration as OC ( from_rc )
import Report ( report )
+import String ( trim )
-- | Construct a connection string (postgres-only, for now) from a
-- Postgres, and so we want to avoid appending e.g. \"host=\" to the
-- connection string if @(host cfg)@ is 'Nothing'.
--
--- Examples:
+-- ==== __Examples__
--
-- >>> let default_cfg = def :: Configuration
-- >>> let cfg = default_cfg { host = Just "localhost" }
connection_string cfg =
trim $ join " " [host_part, port_part, user_part, pw_part, db_part]
where
- -- | Strip leading/trailing whitespace, and collapse multiple
- -- consecutive spaces into one.
- trim :: String -> String
- trim = unwords . words
-
host_part = let h = fmap ("host=" ++) (host cfg) in fromMaybe "" h
port_part = let p = fmap (("port=" ++) . show) (port cfg) in fromMaybe "" p
user_part = let u = fmap ("user=" ++) (username cfg) in fromMaybe "" u
rc_cfg <- OC.from_rc
cmd_cfg <- get_args
- -- Merge the config file options with the command-line ones,
+ -- Merge the config file options with the command-line ones,
-- prefering the command-line ones.
let opt_config = rc_cfg <> cmd_cfg
else connectPostgreSQL (connection_string cfg) >>= report cfg
-- The DB connection is implicitly closed when it gets garbage collected.
- putStrLn r
+ putStr r
where
show_sql_error :: SqlError -> IO ()
show_sql_error se = hPutStrLn stderr $
- "SQL Error (" ++ (show $ seNativeError se) ++ "): " ++ (seErrorMsg se)
+ "SQL Error (" ++ show (seNativeError se) ++ "): " ++ (seErrorMsg se)