Support lines starting with export

This commit is contained in:
Mikko Ahlroth 2021-01-21 07:10:39 +02:00
parent 701d7f2a80
commit 2e24114804
4 changed files with 9 additions and 4 deletions

View file

@ -20,6 +20,9 @@ defmodule DotenvParser do
* `\\\\` - Backslash
* `\\uFFFF` - Unicode escape (4 hex characters to denote the codepoint)
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
@ -38,7 +41,7 @@ defmodule DotenvParser do
"""
@linefeed_re ~R/\r?\n/
@line_re ~R/^\s*[a-z_][a-z_0-9]*\s*=/i
@line_re ~R/^(?:\s*export)?\s*[a-z_][a-z_0-9]*\s*=/i
@dquoted_val_re ~R/^\s*"(.*)(?!\\)"\s*(?:#.*)?$/
@squoted_val_re ~R/^\s*'(.*)(?!\\)'\s*(?:#.*)?$/
@hex_re ~R/^[0-9a-f]+$/i
@ -114,7 +117,7 @@ defmodule DotenvParser do
raise ParseError, "Malformed line cannot be parsed: #{line}"
else
[var, val] = String.split(line, "=", parts: 2)
var = String.trim(var)
var = var |> String.trim() |> String.replace_leading("export ", "")
val = String.trim(val)
with {:dquoted, nil} <- {:dquoted, Regex.run(@dquoted_val_re, val)},

View file

@ -4,7 +4,7 @@ defmodule DotenvParser.MixProject do
def project do
[
app: :dotenv_parser,
version: "1.0.0",
version: "1.1.0",
elixir: "~> 1.11",
start_permanent: Mix.env() == :prod,
deps: deps(),

View file

@ -29,3 +29,4 @@ INLINE_COMMENT="foo#bar" # Bark Bark
INLINE_COMMENT_PLAIN=foo bar # Bark Bark
END_BACKSLASH="something\" # Comment
lowercased_var=foo
export FOO="exports are supported"

View file

@ -30,7 +30,8 @@ defmodule DotenvParserTest do
{"INLINE_COMMENT", "foo#bar"},
{"INLINE_COMMENT_PLAIN", "foo bar"},
{"END_BACKSLASH", "something\\"},
{"lowercased_var", "foo"}
{"lowercased_var", "foo"},
{"FOO", "exports are supported"}
]
end