aboutsummaryrefslogtreecommitdiff
path: root/config/dotenv.exs
diff options
context:
space:
mode:
Diffstat (limited to 'config/dotenv.exs')
-rw-r--r--config/dotenv.exs40
1 files changed, 40 insertions, 0 deletions
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