File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -194,6 +194,31 @@ defmodule Config do
194
194
|> put_config ( )
195
195
end
196
196
197
+ @ doc """
198
+ Reads the configuration for the given root key.
199
+
200
+ This function only reads the configuration from a previous
201
+ `config/2` or `config/3` call. If `root_key` points to an
202
+ application, it does not read its actual application environment.
203
+ Its main use case is to make it easier to access and share
204
+ configuration values across files.
205
+
206
+ If the `root_key` was not configured, it returns `nil`.
207
+
208
+ ## Examples
209
+
210
+ # In config/config.exs
211
+ config :my_app, foo: :bar
212
+
213
+ # In config/dev.exs
214
+ config :another_app, foo: read_config(:my_app)[:foo] || raise "missing parent configuration"
215
+
216
+ """
217
+ @ doc since: "1.18.0"
218
+ def read_config ( root_key ) when is_atom ( root_key ) do
219
+ get_config! ( ) [ root_key ]
220
+ end
221
+
197
222
@ doc """
198
223
Returns the environment this configuration file is executed on.
199
224
Original file line number Diff line number Diff line change @@ -64,6 +64,16 @@ defmodule ConfigTest do
64
64
assert config ( ) == [ app: [ { Repo , other: :value , key: :other } ] ]
65
65
end
66
66
67
+ test "read_config/1" do
68
+ assert read_config ( :lager ) == nil
69
+
70
+ config :lager , key: :value
71
+ assert read_config ( :lager ) == [ key: :value ]
72
+
73
+ config :lager , other: :value
74
+ assert read_config ( :lager ) == [ key: :value , other: :value ]
75
+ end
76
+
67
77
@ tag env: :dev
68
78
test "config_env/0" do
69
79
assert config_env ( ) == :dev
You can’t perform that action at this time.
0 commit comments