Data.Yaml.Config

yaml-0.11.11.2: Support for parsing and rendering YAML documents.

Safe Haskell | None
Language | Haskell2010

Data.Yaml.Config

Contents

- High-level
  - EnvUsage
- Lower level

Description

Functionality for using YAML as configuration files

In particular, merging environment variables with yaml values

`loadYamlSettings` is a high-level API for loading YAML and merging environment variables. A yaml value of `_env:ENV_VAR:default` will lookup the environment variable `ENV_VAR`.

On a historical note, this code was taken directly from the yesod web framework's configuration module.

Synopsis

- loadYamlSettings :: FromJSON settings => [FilePath] -> [Value] -> EnvUsage -> IO settings
- loadYamlSettingsArgs :: FromJSON settings => [Value] -> EnvUsage -> IO settings
- data EnvUsage
- ignoreEnv :: EnvUsage
- useEnv :: EnvUsage
- requireEnv :: EnvUsage
- useCustomEnv :: KeyMap Text -> EnvUsage
- requireCustomEnv :: KeyMap Text -> EnvUsage
- applyCurrentEnv :: Bool -> Value -> IO Value
- getCurrentEnv :: IO (KeyMap Text)
- applyEnvValue :: Bool -> KeyMap Text -> Value -> Value

# High-level

loadYamlSettings #

Arguments

:: FromJSON settings |
=> [FilePath] | run time config files to use, earlier files have precedence
-> [Value] | any other values to use, usually from compile time config. overridden by files
-> EnvUsage |
-> IO settings |

Load the settings from the following three sources:

- Run time config files
- Run time environment variables
- The default compile time config file

For example, to load up settings from `config/foo.yaml` and allow overriding from the actual environment, you can use:

    loadYamlSettings ["config/foo.yaml"] [] useEnv

Since: 0.8.16

loadYamlSettingsArgs #

Arguments

:: FromJSON settings |
=> [Value] | any other values to use, usually from compile time config. overridden by files
-> EnvUsage | use environment variables
-> IO settings |

Same as `loadYamlSettings`, but get the list of runtime config files from the command line arguments.

Since: 0.8.17

## EnvUsage

data EnvUsage #

Defines how we want to use the environment variables when loading a config file. Use the smart constructors provided by this module.

Since: 0.8.16

ignoreEnv :: EnvUsage #

Do not use any environment variables, instead relying on defaults values in the config file.

Since: 0.8.16

useEnv :: EnvUsage #

Use environment variables when available, otherwise use defaults.

Since: 0.8.16

requireEnv :: EnvUsage #

Do not use default values from the config file, but instead take all overrides from the environment. If a value is missing, loading the file will throw an exception.

Since: 0.8.16

useCustomEnv :: KeyMap Text -> EnvUsage #

Same as `useEnv`, but instead of the actual environment, use the provided `HashMap` as the environment.

Since: 0.8.16

requireCustomEnv :: KeyMap Text -> EnvUsage #

Same as `requireEnv`, but instead of the actual environment, use the provided `HashMap` as the environment.

Since: 0.8.16

# Lower level

applyCurrentEnv #

Arguments

:: Bool | require an environment variable to be present?
-> Value |
-> IO Value |

A convenience wrapper around `applyEnvValue` and `getCurrentEnv`

Since: 0.8.16

getCurrentEnv :: IO (KeyMap Text) #

Get the actual environment as a `HashMap` from `Text` to `Text`.

Since: 0.8.16

applyEnvValue #

Arguments

:: Bool | require an environment variable to be present?
-> KeyMap Text |
-> Value |
-> Value |

Override environment variable placeholders in the given `Value` with values from the environment.

If the first argument is `True`, then all placeholders _must_ be provided by the actual environment. Otherwise, default values from the `Value` will be used.

Since: 0.8.16