X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Flwn-epub.git;a=blobdiff_plain;f=src%2FConfiguration.hs;h=4898c08978e6d71846c85c535c39c64b4642edd9;hp=457480a19a3debea6b86b3c9da99ea2ad79d1f0a;hb=4220827f62d772d7edcbdcc1c2f13d6c2eb5f534;hpb=ebedcdb6b1b8925dcfb5700d076f25743fac8645 diff --git a/src/Configuration.hs b/src/Configuration.hs index 457480a..4898c08 100644 --- a/src/Configuration.hs +++ b/src/Configuration.hs @@ -1,6 +1,9 @@ +{-# LANGUAGE DoAndIfThenElse #-} + module Configuration ( Cfg(..), - get_cfg + get_cfg, + use_account ) where @@ -9,6 +12,7 @@ import Data.ConfigFile import Data.Maybe (isJust) import Data.Monoid (Monoid(..)) import System.Directory ( + doesFileExist, getAppUserDataDirectory ) import System.Exit (ExitCode(..), exitWith) @@ -85,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" - - cfg_username = either_to_maybe cp_username - cfg_password = either_to_maybe cp_password - in - Right $ mempty { username = cfg_username, - password = cfg_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 }