Vapor Documentation Beta

Structure Dot​Env​File

public struct DotEnvFile

Reads dotenv (.env) files and loads them into the current process.

let fileio: NonBlockingFileIO
let elg: EventLoopGroup
let file = try DotEnvFile.read(path: ".env", fileio: fileio, on: elg.next()).wait()
for line in file.lines {
    print("\(line.key)=\(line.value)")
}
file.load(overwrite: true) // loads all lines into the process

Dotenv files are formatted using KEY=VALUE syntax. They support comments using the # symbol. They also support strings, both single and double-quoted.

FOO=BAR
STRING='Single Quote String'
# Comment
STRING2="Double Quoted\nString"

Single-quoted strings are parsed literally. Double-quoted strings may contain escaped newlines that will be converted to actual newlines.

Nested Types

DotEnvFile.Line

Represents a KEY=VALUE pair in a dotenv file.

Properties

lines

let lines: [Line]

All KEY=VALUE pairs found in the file.

Methods

load(path:​fileio:​on:​overwrite:​)

public static func load(path: String, fileio: NonBlockingFileIO, on eventLoop: EventLoop, overwrite: Bool = false) -> EventLoopFuture<Void>

Reads a dotenv file from the supplied path and loads it into the process.

let fileio: NonBlockingFileIO
let elg: EventLoopGroup
try DotEnvFile.load(path: ".env", fileio: fileio, on: elg.next()).wait()
print(Environment.process.FOO) // BAR

Use DotEnvFile.read to read the file without loading it.

Parameters

path String

Absolute or relative path of the dotenv file.

fileio Non​Blocking​File​IO

File loader.

event​Loop Event​Loop

Eventloop to perform async work on.

overwrite Bool

If true, values already existing in the process' env will be overwritten. Defaults to false.

read(path:​fileio:​on:​)

public static func read(path: String, fileio: NonBlockingFileIO, on eventLoop: EventLoop) -> EventLoopFuture<DotEnvFile>

Reads a dotenv file from the supplied path.

let fileio: NonBlockingFileIO
let elg: EventLoopGroup
let file = try DotEnvFile.read(path: ".env", fileio: fileio, on: elg.next()).wait()
for line in file.lines {
    print("\(line.key)=\(line.value)")
}
file.load(overwrite: true) // loads all lines into the process
print(Environment.process.FOO) // BAR

Use DotEnvFile.load to read and load with one method.

Parameters

path String

Absolute or relative path of the dotenv file.

fileio Non​Blocking​File​IO

File loader.

event​Loop Event​Loop

Eventloop to perform async work on.

load(overwrite:​)

public func load(overwrite: Bool = false)

Loads this file's KEY=VALUE pairs into the current process.

let file: DotEnvFile
file.load(overwrite: true) // loads all lines into the process

Parameters

overwrite Bool

If true, values already existing in the process' env will be overwritten. Defaults to false.