Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/doc
erl_crash.dump
*.ez
.elixir_ls
21 changes: 13 additions & 8 deletions lib/envy.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Envy do
@key_value_delimeter "="

@override_existing? Application.get_env(:envy, :override_existing, true)

@moduledoc """
Provides explicit and auto loading of env files.

Expand All @@ -27,7 +29,7 @@ defmodule Envy do
"""
def auto_load do
Application.ensure_started(:mix)
current_env = Mix.env |> to_string |> String.downcase
current_env = Mix.env() |> to_string |> String.downcase()

[".env", ".env.#{current_env}"] |> load
end
Expand All @@ -51,7 +53,7 @@ defmodule Envy do
access to dependencies.
"""
def reload_config do
Mix.Config.read!("config/config.exs") |> Mix.Config.persist
Mix.Config.read!("config/config.exs") |> Mix.Config.persist()
end

@doc """
Expand All @@ -70,8 +72,9 @@ defmodule Envy do
end

defp parse_line(line) do
[key, value] = line
|> String.strip
[key, value] =
line
|> String.strip()
|> String.split(@key_value_delimeter, parts: 2)

[key, parse_value(value)]
Expand All @@ -81,21 +84,23 @@ defmodule Envy do
if String.starts_with?(value, "\"") do
unquote_string(value)
else
value |> String.split("#", parts: 2) |> List.first
value |> String.split("#", parts: 2) |> List.first()
end
end

defp unquote_string(value) do
value
|> String.split(~r{(?<!\\)"}, parts: 3)
|> Enum.drop(1)
|> List.first
|> List.first()
|> String.replace(~r{\\"}, ~S("))
end

defp load_env(pairs) when is_list(pairs) do
Enum.each(pairs, fn([key, value]) ->
System.put_env(String.upcase(key), value)
Enum.each(pairs, fn [key, value] ->
if System.get_env(key) == nil || @override_existing? do
System.put_env(String.upcase(key), value)
end
end)
end

Expand Down