Safe Haskell | None |
---|---|
Language | Haskell2010 |
Library for read config files in YAML format.
example.yaml:
server: port: 8080 logs: access: /var/log/server/access.log error: /var/log/server/error.log
Usage example:
module Main where import Prelude hiding (lookup) import Data.Word (Word16) import Data.Yaml.Config (load, subconfig, lookupDefault, lookup) main :: IO () main = do config <- load "./example.yaml" serverConfig <- subconfig "server" config let interface = lookupDefault "interface" "127.0.0.1" serverConfig port :: Word16 = lookupDefault "port" 80 serverConfig logConfig <- subconfig "logs" serverConfig accessLog <- lookup "access" logConfig errorLog <- lookup "error" logConfig mapM_ putStrLn [interface, (show port), errorLog, accessLog]
Documentation
Type contains config section and path from root.
This error can be raised if config has not target path.
load :: FilePath -> IO Config Source
Find file in filesystem and try to load it as YAML config.
May fail with InvalidYaml
if file not found.
>>>
config <- load "example.yaml"
keys :: Config -> [Key] Source
Show all first level config field's.
>>>
keys config
["section1","section2"]
Get subconfig by name.
May fail with KeyError
if target key doesn't exist at current level.
>>>
:set -XOverloadedStrings
>>>
sub <- subconfig "section1" config
Get value for given key.
May fail with KeyError
if key doesn't exist.
>>>
keys sub
["field1","field2"]>>>
putStrLn =<< lookup "field1" sub
value1
Find value in config and return it or return default value.
>>>
lookupDefault "field3" "def" sub
"def"