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