]> gitweb.michael.orlitzky.com - mailbox-count.git/blob - src/Main.hs
Add SQLite support (default if a filename is given as the database).
[mailbox-count.git] / src / Main.hs
1 {-# LANGUAGE DoAndIfThenElse #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 module Main
4 where
5
6 import Data.Maybe ( fromMaybe )
7 import Data.Monoid ( (<>) )
8 import Data.String.Utils ( join )
9 import Database.HDBC.PostgreSQL ( connectPostgreSQL )
10 import Database.HDBC.Sqlite3 ( connectSqlite3 )
11 import System.Console.CmdArgs ( def )
12 import System.Directory ( doesFileExist )
13
14 import CommandLine ( get_args )
15 import Configuration ( Configuration(..), merge_optional )
16 import qualified OptionalConfiguration as OC ( from_rc )
17 import Report ( report )
18
19
20 -- | Construct a connection string (postgres-only, for now) from a
21 -- 'Configuration'. All of these are optional, at least for
22 -- Postgres, and so we want to avoid appending e.g. \"host=\" to the
23 -- connection string if @(host cfg)@ is 'Nothing'.
24 --
25 -- Examples:
26 --
27 -- >>> let default_cfg = def :: Configuration
28 -- >>> let cfg = default_cfg { host = Just "localhost" }
29 -- >>> connection_string cfg
30 -- "host=localhost"
31 -- >>> let cfg2 = cfg { username = Just "postgres" }
32 -- >>> connection_string cfg2
33 -- "host=localhost user=postgres"
34 --
35 connection_string :: Configuration -> String
36 connection_string cfg =
37 trim $ join " " [host_part, port_part, user_part, pw_part, db_part]
38 where
39 -- | Strip leading/trailing whitespace, and collapse multiple
40 -- consecutive spaces into one.
41 trim :: String -> String
42 trim = unwords . words
43
44 host_part = let h = fmap ("host=" ++) (host cfg) in fromMaybe "" h
45 port_part = let p = fmap (("port=" ++) . show) (port cfg) in fromMaybe "" p
46 user_part = let u = fmap ("user=" ++) (username cfg) in fromMaybe "" u
47 pw_part = let pw = fmap ("password=" ++) (password cfg) in fromMaybe "" pw
48 db_part = let db = fmap ("dbname=" ++) (database cfg) in fromMaybe "" db
49
50
51 main :: IO ()
52 main = do
53 rc_cfg <- OC.from_rc
54 cmd_cfg <- get_args
55
56 -- Merge the config file options with the command-line ones,
57 -- prefering the command-line ones.
58 let opt_config = rc_cfg <> cmd_cfg
59
60 -- Update a default config with any options that have been set in
61 -- either the config file or on the command-line. We initialize
62 -- logging before the missing parameter checks below so that we can
63 -- log the errors.
64 let cfg = (def :: Configuration) `merge_optional` opt_config
65
66 -- If a database name was specified, and that name exists as a file
67 -- on the system, assume that the user wanted to use SQLite.
68 r <- case (database cfg) of
69 Nothing -> connectPostgreSQL (connection_string cfg) >>= report cfg
70
71 Just dbname -> do
72 exists <- doesFileExist dbname
73 if exists
74 then connectSqlite3 dbname >>= report cfg
75 else connectPostgreSQL (connection_string cfg) >>= report cfg
76
77 -- The DB connection is implicitly closed when it gets garbage collected.
78 putStrLn r