helper.ex 904 B

1234567891011121314151617181920212223
  1. defmodule Config.Helper do
  2. @doc """
  3. Get a map of environment variables.
  4. `env_list` contains a list of environment variables to load, either as a single
  5. binary (e.g. "PORT") or a tuple of a binary and an informative message to
  6. display if the variable is missing (e.g. {"PORT", "It should be around 4000"}).
  7. The map key is the environment variable converted to lower-case
  8. """
  9. def load_all(env_list) when is_list(env_list) do
  10. env_list
  11. |> Map.new(fn
  12. {env, msg} -> {env |> to_env_dict_key, load_from_env(env, msg)}
  13. env when is_binary(env) -> {env |> to_env_dict_key, load_from_env(env)}
  14. end)
  15. end
  16. def load_from_env(env, msg \\ "") when is_binary(env) and is_binary(msg),
  17. do: System.get_env(env) || raise("environment variable #{env} is missing. \n#{msg}")
  18. defp to_env_dict_key(env) when is_binary(env), do: env |> String.downcase() |> String.to_atom()
  19. end