View Source Nvir.Parser (Nvir v0.9.2)
A simple .env file parser.
Summary
Functions
Returns a list of {key, value}
for all variables in the given content.
Types
Functions
@spec parse(String.t(), String.t()) :: {:ok, [variable()]} | {:error, Exception.t()}
Returns a list of {key, value}
for all variables in the given content.
This function only parses strings, and will not attempt to read from the given
path
. The path
variable is only useful to give more information when an
error is returned.
Each returned value is either a string, or a function in the case of variable interpolation. That function accepts a resolver calback that provides the value of previous variables.
Resolver example
iex> file_contents = "GREETING=$INTRO $WHO!"
iex> {:ok, [{"GREETING", template}]} = Nvir.Parser.parse(file_contents)
iex> resolver = fn
...> "INTRO" -> "Hello"
...> "WHO" -> "World"
...> end
iex> template.(resolver)
"Hello World!"
When working with the system env you will likely use &System.get_env(&1, "")
as a resolver. It is common to use an empty string for undefined system
variables, but you can of course raise from your function if it better suits
your needs.