View Source ConfigHelpers (config_helpers v1.1.0)

Module to be imported by runtime.exs to allow accessing the get_env function.

Note that functions of this module are not supposed to be called by regular application code since they use Config.config_env() which is only possible while reading config files.

Link to this section Summary

Functions

Read configuration from an environment variables while providing defaults for some envs.

3-arity clause needed to support single default value for all envs together with options list. See get_env/2 for documentation.

Link to this section Functions

Link to this function

get_env(key, defaults \\ [])

View Source

Read configuration from an environment variables while providing defaults for some envs.

Defaults can be given as keyword list or single value (equivalent to default: <value>). If the given key is not set in the environment (and not empty, see allow_empty), default keys are checked in the following order:

  1. Current env, e.g. :dev, :test or :prod
  2. :non_prod, if current env is :dev or :test
  3. :default

If no value can be found, ConfigHelpers.EnvError is raised.

The following options can be added to the defaults list:

  • as: Typecast values to :integer or :boolean
    • For :boolean, the following values are considered truthy: ["true", "True", "TRUE", "yes", "YES", "t", "T", "1"]
    • For :integer, passing a value that fails conversion using String.to_integer/1 causes ArgumentError to be raised.
  • allow_empty: Whether to accept empty strings as being set. By default, empty strings don't count as being set.
Link to this function

get_env(key, default, opts)

View Source

3-arity clause needed to support single default value for all envs together with options list. See get_env/2 for documentation.