Helpers for handling OS environment variables.
It helps to:
- cast values of environment variables
- provide user-friendly error messages
Summary
Functions
Gets the value of the given environment variable, and casts it into given type.
Gets the value of the given environment variable, and casts it into given type.
Types
Functions
Gets the value of the given environment variable, and casts it into given type.
If the environment variable is not set, raises an error.
Examples
iex> SystemEnv.fetch_env!("NOT_SET", :boolean)
** (SystemEnv.EnvError) environment variable NOT_SET is missing
(system_env) lib/system_env.ex:134: SystemEnv.fetch_env!/3
iex:1: (file)
# export PHX_SERVER=true
iex> SystemEnv.fetch_env!("PHX_SERVER", :boolean)
true
# export DB_POOL_SIZE=10
iex> SystemEnv.fetch_env!("DB_POOL_SIZE", :integer)
10
# export PERCENT=0.95
iex> SystemEnv.fetch_env!("PERCENT", :float)
0.95
# export USER=billy
iex> SystemEnv.fetch_env!("USER", :string)
"billy"
# export USER=billy
iex> SystemEnv.fetch_env!("USER", :atom)
:billy
# pass extra message
iex> SystemEnv.fetch_env!(
...> "DATABASE_URL",
...> "Set it to something like: ecto://USER:PASS@HOST/DATABASE"
...> )
** (SystemEnv.EnvError) environment variable DATABASE_URL is missing. Set it to something like: ecto://USER:PASS@HOST/DATABASE
(system_env) lib/system_env.ex:134: SystemEnv.fetch_env!/3
iex:1: (file)
Gets the value of the given environment variable, and casts it into given type.
If the environment variable is not set, returns nil.
Examples
iex> SystemEnv.get_env("NOT_SET", :boolean)
nil
# export PHX_SERVER=true
iex> SystemEnv.get_env("PHX_SERVER", :boolean)
true
# export DB_POOL_SIZE=10
iex> SystemEnv.get_env("DB_POOL_SIZE", :integer)
10
# export PERCENT=0.95
iex> SystemEnv.get_env("PERCENT", :float)
0.95
# export USER=billy
iex> SystemEnv.get_env("USER", :string)
"billy"
# export USER=billy
iex> SystemEnv.get_env("USER", :atom)
:billy