From 2e24114804826c54b2534c60c4bc473b4714b1cb Mon Sep 17 00:00:00 2001 From: Mikko Ahlroth Date: Thu, 21 Jan 2021 07:10:39 +0200 Subject: [PATCH] Support lines starting with export --- lib/dotenv_parser.ex | 7 +++++-- mix.exs | 2 +- test/data/.env | 1 + test/dotenv_parser_test.exs | 3 ++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/dotenv_parser.ex b/lib/dotenv_parser.ex index 0e38673..c2ae23b 100644 --- a/lib/dotenv_parser.ex +++ b/lib/dotenv_parser.ex @@ -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)}, diff --git a/mix.exs b/mix.exs index ca98e1f..6026e7c 100644 --- a/mix.exs +++ b/mix.exs @@ -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(), diff --git a/test/data/.env b/test/data/.env index 50c7676..3201250 100644 --- a/test/data/.env +++ b/test/data/.env @@ -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" diff --git a/test/dotenv_parser_test.exs b/test/dotenv_parser_test.exs index c5ad954..071260f 100644 --- a/test/dotenv_parser_test.exs +++ b/test/dotenv_parser_test.exs @@ -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