From 9d1e4362f78ab0a0b1a0007aaa38c6cc94c0a62d Mon Sep 17 00:00:00 2001 From: Joris Date: Mon, 21 Mar 2016 10:29:21 +0100 Subject: Add an example --- Data/ConfigManager.hs | 31 +++++++++++++++++++++++++++++++ Data/ConfigManager/Reader.hs | 4 ++-- config-manager.cabal | 3 +++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Data/ConfigManager.hs b/Data/ConfigManager.hs index aa92554..37da8b6 100644 --- a/Data/ConfigManager.hs +++ b/Data/ConfigManager.hs @@ -22,6 +22,9 @@ module Data.ConfigManager -- ** Comments -- $comments + -- ** Example + -- $example + -- * Configuration loading readConfig @@ -96,3 +99,31 @@ lookupDefault defaultValue name config = foldl (flip const) defaultValue $ looku -- -- > # Comment -- > x = 8 # Another comment + +-- $example +-- +-- From application.conf: +-- +-- > port = 3000 +-- > mailFrom = "no-reply@mail.com" +-- > currency = "$" +-- +-- Read the configuration: +-- +-- > import qualified Data.ConfigManager as Conf +-- > +-- > data Conf = Conf +-- > { port :: Int +-- > , mailFrom :: String +-- > , currency :: String +-- > } deriving (Read, Eq, Show) +-- > +-- > getConfig :: IO (Either Text Conf) +-- > getConfig = +-- > (flip fmap) (Conf.readConfig "application.conf") (\configOrError -> do +-- > conf <- configOrError +-- > Conf <$> +-- > Conf.lookup "port" conf <*> +-- > Conf.lookup "mailFrom" conf <*> +-- > Conf.lookup "currency" conf +-- > ) diff --git a/Data/ConfigManager/Reader.hs b/Data/ConfigManager/Reader.hs index c2f75a7..d422996 100644 --- a/Data/ConfigManager/Reader.hs +++ b/Data/ConfigManager/Reader.hs @@ -43,8 +43,8 @@ go fileDir (Right config) expr = Binding name value -> return . Right . Config $ M.insert name value (hashMap config) Import requirement path -> do - eitherConfig <- readConfig requirement (fileDir path) - case eitherConfig of + configOrError <- readConfig requirement (fileDir path) + case configOrError of Left errorMessage -> return . Left $ errorMessage Right importedConfig -> diff --git a/config-manager.cabal b/config-manager.cabal index 6dcf25e..99d5b1b 100644 --- a/config-manager.cabal +++ b/config-manager.cabal @@ -9,6 +9,9 @@ description: * required or optional imports, . * comments. + . + For details of the configuration file format, see + . homepage: https://gitlab.com/guyonvarch/config-manager bug-reports: https://gitlab.com/guyonvarch/config-manager/issues license: GPL-3 -- cgit v1.2.3