From ebbe27625ba2594bd71f2b48f0f38d67ed805c02 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 28 Jan 2020 20:05:52 -0500 Subject: dotenv loading works --- config/dotenv.exs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 config/dotenv.exs (limited to 'config/dotenv.exs') diff --git a/config/dotenv.exs b/config/dotenv.exs new file mode 100644 index 0000000..b084ba1 --- /dev/null +++ b/config/dotenv.exs @@ -0,0 +1,40 @@ +defmodule Thulani.Config.Dotenv do + require Logger + + def load_dotenv! do + {:ok, projroot} = __DIR__ |> get_projroot + + contents = + case projroot |> Path.join(".env") |> File.read() do + {:error, :enoent} -> + Logger.warn("skipping dotenv file: doesn't exist") + "" + + {:ok, contents} -> + contents + end + + contents + |> String.split("\n") + |> Enum.map(&String.trim/1) + |> Enum.filter(fn x -> x != "" end) + |> Enum.map(fn x -> + result = String.split(x, "=") + {Enum.at(result, 0), Enum.at(result, 1)} + end) + |> System.put_env() + end + + defp get_projroot(base) do + result = + with {:ok, info} <- base |> Path.join(".thulani_root") |> File.stat(), + :regular <- info.type, + do: {:ok, base} + + case result do + {:error, :enoent} -> get_projroot(base |> Path.join("..") |> Path.expand()) + {:ok, "/"} -> {:error, "couldn't find .thulani_root"} + x -> x + end + end +end -- cgit v1.3.1