Packages
Parse and validate your environment variables easily in Elixir. Supports compile time validation and runtime parsing of all environment variable accessed from code. Validate all set environment variables against app requirements with defaults and fallbacks before booting the app.
Current section
Files
Jump to
Current section
Files
lib/mahaul.ex
defmodule Mahaul do
@external_resource "README.md"
@moduledoc File.read!("README.md") |> String.replace("# Mahaul\n\n", "", global: false)
@version "0.5.0"
@doc false
def version, do: @version
@doc false
defmacro __using__(opts) do
quote location: :keep, bind_quoted: [opts: opts] do
require Logger
alias Mahaul.Helpers
@after_compile Mahaul
@doc false
def __opts__, do: unquote(opts)
Enum.each(opts, fn {key, val} ->
str_key = Atom.to_string(key)
fn_name = str_key |> String.downcase() |> String.to_atom()
def unquote(fn_name)() do
Helpers.get_env(unquote(key), unquote(val))
end
end)
@doc ~s"""
Same as `validate/0` except that it raises exception if the required
environment variables are not set or are invalid.
"""
def validate! do
Logger.info("#{__MODULE__}: validating environment variables...")
Helpers.validate!(Helpers.parse_all(unquote(opts)))
Logger.info("#{__MODULE__}: environment variables ok")
end
@doc ~s"""
Validates all currently set environment variables against the app required
environment variables. Return {:ok} if all required environment variables
are present and can be parsed as per the given configurations. It otherwise
return {:error, error_string} with the list of errors.
"""
def validate do
Helpers.validate(Helpers.parse_all(unquote(opts)))
end
end
end
alias Mahaul.Helpers
@doc false
defmacro __after_compile__(%{module: module}, _env) do
Helpers.validate_opts!(module.__opts__())
end
end