X-Git-Url: http://gitweb.michael.orlitzky.com/?p=mailbox-count.git;a=blobdiff_plain;f=src%2FMain.hs;h=633e969fcef69849fd07ed602990e52a0f49d80f;hp=b52bf504c02da2592670be05e44c1102a04dd413;hb=7a6ba612bdec5ba940dc6f74143ca64a9163fff2;hpb=4a39ec095893aadf86c37e9089cefd346113b55e diff --git a/src/Main.hs b/src/Main.hs index b52bf50..633e969 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,7 +1,9 @@ module Main where +import Data.Maybe ( fromMaybe ) import Data.Monoid ( (<>) ) +import Data.String.Utils ( join ) import Database.HDBC.PostgreSQL ( connectPostgreSQL ) import System.Console.CmdArgs ( def ) @@ -10,13 +12,36 @@ import Configuration ( Configuration(..), merge_optional ) import qualified OptionalConfiguration as OC ( from_rc ) import Report ( report ) +-- | Construct a connection string (postgres-only, for now) from a +-- 'Configuration'. All of these are optional, at least for +-- Postgres, and so we want to avoid appending e.g. \"host=\" to the +-- connection string if @(host cfg)@ is 'Nothing'. +-- +-- Examples: +-- +-- >>> let default_cfg = def :: Configuration +-- >>> let cfg = default_cfg { host = Just "localhost" } +-- >>> connection_string cfg +-- "host=localhost" +-- >>> let cfg2 = cfg { username = Just "postgres" } +-- >>> connection_string cfg2 +-- "host=localhost user=postgres" +-- connection_string :: Configuration -> String connection_string cfg = - "host=" ++ (host cfg) ++ " " ++ - "port=" ++ (show $ port cfg) ++ " " ++ - "user=" ++ (username cfg) ++ " " ++ - "password=" ++ (password cfg) ++ " " ++ - "dbname=" ++ (database 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 + pw_part = let pw = fmap ("password=" ++) (password cfg) in fromMaybe "" pw + db_part = let db = fmap ("dbname=" ++) (database cfg) in fromMaybe "" db + main :: IO () main = do