Skip to content

Commit

Permalink
Add opts argument to resolve_env!/4 (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardd authored and AndrewDryga committed Aug 2, 2019
1 parent 903632d commit fa436d1
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/confex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,24 @@ defmodule Confex do
...> Confex.resolve_env!(:myapp)
** (ArgumentError) can't fetch value for key `:test_var` of application `:myapp`, can not cast "foo" to Integer
The optional `opts` argument is passed through to the internal call to
`Application.put_env/4` and may be used to set the `timeout` and/or
`persistent` parameters.
*Warning!* Do not use this function if you want to change your environment
while VM is running. All `{:system, _}` tuples would be replaced with actual values.
"""
@spec resolve_env!(app :: app()) :: [{key(), value()}] | no_return
def resolve_env!(app) do
@spec resolve_env!(app :: app(), opts :: Keyword.t()) :: [{key(), value()}] | no_return
def resolve_env!(app, opts \\ []) do
app
|> Application.get_all_env()
|> Enum.map(&resolve_and_update_env(app, &1))
|> Enum.map(&resolve_and_update_env(app, &1, opts))
end

defp resolve_and_update_env(app, {key, config}) do
defp resolve_and_update_env(app, {key, config}, opts) do
case Resolver.resolve(config) do
{:ok, config} ->
:ok = Application.put_env(app, key, config)
:ok = Application.put_env(app, key, config, opts)
{key, config}

{:error, {_reason, message}} ->
Expand Down

0 comments on commit fa436d1

Please sign in to comment.