View Source Jetenv in 5 minutes
Features
- Runtime configure from Environment
- Zero custom
runtime.exs
coding - Explicit type conversion
- Address any Application environment node
- Fully automatic scan of Environment variables
Basics
- Common Env prefix:
je__
(can be customized) - Separator is two successive underscores
__
(equals a dot.
) - Think of Application Env as a single root Keyword tree
Examples
A string config
Config:
config :my_app, :a_string_value, "this is a string"
Jetenv:
export je__my_app__a_string_value__S="this is a string"
Note, the prefix je__
and the type suffix __S
A value in the tree
Config
config :my_app, :group_of_options, [
option_bool: true,
sub_options: [
deeper_option_number: 14
]
]
Jetenv
je__my_app__group_of_options__option_bool__B=true
je__my_app__group_of_options__sub_options__deeper_option_number__I=14
A JSON value (maps/lists)
Note that as Config does not merge non-keyword nodes, nor does Jetenv.
Config
config :my_app, :map_data,
%{
a: 11,
b: 12
}
Jetenv
je__my_app__map_data__J={"a": 11, "b": 12}
Result
iex(2)> Jetenv.load_all_env
[my_app: [map_data: %{"a" => 11, "b" => 12}]]
iex(3)> Jetenv.load_all_env |> Application.put_all_env
:ok
iex(4)> Application.get_all_env :my_app
[map_data: %{"a" => 11, "b" => 12}]
Jetenv.load_env
will load config only for loaded Applications.
load_all_env
will load anything found with the active prefix, this
will emit warnings on startup that configuration elements exist for
an unloaded application (which is why it's not the default behaviour).
These two are clearly not equivalent, but Jetenv can also handle erlang terms.
Erlang Term
sh% export je__my_app__config1__map__G='#{a => 1, b => 2}.'
sh% iex -S mix
iex(1)> Jetenv.load_all_env(prefix: "je")
[my_app: [config1: [map: %{a: 1, b: 2}]]]
iex(2)> Jetenv.load_all_env(prefix: "je") |> Application.put_all_env
:ok
iex(3)> Application.get_all_env :my_app
[config1: [map: %{a: 1, b: 2}]]
Adding to a project
- No forklift required
- Use runtime_merge to cooperate with Config
For a new project
import Config
Jetenv.load_env()
For an existing runtime.exs
:
import Config
config :my_app .....
Jetenv.load_env() |> Jetenv.runtime_merge()
QR Link
