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

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement