AppEnv

Copy environment variables to Application config. Intended to be used at runtime to override config values using environment variables.

Examples

Copying a value wholesale.

%{"foo" => "bar"} = Application.get_env(:my_app, :my_config)
"1" = System.get_env("FOO")

:ok = AppEnv.copy("FOO", :my_app, :my_config)

"1" = Application.get_env(:my_config, :my_config)

Copying a value with custom parsing and merging.

%{"foo" => "bar"} = Application.get_env(:my_app, :my_config)
"1" = System.get_env("FOO")

:ok = AppEnv.copy("FOO", :my_app, :my_config, fn old_value, env_value ->
  case Integer.parse(env_value) do
    {new_value, ""} -> {:ok, Map.put(old_value, "foo", new_value)}
    error -> {:error, "error parsing FOO: #{inspect(error)}"}
  end
end)

%{"foo" => 1} = Application.get_env(:my_config, :my_config)

Copying a value to a Kernel.put_in/3-style path.

%{"foo" => "bar"} = Application.get_env(:my_app, :my_config)
"1" = System.get_env("FOO")

:ok = AppEnv.copy_to("FOO", :my_app, :my_config, ["foo"])

%{"foo" => "1"} = Application.get_env(:my_config, :my_config)

Copying a value to a Kernel.put_in/3-style path with custom formatting.

%{"foo" => {"bar" => "baz"}} = Application.get_env(:my_app, :my_config)
"1" = System.get_env("FOO_BAR")

:ok = AppEnv.copy_to("FOO_BAR", :my_app, :my_config, ["foo", "bar"], fn env_value ->
  case Integer.parse(env_value) do
    {new_value, ""} -> {:ok, new_value}
    error -> {:error, "error parsing FOO: #{inspect(error)}"}
  end
end)

%{"foo" => {"bar" => 1}} = Application.get_env(:my_config, :my_config)

Installation

The package can be installed by adding app_env to your list of dependencies in mix.exs:

def deps do
  [
    {:app_env, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/app_env.