I am trying to create a simple configuration file to read static data from a file . I used the examples from ConfigFile-1.1.4 to read the data from a file, although I didn't understand bits of the code I do have a it working. But I need to thread this config file through out my application ,and I understand i need to use the readerT monad for that ?
The working part of my code to read from the file is below
getConfig = do
config <- readConfig "config.cfg"
putStrLn $ "The path value is: " ++ (path config)
putStrLn $ "The filename value is: " ++ (fileName config)
readConfig :: String -> IO ConfigInfo
readConfig f = do
rv <-
runErrorT $
do
cp <- join $ liftIO $ readfile emptyCP f
let x = cp
-- read out the attributes
pv <- get x "DEFAULT" "path"
fv <- get x "DEFAULT" "filename"
-- build the config value
return (ConfigInfo {path = pv, fileName = fv})
either (\x -> error (snd x)) (\x -> return x) rv
I have a function ,
get :: ID -> Name -> IO Keys
get u n = do
p <- readFile ("getthisvaluefromconfigfile" ++ "getfromConfigfile")
.
.
.
return (p, b)
From the examples I read I am able to understand that the ask function would return me the environment ,and then i can use the functions for the ConfigInfo to extract values .
a)I am just not able to understand how do load the values into the environment in the getConfig function ?
b)And how do I specify in the ask function (Get )which instance of the ConfigInfo to read from ?
I did try to research I found some examples which use the reader monad ,but none of them show how to load the env .