Skip to content

Commit

Permalink
Factor loadExactConfig out of loadRawConfig
Browse files Browse the repository at this point in the history
loadExactConfig will load just the config in the given file path or return
nothing if the file does not exist. loadRawConfig will continue to load the user
config in ~/.cabal/config or the file specified by CABAL_CONFIG. loadExactConfig
is factored out so that the same logic may be used to read config files from
other locations.
  • Loading branch information
ttuegel committed Nov 15, 2017
1 parent b2a2dbe commit b1845ef
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions cabal-install/Distribution/Client/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
module Distribution.Client.Config (
SavedConfig(..),
loadConfig,
loadExactConfig,
getConfigFilePath,

showConfig,
Expand Down Expand Up @@ -613,31 +614,41 @@ extendToEffectiveConfig config = do
loadRawConfig :: Verbosity -> Flag FilePath -> IO SavedConfig
loadRawConfig verbosity configFileFlag = do
(source, configFile) <- getConfigFilePathAndSource configFileFlag
notice verbosity $ "Global config file path source is " ++ sourceMsg source ++ "."
minp <- loadExactConfig verbosity configFile
case minp of
Nothing -> createDefaultConfigFile verbosity configFile
Just conf -> return conf

where
sourceMsg CommandlineOption = "commandline option"
sourceMsg EnvironmentVariable = "env var CABAL_CONFIG"
sourceMsg Default = "default config file"

data ConfigFileSource = CommandlineOption
| EnvironmentVariable
| Default

-- | Like 'loadRawConfig', but loads only exactly the file given.
--
loadExactConfig :: Verbosity -> FilePath -> IO (Maybe SavedConfig)
loadExactConfig verbosity configFile = do
minp <- readConfigFile mempty configFile
case minp of
Nothing -> do
notice verbosity $ "Config file path source is " ++ sourceMsg source ++ "."
notice verbosity $ "Config file " ++ configFile ++ " not found."
createDefaultConfigFile verbosity configFile
return Nothing
Just (ParseOk ws conf) -> do
notice verbosity $ "Config file " ++ configFile ++ " loaded."
unless (null ws) $ warn verbosity $
unlines (map (showPWarning configFile) ws)
return conf
return (Just conf)
Just (ParseFailed err) -> do
let (line, msg) = locatedErrorMsg err
die' verbosity $
"Error parsing config file " ++ configFile
++ maybe "" (\n -> ':' : show n) line ++ ":\n" ++ msg

where
sourceMsg CommandlineOption = "commandline option"
sourceMsg EnvironmentVariable = "env var CABAL_CONFIG"
sourceMsg Default = "default config file"

data ConfigFileSource = CommandlineOption
| EnvironmentVariable
| Default

-- | Returns the config file path, without checking that the file exists.
-- The order of precedence is: input flag, CABAL_CONFIG, default location.
getConfigFilePath :: Flag FilePath -> IO FilePath
Expand Down

0 comments on commit b1845ef

Please sign in to comment.