diff options
| author | Nathan Perry <np@nathanperry.dev> | 2020-01-28 20:05:52 -0500 |
|---|---|---|
| committer | Nathan Perry <np@nathanperry.dev> | 2020-01-28 20:05:52 -0500 |
| commit | ebbe27625ba2594bd71f2b48f0f38d67ed805c02 (patch) | |
| tree | 72e53f8775fa774d8250c6bcdec27428bd07f9bb | |
| parent | 5dadc47eb07be076a8b270458badb9d8cd7dfead (diff) | |
dotenv loading works
| -rw-r--r-- | apps/thulani_bot/lib/thulani/bot/application.ex | 1 | ||||
| -rw-r--r-- | apps/thulani_bot/lib/thulani/bot/config.ex | 7 | ||||
| -rw-r--r-- | apps/thulani_bot/mix.exs | 3 | ||||
| -rw-r--r-- | config/config.exs | 4 | ||||
| -rw-r--r-- | config/dev.exs | 7 | ||||
| -rw-r--r-- | config/dotenv.exs | 40 | ||||
| -rw-r--r-- | config/load_from_dotenv.exs | 36 |
7 files changed, 48 insertions, 50 deletions
diff --git a/apps/thulani_bot/lib/thulani/bot/application.ex b/apps/thulani_bot/lib/thulani/bot/application.ex index 63bd89e..8ab6e90 100644 --- a/apps/thulani_bot/lib/thulani/bot/application.ex +++ b/apps/thulani_bot/lib/thulani/bot/application.ex @@ -4,6 +4,7 @@ defmodule Thulani.Bot.Application do def start(_type, _args) do Config.init!() + Application.start(:nostrum) children = [] diff --git a/apps/thulani_bot/lib/thulani/bot/config.ex b/apps/thulani_bot/lib/thulani/bot/config.ex index 5542752..d93e8d1 100644 --- a/apps/thulani_bot/lib/thulani/bot/config.ex +++ b/apps/thulani_bot/lib/thulani/bot/config.ex @@ -11,7 +11,7 @@ defmodule Thulani.Bot.Config do require Logger - def init!() do + def init! do load_env() |> Enum.each(fn {application, vals} -> Enum.each(vals, fn {key, val} -> Application.put_env(application, key, val) end) @@ -22,9 +22,7 @@ defmodule Thulani.Bot.Config do end end - def load_env() do - if Application.get_env(:thulani, :env) == :dev, do: load_dotenv() - + def load_env do %{ nostrum: [ token: System.fetch_env!("THULANI_TOKEN"), @@ -54,5 +52,4 @@ defmodule Thulani.Bot.Config do {env_var, value} end) end - end diff --git a/apps/thulani_bot/mix.exs b/apps/thulani_bot/mix.exs index 13afa4f..9d3b6ae 100644 --- a/apps/thulani_bot/mix.exs +++ b/apps/thulani_bot/mix.exs @@ -24,8 +24,7 @@ defmodule Thulani.Bot.MixProject do defp deps do [ - # we look up the token based on an environment variable, so we can't do this - {:nostrum, "~> 0.4"}, + {:nostrum, "~> 0.4", runtime: false}, {:util, in_umbrella: true} ] end diff --git a/config/config.exs b/config/config.exs index f0746d1..b19babd 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,9 +1,9 @@ import Config -config :thulani, +config :thulani_bot, env: Mix.env() config :logger, level: :info -import_config "#{Mix.env}.exs"
\ No newline at end of file +import_config "#{Mix.env()}.exs" diff --git a/config/dev.exs b/config/dev.exs index 9cba782..c1414e2 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -6,9 +6,6 @@ config :logger, config :nostrum, dev: true -IO.puts("hello") +import_config "dotenv.exs" -Thulani.Bot.Config.load_env() -|> Enum.each(fn {key, vals} -> - config key, vals -end) +Thulani.Config.Dotenv.load_dotenv!() 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 diff --git a/config/load_from_dotenv.exs b/config/load_from_dotenv.exs deleted file mode 100644 index 244e2bc..0000000 --- a/config/load_from_dotenv.exs +++ /dev/null @@ -1,36 +0,0 @@ -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 |
