]> gitweb.michael.orlitzky.com - dead/lwn-epub.git/commitdiff
Don't crash if the config file doesn't exist.
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 28 Jun 2012 21:12:49 +0000 (17:12 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 28 Jun 2012 21:12:49 +0000 (17:12 -0400)
src/Configuration.hs

index 5745d095b9e9de204012889e0f55c2bf83180a68..b7915073e842136724224a76851715b742209c06 100644 (file)
@@ -1,3 +1,5 @@
+{-# LANGUAGE DoAndIfThenElse #-}
+
 module Configuration (
   Cfg(..),
   get_cfg,
@@ -10,6 +12,7 @@ import Data.ConfigFile
 import Data.Maybe (isJust)
 import Data.Monoid (Monoid(..))
 import System.Directory (
+  doesFileExist,
   getAppUserDataDirectory
   )
 import System.Exit (ExitCode(..), exitWith)
@@ -86,25 +89,30 @@ config_filename = Cmd.program_name ++ ".conf"
 config_path :: IO FilePath
 config_path = do
   cfg_dir <- getAppUserDataDirectory Cmd.program_name
-  return $ joinPath [cfg_dir, config_filename]
+  let cfg_file = joinPath [cfg_dir, config_filename]
+  return $ cfg_file
 
 parse_config :: IO (Either String Cfg)
 parse_config = do
   cfg_file <- config_path
-  parse_result <- readfile emptyCP cfg_file
-
-  return $
-    case parse_result of
-      Left err -> Left (format_cpe err)
-      Right cp ->
-          let cp_username = get cp "DEFAULT" "username"
-              cp_password = get cp "DEFAULT" "password"
+  it_exists <- doesFileExist cfg_file
+  if not it_exists then do
+    return $ Right mempty
+  else do
+    parse_result <- readfile emptyCP cfg_file
+
+    return $
+      case parse_result of
+        Left err -> Left (format_cpe err)
+        Right cp ->
+            let cp_username = get cp "DEFAULT" "username"
+                cp_password = get cp "DEFAULT" "password"
   
-              cfg_username = either_to_maybe cp_username
-              cfg_password = either_to_maybe cp_password
-          in
-              Right $ mempty { username = cfg_username,
-                               password = cfg_password }
+                cfg_username = either_to_maybe cp_username
+                cfg_password = either_to_maybe cp_password
+            in
+                Right $ mempty { username = cfg_username,
+                                 password = cfg_password }