DotEnv.jl

Get environment variables from .env files in your Julia projects.
Author vmari
Popularity
6 Stars
Updated Last
8 Months Ago
Started In
July 2018

DotEnv.jl

Build Status Coverage Status

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.

Install

Pkg.add("DotEnv")

Usage

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

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)

Options

Path

Default: .env

You can specify a custom path for your .env file.

using DotEnv
DotEnv.config(path = "custom.env")

Manual Parsing

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")

Rules

You can write your .env file using the following rules:

  • BASIC=basic becomes Dict("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 override ENV try:
using DotEnv

cfg = DotEnv.parse(read(".env.override"))

for (k, v) in cfg
    ENV[k] = v
end

Note about credits

We want to thank @motdotla. Our code is mostly based on his repo