ConfigExt v0.2.0 ConfigExt

A helper module, which contains common functions used around loading config at runtime.

Summary

Functions

Looks for dynamic patterns in input, when found - evals them - otherwise passes input forward. Supported input formats are

An extension to ConfigExt.load/1 function, which accepts default value as a second argument

Functions

load(value)

Looks for dynamic patterns in input, when found - evals them - otherwise passes input forward. Supported input formats are:

{:system, KEY}
{:system, KEY, default}
{:function, Module, function_name} # which expands to below version with empty list of arguments
{:function, Module, function_name, [arg1, ...]}

Returns a tuple with two elements:

  • {:ok, value} for matched pattern if the environment variable is present, or default is present, or if the pattern wasn’t found.
  • {:error, message} if given environment variable was empty and there was no default value.

Examples

Given CONFIG_EXT_TEST=foo is set in environment.

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST"})
{:ok, "foo"}

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST", "bar"})
{:ok, "foo"}

When CONFIG_EXT_TEST is not set.

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST"})
{:error, ""}

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST", "bar"})
{:ok, "bar"}

For input with function pattern.

defmodule Foo do
  def bar, do: "baz"
end

iex> ConfigExt.load({:function, Foo, :bar, []})
{:ok, "baz"}

Function pattern should return a non nil value, otherwise it’s an error.

defmodule Foo do
  def bar, do: nil
end

iex> ConfigExt.load({:function, Foo, :bar, []})
{:error, "empty value"}

If the function doesn’t exist or it’s private you should get correct error message as well.

For input without pattern.

iex> ConfigExt.load(:error)
{:ok, :error} # for example logger level

iex> ConfigExt.load("baz")
{:ok, "baz"}
load(value, default)

An extension to ConfigExt.load/1 function, which accepts default value as a second argument.

Returns:

  • {:ok, value} as in ConfigExt.load/1
  • {:ok, default} in case of:
  • pattern failure
  • nil input value

Examples

Given CONFIG_EXT_TEST=foo is set in environment.

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST"}, "baz")
{:ok, "foo"}

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST", "bar"}, "baz")
{:ok, "foo"}

When CONFIG_EXT_TEST is not set, default value is used.

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST"}, "baz")
{:ok, "baz"}

When pattern comes with default, it takes precedence before the given one.

iex> ConfigExt.load({:system, "CONFIG_EXT_TEST", "bar"}, "baz")
{:ok, "bar"}

For input with function pattern.

defmodule Foo do
  def bar, do: "baz"
end

iex> ConfigExt.load({:function, Foo, :bar, []}, "buz")
{:ok, "baz"}

Function pattern should return a non nil value, otherwise default value will get returned.

defmodule Foo do
  def bar, do: nil
end

iex> ConfigExt.load({:function, Foo, :bar, []}, "buz")
{:ok, "buz"}

If the function doesn’t exist or it’s private you should get a {:ok, default} as well.

For input without pattern.

iex> ConfigExt.load(:error, "baz")
{:ok, :error} # as :error is a valid input

iex> ConfigExt.load("foo", "bar")
{:ok, "foo"}