Envar (envar v1.1.0)
Docs for Envar; Environment Variable checker/getter.
Variable names are logged to improve developer experience.
The values of Environment Variables should never be logged here.
If an App needs to debug a variable, it can log it locally.
Link to this section Summary
Functions
get/2 gets an environment variable by name
with an optional second argument default_value
which, as it's name suggests, defines the default value
for the evironment variable if it is not set.
is_set_all/1 binary check that ALL
environment variable in a List are defined.
e.g: Envar.is_set_all?(~w/HEROKU FLYIO/) will return false
if both the HEROKU and FLYIO environment variables are not set.
When all of the environment variables in the list are set,
it will return true.
It's the equivalent of writing:
Envar.is_set?("HEROKU") && Envar.is_set?("FLYIO").
is_set_any/1 binary check that any
environment variable in a List is defined.
e.g: Envar.is_set_any?(["HEROKU", "FLYIO"]) will return false
if both the HEROKU and FLYIO environment variables are not set.
When any of the environment variables in the list are set,
it will return true.
It's the equivalent of writing:
Envar.is_set?("HEROKU") || Envar.is_set?("FLYIO").
is_set/1 binary check that an environment variable is defined by name
e.g: Envar.is_set?("HEROKU") will return false
if the HEROKU environment variable is not set.
When a particular variable is set, it will return true.
load/1 load a file containing a line-separated list
of environment variables e.g: .env
Set the value of each environment variable.
read/1 reads a file containing a line-separated list
of environment variables e.g: .env
Returns a Map in the form %{ KEY: value, MYVAR: value2 }
require_env_file/1 load a file containing a line-separated list
of environment variables e.g: .env
Set the value of each environment variable.
Log an Error if the file is not available
Link to this section Functions
get(varname, default \\ nil)
get/2 gets an environment variable by name
with an optional second argument default_value
which, as it's name suggests, defines the default value
for the evironment variable if it is not set.
examples
Examples
iex> System.put_env("HELLO", "world")
iex> Envar.get("HELLO")
"world"
iex> Envar.get("FOO", "bar")
"bar"
is_set_all?(list)
is_set_all/1 binary check that ALL
environment variable in a List are defined.
e.g: Envar.is_set_all?(~w/HEROKU FLYIO/) will return false
if both the HEROKU and FLYIO environment variables are not set.
When all of the environment variables in the list are set,
it will return true.
It's the equivalent of writing:
Envar.is_set?("HEROKU") && Envar.is_set?("FLYIO").
examples
Examples
iex> Envar.is_set_all?(["HEROKU", "AWS"])
false
iex> Envar.set("HELLO", "world")
iex> Envar.set("GOODBYE", "au revoir")
iex> Envar.is_set_all?(["HELLO", "GOODBYE"])
true
is_set_any?(list)
is_set_any/1 binary check that any
environment variable in a List is defined.
e.g: Envar.is_set_any?(["HEROKU", "FLYIO"]) will return false
if both the HEROKU and FLYIO environment variables are not set.
When any of the environment variables in the list are set,
it will return true.
It's the equivalent of writing:
Envar.is_set?("HEROKU") || Envar.is_set?("FLYIO").
examples
Examples
iex> Envar.is_set_any?(["HEROKU", "AWS"])
false
iex> System.put_env("HELLO", "world")
iex> Envar.is_set_any?(["HELLO", "GOODBYE"])
true
is_set?(varname)
is_set/1 binary check that an environment variable is defined by name
e.g: Envar.is_set?("HEROKU") will return false
if the HEROKU environment variable is not set.
When a particular variable is set, it will return true.
examples
Examples
iex> Envar.is_set?("HEROKU")
false
iex> System.put_env("HELLO", "world")
iex> Envar.is_set?("HELLO")
true
keys(filename)
load(filename)
@spec load(binary()) :: :ok
load/1 load a file containing a line-separated list
of environment variables e.g: .env
Set the value of each environment variable.
examples
Examples
iex> Envar.load(".env")
:ok
read(filename)
read/1 reads a file containing a line-separated list
of environment variables e.g: .env
Returns a Map in the form %{ KEY: value, MYVAR: value2 }
examples
Examples
iex> Envar.read(".env")
%{
"ADMIN_EMAIL" => "alex@gmail.com",
"EVERYTHING" => "awesome!",
"SECRET" => "master plan"
}
require_env_file(filename)
@spec require_env_file(binary()) :: :ok
require_env_file/1 load a file containing a line-separated list
of environment variables e.g: .env
Set the value of each environment variable.
Log an Error if the file is not available
examples
Examples
iex> Envar.require_env_file(".env")
:ok
iex> Envar.require_env_file(".env_not_there")
:error
set(varname, value)
set/2 set the value of an environment variable varname.
Accepts two String parameters: varname and value.
examples
Examples
iex> Envar.set("API_KEY", "YourSuperLongAPIKey")
:ok