From: Cameron Ball Date: Thu, 25 Jul 2019 07:22:07 +0000 (+0800) Subject: Use Either for config errors instead of Maybe X-Git-Url: http://cameron1729.xyz/?p=xyz.git;a=commitdiff_plain;h=b91e0acb3a2477f2b41e9d822dde3eb3bd7ceb2e Use Either for config errors instead of Maybe --- diff --git a/app/Main.hs b/app/Main.hs index 6e38fcb..43b6039 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -45,16 +45,16 @@ getGlobalProjectConfig = getGlobalProjectConfigPath >>= (\path -> doesFileExist getLocalProjectConfig :: IO (Maybe LocalConfig) getLocalProjectConfig = doesFileExist "xyz.json" >>= bool (return Nothing) (BL.readFile "xyz.json" >>= return . decode) -resolveProjectConfig :: IO (Maybe ProjectConfig) +resolveProjectConfig :: IO (Either Text ProjectConfig) resolveProjectConfig = do l <- getLocalProjectConfig g <- getGlobalProjectConfig - case (l, g) of - (Just l, Just g) -> return . Just $ getProjectConfig g l - (Just _, Nothing) -> fail "Busted global config" - (Nothing, Just _) -> fail "Busted local config" - _ -> fail "All configs are busted" + return $ case (l, g) of + (Just l, Just g) -> Right $ getProjectConfig g l + (Just _, Nothing) -> Left "Busted global config" + (Nothing, Just _) -> Left "Busted local config" + _ -> Left "All configs are busted" continueIfInvalidProject :: IO () -> IO () continueIfInvalidProject next = doesFileExist "xyz.json" >>= bool next (putStrLn "Project already initialised") @@ -85,7 +85,7 @@ init = do config <- resolveProjectConfig case config of - Just projectConfig -> do + Right projectConfig -> do createDirectory "entries" case (commitCommand (projectConfig :: ProjectConfig), gitRemote (projectConfig :: ProjectConfig)) of @@ -94,8 +94,8 @@ init = do (_, _) -> return() putStrLn "\nInitialised empty project" - Nothing -> do - putStrLn "\nSomething went real wrong" + Left error -> do + putStrLn $ "\n" +++ error build :: ProjectConfig -> IO () build config = allFilesIn "entries" >>= (writeOutProject config) >> putStrLn "Build successful" @@ -149,10 +149,10 @@ usage = putStr . ununlines $ [ ] ] -gogogo :: (ProjectConfig -> IO ()) -> Maybe ProjectConfig -> IO () +gogogo :: (ProjectConfig -> IO ()) -> Either Text ProjectConfig -> IO () gogogo next projectConfig = case projectConfig of - Just config -> next config - _ -> fail "This does not appear to be a vailid project directory" -- Never executes, the cases in resolveProjectConfig will fail first. + Right config -> next config + Left error -> putStrLn $ "This does not appear to be a vailid project directory: " +++ error -- Never executes, the cases in resolveProjectConfig will fail first. parse ["init"] = initGlobalIfNeeded (continueIfInvalidProject init) >> exitSuccess parse ["build"] = resolveProjectConfig >>= (gogogo build) >> exitSuccess