michallepicki

michallepicki

Loading additional config files from runtime.exs for test and dev

I know that import_config/1 cannot be used in runtime.exs (docs), I am guessing that’s because of how runtime.exs has special handling for being copied into a release, and this does not extend to extra files. Config.Provider can be used to load additional files on release boot time.

But what’s the recommended approach for loading additional files in dev and test environments, assuming that releases are not used? Other than for aestethics (compared to nesting in if config_env() == :dev), also to be able to have git-ignored files for customizing the runtime config by individual devs (e.g. runtime.dev.local.exs).

I came up with something like this, but I don’t know if it’s fine to use Config.Reader inside runtime.exs:

# inside runtime.exs

# import_config does not work for runtime.exs as a safeguard
# because it would potentially fail to find the file when running a release
# so we implement our own similar functionality for non release envs
import_non_release_config_when_present = fn env, relative_path ->
  if env == :prod, do: raise "this would fail when building a release"

  path = Path.expand(relative_path, __DIR__)

  if config_env() == env && File.exists?(path) do
    path
    |> Config.Reader.read!()
    |> Enum.each(fn {root_key, configs} ->
      Enum.each(configs, fn {key, value} ->
        config root_key, key, value
      end)
    end)
  end
end

for env <- [:dev, :test] do
  import_non_release_config_when_present.(env, "runtime.#{env}.exs")
  import_non_release_config_when_present.(env, "runtime.#{env}.local.exs")
end

Most Liked

jstimps

jstimps

Perhaps Code.require_file(config_path) ? I’ve used this in the past but I’m not sure if there’s a better way either.

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement