Exenv.Adapter behaviour (Exenv v0.4.1) View Source

Defines an Exenv adapter.

An Exenv adapter is simply a module that adheres to the callbacks required. It can be as simple as:

defmodule MyAdapter do
  use Exenv.Adapter

  @imple true
  def load(opts) do
    # load some system env vars

    :ok
  end
end

Some adapters may be simple and do not require a process on their own. But if some form of state is needed, we can also make our adapter process-based. If we define our adapter within the normal Exenv startup flow, this process will then be automatically started and supervised. Below is an example:

defmodule MyAdapter do
  use Exenv.Adapter
  use GenServer

  @impl true
  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: __MODULE__)
  end

  @impl true
  def init(opts) do
    {:ok, opts}
  end

  @impl true
  def load(opts) do
    # load some system env vars

    GenServer.call(__MODULE__, {:load, opts})
  end

  @impl true
  def handle_call({:load, opts}, _from, config) do
    # load some system env vars

    {:reply, :ok, config}
  end
end

And thats it! We can know start using our new adapter.

Reading Files

If your adapter reads files in order to load env vars, it is recommended that Exenv.read_file/2 is used. This will enabled support for secrets encryption. If a user passes the one of the following with your options, the file will be automatically decrypted.

  # Decrypts the file using MASTER_KEY env var
  [encryption: true]

  # Decrypts the file using a master key file
  [encryption: [master_key: "/path/to/master.key"]]

Link to this section Summary

Callbacks

Loads the system env vars using the adapter and options provided.

Starts the adapter process if required.

Link to this section Types

Specs

config() :: {t(), keyword()}

Specs

result() :: :ok | {:error, term()}

Specs

t() :: module()

Link to this section Callbacks

Specs

load(opts :: keyword()) :: result()

Loads the system env vars using the adapter and options provided.

Specs

start_link(opts :: keyword()) :: GenServer.on_start()

Starts the adapter process if required.