summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2012-11-05 17:33:41 +0100
committerJasper Van der Jeugt <m@jaspervdj.be>2012-11-05 17:33:41 +0100
commit4a9b84e32146f0313ecaecf504c6f183a51753cc (patch)
tree01765e738d34a161fd40dbc21b2351b54480d820 /src
parente0a4dca876ccc3ceb5dac98f1951a773377f7b82 (diff)
downloadstylish-haskell-4a9b84e32146f0313ecaecf504c6f183a51753cc.tar.gz
Rewrite config file searching a bit
See #21
Diffstat (limited to 'src')
-rw-r--r--src/Language/Haskell/Stylish/Config.hs52
1 files changed, 20 insertions, 32 deletions
diff --git a/src/Language/Haskell/Stylish/Config.hs b/src/Language/Haskell/Stylish/Config.hs
index ad2ae61..6c05e0a 100644
--- a/src/Language/Haskell/Stylish/Config.hs
+++ b/src/Language/Haskell/Stylish/Config.hs
@@ -11,7 +11,7 @@ module Language.Haskell.Stylish.Config
--------------------------------------------------------------------------------
import Control.Applicative (pure, (<$>), (<*>))
-import Control.Monad (forM, msum, mzero)
+import Control.Monad (forM, mzero)
import Data.Aeson (FromJSON (..))
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as A
@@ -71,38 +71,26 @@ defaultConfigFilePath = getDataFileName "data/stylish-haskell.yaml"
--------------------------------------------------------------------------------
configFilePath :: Verbose -> Maybe FilePath -> IO (Maybe FilePath)
-configFilePath verbose userSpecified = do
- (current, currentE) <- check $ (</> configFileName) <$>
- getCurrentDirectory
- (projectRoot, projectRootE) <- checkUntilFound =<< (map (</> configFileName))
- <$> getAncestorDirectories
- (home, homeE) <- check $ (</> configFileName) <$>
- getHomeDirectory
- (def, defE) <- check defaultConfigFilePath
- return $ msum
- [ userSpecified
- , if currentE then Just current else Nothing
- , if projectRootE then Just projectRoot else Nothing
- , if homeE then Just home else Nothing
- , if defE then Just def else Nothing
- ]
+configFilePath _ (Just userSpecified) = return $ Just userSpecified
+configFilePath verbose Nothing = do
+ current <- getCurrentDirectory
+ home <- getHomeDirectory
+ def <- defaultConfigFilePath
+ search $
+ [d </> configFileName | d <- ancestors current] ++
+ [home </> configFileName, def]
where
- getAncestorDirectories :: IO [FilePath]
- getAncestorDirectories = map joinPath . reverse . drop 2
- . inits . splitPath <$> getCurrentDirectory
-
- checkUntilFound :: [FilePath] -> IO (FilePath, Bool)
- checkUntilFound [] = return ("", False)
- checkUntilFound (f:fs) = do
- res@(_, ex) <- check (return f)
- if ex then return res else checkUntilFound fs
-
- check :: IO FilePath -> IO (FilePath, Bool)
- check fp = do
- fp' <- fp
- ex <- doesFileExist fp'
- verbose $ fp' ++ if ex then " exists" else " does not exist"
- return (fp', ex)
+ -- All ancestors of a dir (including that dir)
+ ancestors :: FilePath -> [FilePath]
+ ancestors = init . map joinPath . reverse . inits . splitPath
+
+ search :: [FilePath] -> IO (Maybe FilePath)
+ search [] = return Nothing
+ search (f : fs) = do
+ -- TODO Maybe catch an error here, dir might be unreadable
+ exists <- doesFileExist f
+ verbose $ f ++ if exists then " exists" else " does not exist"
+ if exists then return (Just f) else search fs
--------------------------------------------------------------------------------