1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
|