aboutsummaryrefslogtreecommitdiff
path: root/config/load_from_dotenv.exs
blob: 244e2bc8c081df3feed1c8572d7da16c4cf84a9a (plain) (blame)
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
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

def 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