Improve config file handling (local+user conf files)
parent
214653bee5
commit
23f19e653f
|
@ -131,6 +131,7 @@ executable brittany
|
||||||
, strict
|
, strict
|
||||||
, monad-memo
|
, monad-memo
|
||||||
, safe
|
, safe
|
||||||
|
, filepath >=1.4.1.0 && <1.5
|
||||||
}
|
}
|
||||||
hs-source-dirs: src-brittany
|
hs-source-dirs: src-brittany
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
|
@ -40,6 +40,8 @@ import DataTreePrint
|
||||||
import UI.Butcher.Monadic
|
import UI.Butcher.Monadic
|
||||||
|
|
||||||
import qualified System.Exit
|
import qualified System.Exit
|
||||||
|
import qualified System.Directory as Directory
|
||||||
|
import qualified System.FilePath.Posix as FilePath
|
||||||
|
|
||||||
import Paths_brittany
|
import Paths_brittany
|
||||||
|
|
||||||
|
@ -119,12 +121,9 @@ mainCmdParser = do
|
||||||
_ -> do
|
_ -> do
|
||||||
liftIO $ putStrErrLn $ "more than one output, aborting"
|
liftIO $ putStrErrLn $ "more than one output, aborting"
|
||||||
System.Exit.exitWith (System.Exit.ExitFailure 50)
|
System.Exit.exitWith (System.Exit.ExitFailure 50)
|
||||||
let configPath = maybe "brittany.yaml" id $ listToMaybe $ reverse configPaths
|
config <- runMaybeT (readConfigs cmdlineConfig configPaths) >>= \case
|
||||||
config <- do
|
Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 50)
|
||||||
may <- runMaybeT $ readMergePersConfig cmdlineConfig configPath
|
Just x -> return x
|
||||||
case may of
|
|
||||||
Nothing -> System.Exit.exitWith (System.Exit.ExitFailure 50)
|
|
||||||
Just x -> return x
|
|
||||||
when (runIdentity $ _dconf_dump_config $ _conf_debug $ config) $ do
|
when (runIdentity $ _dconf_dump_config $ _conf_debug $ config) $ do
|
||||||
trace (showTree config) $ return ()
|
trace (showTree config) $ return ()
|
||||||
liftIO $ do
|
liftIO $ do
|
||||||
|
@ -214,3 +213,20 @@ mainCmdParser = do
|
||||||
]
|
]
|
||||||
then trace "----"
|
then trace "----"
|
||||||
else id
|
else id
|
||||||
|
|
||||||
|
readConfigs :: ConfigF Maybe -> [System.IO.FilePath] -> MaybeT IO Config
|
||||||
|
readConfigs cmdlineConfig configPaths = do
|
||||||
|
let defLocalConfigPath = "brittany.yaml"
|
||||||
|
userBritPath <- liftIO $ Directory.getAppUserDataDirectory "brittany"
|
||||||
|
let defUserConfigPath = userBritPath FilePath.</> "config.yaml"
|
||||||
|
merged <- case configPaths of
|
||||||
|
[] -> do
|
||||||
|
liftIO $ Directory.createDirectoryIfMissing False userBritPath
|
||||||
|
return cmdlineConfig
|
||||||
|
>>= readMergePersConfig defLocalConfigPath False
|
||||||
|
>>= readMergePersConfig defUserConfigPath True
|
||||||
|
-- TODO: ensure that paths exist ?
|
||||||
|
paths -> foldl (\prev p -> prev >>= readMergePersConfig p False)
|
||||||
|
(return cmdlineConfig)
|
||||||
|
paths
|
||||||
|
return $ cZip fromMaybeIdentity staticDefaultConfig merged
|
||||||
|
|
|
@ -142,23 +142,26 @@ configParser = do
|
||||||
-- , infoIntersperse = True
|
-- , infoIntersperse = True
|
||||||
-- }
|
-- }
|
||||||
|
|
||||||
readMergePersConfig :: ConfigF Maybe -> System.IO.FilePath -> MaybeT IO Config
|
readMergePersConfig
|
||||||
readMergePersConfig conf path = do
|
:: System.IO.FilePath -> Bool -> ConfigF Maybe -> MaybeT IO (ConfigF Maybe)
|
||||||
|
readMergePersConfig path shouldCreate conf = do
|
||||||
exists <- liftIO $ System.Directory.doesFileExist path
|
exists <- liftIO $ System.Directory.doesFileExist path
|
||||||
if exists
|
if
|
||||||
then do
|
| exists -> do
|
||||||
contents <- liftIO $ ByteString.readFile path -- no lazy IO, tyvm.
|
contents <- liftIO $ ByteString.readFile path -- no lazy IO, tyvm.
|
||||||
fileConf <- case Data.Yaml.decodeEither contents of
|
fileConf <- case Data.Yaml.decodeEither contents of
|
||||||
Left e -> do
|
Left e -> do
|
||||||
liftIO $ putStrLn $ "error reading in brittany config from " ++ path ++ ":"
|
liftIO
|
||||||
liftIO $ putStrLn e
|
$ putStrLn
|
||||||
mzero
|
$ "error reading in brittany config from " ++ path ++ ":"
|
||||||
Right x -> return x
|
liftIO $ putStrLn e
|
||||||
return $ cZip fromMaybeIdentity staticDefaultConfig
|
mzero
|
||||||
$ cZip (<|>) conf fileConf
|
Right x -> return x
|
||||||
else do
|
return $ (cZip (<|>) conf fileConf)
|
||||||
liftIO $ ByteString.writeFile path
|
| shouldCreate -> do
|
||||||
$ Data.Yaml.encode
|
liftIO $ ByteString.writeFile path
|
||||||
$ cMap (Just . runIdentity) staticDefaultConfig
|
$ Data.Yaml.encode
|
||||||
return $ cZip fromMaybeIdentity staticDefaultConfig
|
$ cMap (Just . runIdentity) staticDefaultConfig
|
||||||
$ conf
|
return $ conf
|
||||||
|
| otherwise -> do
|
||||||
|
return conf
|
||||||
|
|
Loading…
Reference in New Issue