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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
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