ConfigExt v0.3.0 ConfigExt
A helper module, which contains common functions used around loading config at runtime.
Summary
Functions
Returns the evaluated value for key
in app
’s environment as a tuple
Returns the evaluated value for key
in app
’s environment
Returns the evaluated value for key
in app
’s environment
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
Same as ConfigExt.load/1
but instead of tuple, returns the value directly and on :error
raises ArgumentError
Same as ConfigExt.load!/1
but will return default value instead of raising exception
Functions
Returns the evaluated value for key
in app
’s environment as a tuple.
If the configuration parameter does not exist, or pattern fails the function returns :error.
A drop in replacement for Application.fetch_env/2
.
Returns the evaluated value for key
in app
’s environment.
If the configuration parameter does not exist or pattern will fail, raises ArgumentError.
A drop in replacement for Application.fetch_env!/2
.
Returns the evaluated value for key
in app
’s environment.
If the configuration parameter does not exist or pattern will fail, the function returns the default value.
A drop in replacement for Application.get_env/3
.
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"}
An extension to ConfigExt.load/1
function, which accepts default value as a second argument.
Returns:
{:ok, value}
as inConfigExt.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"}
Same as ConfigExt.load/1
but instead of tuple, returns the value directly and on :error
raises ArgumentError
.
Examples
Given CONFIG_EXT_TEST=foo
is set in environment.
iex> ConfigExt.load!({:system, "CONFIG_EXT_TEST"})
"foo"
When CONFIG_EXT_TEST
is not set.
iex> ConfigExt.load!({:system, "CONFIG_EXT_TEST"})
** (ArgumentError) ENV Key: CONFIG_EXT_TEST is missing
(config_ext) lib/config_ext.ex:24: ConfigExt.load!/1
Same as ConfigExt.load!/1
but will return default value instead of raising exception.
Examples
Given CONFIG_EXT_TEST=foo
is set in environment.
iex> ConfigExt.load!({:system, "CONFIG_EXT_TEST"}, "bar")
"foo"
When CONFIG_EXT_TEST
is not set.
iex> ConfigExt.load!({:system, "CONFIG_EXT_TEST"}, "bar")
"bar"