DotEnv.jl is a zero-dependency package that loads environment variables from a .env
file into ENV
. Storing configuration in the environment is based on The Twelve-Factor App methodology.
Pkg.add("DotEnv")
using DotEnv
DotEnv.config()
Create a .env
file in your project. You can add environment-specific variables using the rule NAME=VALUE
.
For example:
#.env file
DB_HOST=127.0.0.1
DB_USER=john
DB_PASS=42
In this way, ENV
obtain both, the keys and the values you set in your .env
file.
ENV["DB_PASS"]
"42"
config
reads your .env file, parse the content, stores it to
ENV
,
and finally return a Dict with the content.
import DotEnv
cfg = DotEnv.config()
println(cfg)
Default: .env
You can specify a custom path for your .env file.
using DotEnv
DotEnv.config(path = "custom.env")
DotEnv.parse
accepts a String or an IOBuffer (Any value that can be converted into String), and it will return
a Dict with the parsed keys and values.
import DotEnv
buff = IOBuffer("BASIC=basic")
cfg = DotEnv.parse(buff) # will return a Dict
println(config) # Dict("BASIC"=>"basic")
You can write your .env
file using the following rules:
BASIC=basic
becomesDict("BASIC"=>"basic")
- empty lines are skipped
#
are comments- empty content is treated as an empty string (
EMPTY=
->Dict("EMPTY"=>"")
) - external single and double quotes are removed (
SINGLE_QUOTE='quoted'
->Dict("SINGLE_QUOTE"=>"quoted")
) - inside double quotes, new lines are expanded (
MULTILINE="new\nline"
->
Dict("MULTILINE"=>"new
line")
- inner quotes are maintained (like JSON) (
JSON={"foo": "bar"}
->Dict("JSON"=>"{\"foo\": \"bar\"}")"
) - extra spaces are removed from both ends of the value (
FOO=" some value "
->Dict("FOO"=>"some value")
) - previous
ENV
environment variables are not replaced. If you want to overrideENV
try:
using DotEnv
cfg = DotEnv.parse(read(".env.override"))
for (k, v) in cfg
ENV[k] = v
end
We want to thank @motdotla. Our code is mostly based on his repo