]> gitweb.michael.orlitzky.com - list-remote-forwards.git/blob - src/Main.hs
Get things in shape finally:
[list-remote-forwards.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 ( SqlError(..), handleSql )
10 import Database.HDBC.PostgreSQL ( connectPostgreSQL )
11 import Database.HDBC.Sqlite3 ( connectSqlite3 )
12 import System.Console.CmdArgs ( def )
13 import System.Directory ( doesFileExist )
14 import System.IO ( hPutStrLn, stderr )
15
16 import CommandLine ( get_args )
17 import Configuration ( Configuration(..), merge_optional )
18 import qualified OptionalConfiguration as OC ( from_rc )
19 import Report ( report )
20 import String ( trim )
21
22
23 -- | Construct a connection string (postgres-only, for now) from a
24 -- 'Configuration'. All of these are optional, at least for
25 -- Postgres, and so we want to avoid appending e.g. \"host=\" to the
26 -- connection string if @(host cfg)@ is 'Nothing'.
27 --
28 -- ==== __Examples__
29 --
30 -- >>> let default_cfg = def :: Configuration
31 -- >>> let cfg = default_cfg { host = Just "localhost" }
32 -- >>> connection_string cfg
33 -- "host=localhost"
34 -- >>> let cfg2 = cfg { username = Just "postgres" }
35 -- >>> connection_string cfg2
36 -- "host=localhost user=postgres"
37 --
38 connection_string :: Configuration -> String
39 connection_string cfg =
40 trim $ join " " [host_part, port_part, user_part, pw_part, db_part]
41 where
42 host_part = let h = fmap ("host=" ++) (host cfg) in fromMaybe "" h
43 port_part = let p = fmap (("port=" ++) . show) (port cfg) in fromMaybe "" p
44 user_part = let u = fmap ("user=" ++) (username cfg) in fromMaybe "" u
45 pw_part = let pw = fmap ("password=" ++) (password cfg) in fromMaybe "" pw
46 db_part = let db = fmap ("dbname=" ++) (database cfg) in fromMaybe "" db
47
48
49 main :: IO ()
50 main = do
51 rc_cfg <- OC.from_rc
52 cmd_cfg <- get_args
53
54 -- Merge the config file options with the command-line ones,
55 -- prefering the command-line ones.
56 let opt_config = rc_cfg <> cmd_cfg
57
58 -- Update a default config with any options that have been set in
59 -- either the config file or on the command-line. We initialize
60 -- logging before the missing parameter checks below so that we can
61 -- log the errors.
62 let cfg = (def :: Configuration) `merge_optional` opt_config
63
64 -- If a database name was specified, and that name exists as a file
65 -- on the system, assume that the user wanted to use SQLite.
66 handleSql show_sql_error $ do
67 r <- case (database cfg) of
68 Nothing -> connectPostgreSQL (connection_string cfg) >>= report cfg
69
70 Just dbname -> do
71 exists <- doesFileExist dbname
72 if exists
73 then connectSqlite3 dbname >>= report cfg
74 else connectPostgreSQL (connection_string cfg) >>= report cfg
75
76 -- The DB connection is implicitly closed when it gets garbage collected.
77 putStr r
78
79 where
80 show_sql_error :: SqlError -> IO ()
81 show_sql_error se = hPutStrLn stderr $
82 "SQL Error (" ++ (show $ seNativeError se) ++ "): " ++ (seErrorMsg se)