]> gitweb.michael.orlitzky.com - list-remote-forwards.git/blobdiff - src/Main.hs
list-remote-forwards.cabal: delete the redundant description
[list-remote-forwards.git] / src / Main.hs
index d1d1ea81fa43516bd44b01b294326451b9fbafaf..7a17f077f8ad375a2f365ce53bb1b510d39c5958 100644 (file)
@@ -1,21 +1,26 @@
 {-# LANGUAGE DoAndIfThenElse #-}
 {-# LANGUAGE NoMonomorphismRestriction #-}
-module Main
+module Main (main)
 where
 
 import Data.Maybe ( fromMaybe )
-import Data.Monoid ( (<>) )
 import Data.String.Utils ( join )
-import Database.HDBC ( SqlError(..), handleSql )
+import Database.HDBC (
+  SqlError(seNativeError, seErrorMsg),
+  handleSql )
 import Database.HDBC.PostgreSQL ( connectPostgreSQL )
 import Database.HDBC.Sqlite3 ( connectSqlite3 )
 import System.Console.CmdArgs ( def )
 import System.Directory ( doesFileExist )
 import System.IO ( hPutStrLn, stderr )
+
 import CommandLine ( get_args )
-import Configuration ( Configuration(..), merge_optional )
+import Configuration (
+  Configuration(database, host, password, port, username),
+  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
@@ -23,7 +28,7 @@ import Report ( report )
 --   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" }
@@ -37,11 +42,6 @@ connection_string :: Configuration -> String
 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
@@ -54,7 +54,7 @@ main = do
   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
 
@@ -77,9 +77,9 @@ main = do
           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)