View Source DotenvParser (dotenv_parser v2.0.1)

Simple parser for dotenv style files.

Supports simple variable–value pairs with upper and lower cased variables. Values are trimmed of extra whitespace.

Blank lines and lines starting with # are ignored. Additionally inline comments can be added after values with a #, i.e. FOO=bar # comment.

Single quote or double quote value to prevent trimming of whitespace and allow usage of # in value, i.e. FOO=' bar # not comment ' # comment.

Single quoted values don't do any unescaping. Double quoted values will unescape the following:

  • \n - Linefeed
  • \r - Carriage return
  • \t - Tab
  • \f - Form feed
  • \b - Backspace
  • \" and \' - Quotes
  • \\ - Backslash
  • \uFFFF - Unicode escape (4 hex characters to denote the codepoint)
  • A backslash at the end of the line in a multiline value will remove the linefeed.

Values can span multiple lines when single or double quoted:

MULTILINE="This is a
multiline value."

This will result in the following:

System.fetch_env!("MULTILINE") == "This is a\nmultiline value."

A line can start with export for easier interoperation with regular shell scripts. These lines are treated the same as any others.

Serving suggestion

If you load lots of environment variables in config/runtime.exs, you can easily configure them for development by having an .env file in your development environment and using DotenvParser at the start of the file:

import Config

if Config.config_env() == :dev do
  DotenvParser.load_file(".env")
end

# Now variables from `.env` are loaded into system env
config :your_project,
  database_url: System.fetch_env!("DB_URL")

Link to this section Summary

Types

Pair of variable name, variable value.

Functions

Parse given data and load the variables to the environment.

Parse given file and load the variables to the environment.

Parse given data and return a list of variable–value tuples.

Parse given file and return a list of variable–value tuples.

Parse given single line and return a variable–value tuple, or a continuation value if the line started or continued a multiline value.

Link to this section Types

Specs

value_pair() :: {String.t(), String.t()}

Pair of variable name, variable value.

Link to this section Functions

Specs

load_data(String.t()) :: :ok

Parse given data and load the variables to the environment.

If a line cannot be parsed, an error is raised and no values are loaded to the environment.

Specs

load_file(String.t()) :: :ok

Parse given file and load the variables to the environment.

If a line cannot be parsed or the file cannot be read, an error is raised and no values are loaded to the environment.

Specs

parse_data(String.t()) :: [value_pair()]

Parse given data and return a list of variable–value tuples.

If a line cannot be parsed, an error is raised.

Specs

parse_file(String.t()) :: [value_pair()]

Parse given file and return a list of variable–value tuples.

If a line cannot be parsed or the file cannot be read, an error is raised.

Specs

Parse given single line and return a variable–value tuple, or a continuation value if the line started or continued a multiline value.

If line cannot be parsed, an error is raised.

The second argument needs to be nil or a continuation value returned from parsing the previous line.