From 1b1245577ec53a2ebceb3e4535c5bdc001b1bc28 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Tue, 14 Feb 2012 18:37:45 +0900 Subject: [PATCH] Removing the anti-pattern: IO (Maybe a). --- CabalDev.hs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/CabalDev.hs b/CabalDev.hs index 34dfe5c..91b4f1f 100644 --- a/CabalDev.hs +++ b/CabalDev.hs @@ -6,32 +6,31 @@ module CabalDev (modifyOptions) where -} import Control.Applicative ((<$>)) +import Control.Exception (SomeException, throwIO) import Data.List (find) -import System.FilePath (splitPath,joinPath,()) +import GHC (gcatch) import System.Directory +import System.FilePath (splitPath,joinPath,()) import Text.Regex.Posix ((=~)) - import Types modifyOptions :: Options -> IO Options -modifyOptions opts = - fmap (has_cdev opts) findCabalDev +modifyOptions opts = found `gcatch` notFound where - has_cdev :: Options -> Maybe String -> Options - has_cdev op Nothing = op - has_cdev op (Just path) = addPath op path + found = addPath opts <$> findCabalDev + notFound :: SomeException -> IO Options + notFound _ = return opts -findCabalDev :: IO (Maybe String) -findCabalDev = - getCurrentDirectory >>= searchIt . splitPath +findCabalDev :: IO String +findCabalDev = getCurrentDirectory >>= searchIt . splitPath addPath :: Options -> String -> Options addPath orig_opts path = do let orig_ghcopt = ghcOpts orig_opts orig_opts { ghcOpts = orig_ghcopt ++ ["-package-conf", path] } -searchIt :: [FilePath] -> IO (Maybe FilePath) -searchIt [] = return Nothing +searchIt :: [FilePath] -> IO FilePath +searchIt [] = throwIO $ userError "Not found" searchIt path = do a <- doesDirectoryExist (mpath path) if a then @@ -41,7 +40,7 @@ searchIt path = do where mpath a = joinPath a "cabal-dev/" -findConf :: FilePath -> IO (Maybe FilePath) +findConf :: FilePath -> IO FilePath findConf path = do - f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path - return ((path ) <$> f) + Just f <- find (=~ "packages.*\\.conf") <$> getDirectoryContents path + return $ path f