config-manager-0.3.0.1: Configuration management

LicenseGPL-3
MaintainerJoris Guyonvarch <joris@guyonvarch.me>
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell2010

Data.ConfigManager

Contents

Description

A configuration management library.

Synopsis

Configuration file format

A configuration file consists of a series of:

  • bindings,
  • imports,
  • and comments.

Binding a name to a value

A binding associates a name to a value:

number = 1
my-string = "Hello"
a_double = 4.0
thatIsABoolean = True
a_double = 5.0
diffTime = 1 day
otherDiffTime = 3 hours
  • If two or more bindings have the same name, only the last one is kept.
  • Accepted duration values are seconds, minutes, hours, days and weeks.

Import other files

An import is either required or optional:

import "database.conf"
importMaybe "local.conf"

Comments

A comment begins with # and continues to the end of the line:

# Comment
x = 8 # Another comment

Example

From application.conf:

port = 3000
mailFrom = "no-reply@mail.com"
currency = "$"
expiration = 30 minutes

Read the configuration:

import qualified Data.ConfigManager as Conf
import Data.Time.Clock (DiffTime)

data Conf = Conf
  { port :: Int
  , mailFrom :: String
  , currency :: String
  , expiration :: DiffTime
  } deriving (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 <*>
      Conf.lookup "expiration" conf
  )

Configuration loading

Lookup functions

lookup :: Configured a => Name -> Config -> Either Text a Source #

Lookup for the value associated to a name.

lookupDefault :: Configured a => a -> Name -> Config -> a Source #

Lookup for the value associated to a name and return the default value if no binding exists with the given name.