DotEnv

public struct DotEnv

An environment variable loader.

You can either read the file and then load it or load in one step.

// read and then load
let path: String
var env = try DotEnv.read(path: path)
env.lines // [Line] (key=value pairs)
env.load()
print(ProcessInfo.processInfo.environment["FOO"]) // BAR

or

// load it
let path: String
var env = try DotEnv.load(path: path)
env.lines // [Line] (key=value pairs)
print(ProcessInfo.processInfo.environment["FOO"]) // BAR
  • Reads two DotEnv files relevant to the environment and loads them into the environment.

    The suffix parameter allows you to read a secondary file. This file will be loaded first and file that the path parameter points to will be read second. By doing this the path.suffix environment settings get overwriten any path settings.

    let path: String
    let suffix: String
    let elgs: EventLoopGroupSource
    let fileio: NonBlockingFileIO
    try DotEnv.load(path: path, suffix: suffix, on: elgs, fileio: fileio)
    print(ProcessInfo.processInfo.environment["FOO"]) // BAR
    

    Declaration

    Swift

    public static func load(
        path: String = ".env",
        suffix: String,
        on eventLoopGroupSource: EventLoopGroupSource = .createNew,
        fileio: NonBlockingFileIO,
        overwrite: Bool = true
    )

    Parameters

    path

    Path to the file you wish to load (including filename and extension)

    suffix

    A suffix to add onto the path (for loading a seperate file)

    eventLoopGroupSource

    Either provides an EventLoopGroup or tells the function to create a new one

    fileio

    NonBlockingFileIO that is used to read the .env file(s)

    overwrite

    Set to false to prevent overwiting current environment variables

  • Reads a DotEnv file relevant to the environment and loads them into the environment.

    let path: String
    let elgs: EventLoopGroupSource
    let fileio: NonBlockingFileIO
    try DotEnv.load(path: path, on: elgs, fileio: fileio)
    print(ProcessInfo.processInfo.environment["FOO"]) // BAR
    

    Declaration

    Swift

    public static func load(
        path: String,
        on eventLoopGroupSource: EventLoopGroupSource = .createNew,
        fileio: NonBlockingFileIO,
        overwrite: Bool = true
    )

    Parameters

    path

    Path to the file you wish to load (including filename and extension)

    eventLoopGroupSource

    Either provides an EventLoopGroup or tells the function to create a new one.

    fileio

    NonBlockingFileIO that is used to read the .env file(s).

    overwrite

    Set to false to prevent overwiting current environment variables

  • Reads DotEnv files relevant to the environment and loads them into the environment.

    let path: String
    let el: EventLoop
    let fileio: NonBlockingFileIO
    try DotEnv.load(path: path, on: el, fileio: fileio)
    print(ProcessInfo.processInfo.environment["FOO"]) // BAR
    

    Declaration

    Swift

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

    Parameters

    path

    Path to the file you wish to load (including filename and extension)

    eventLoop

    EventLoop to perform async work on.

    fileio

    NonBlockingFileIO that is used to read the .env file(s).

    overwrite

    Set to false to prevent overwiting current environment variables

    Return Value

    EventLoopFuture<Void>

  • Reads a DotEnv file from the supplied path.

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

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

    Declaration

    Swift

    public static func read(
        path: String = ".env",
        fileio: NonBlockingFileIO,
        on eventLoop: EventLoop
    ) -> EventLoopFuture<DotEnv>

    Parameters

    path

    Absolute or relative path of the dotenv file.

    fileio

    NonBlockingFileIO

    eventLoop

    EventLoop to perform async work on.

    Return Value

    EventLoopFuture<DotEnv>

  • Reads two DotEnv files relevant to the environment and loads them into the environment.

    The suffix parameter allows you to read a secondary file. This file will be loaded first and file that the path parameter points to will be read second. By doing this the path.suffix environment settings get overwriten any path settings.

    let path: String
    let suffix: String
    let encoding: String.Encoding
    try DotEnv.load(path: path, suffix: suffix, encoding: Encoding)
    print(ProcessInfo.processInfo.environment["FOO"]) // BAR
    

    Declaration

    Swift

    public static func load(path: String,
                            suffix: String,
                            encoding: String.Encoding = .utf8,
                            overwrite: Bool = true) throws

    Parameters

    path

    Path to the file you wish to load (including filename and extension)

    suffix

    A suffix to add onto the path (for loading a seperate file)

    encoding

    The file’s encoding

    overwrite

    Set to false to prevent overwiting current environment variables

  • Reads a DotEnv file relevant to the environment and loads them into the environment.

    let path: String
    let encoding: String.Encoding
    try DotEnv.load(path: path, encoding: Encoding)
    print(ProcessInfo.processInfo.environment["FOO"]) // BAR
    

    Declaration

    Swift

    public static func load(path: String,
                            encoding: String.Encoding = .utf8,
                            overwrite: Bool = true) throws

    Parameters

    path

    Path to the file you wish to load (including filename and extension)

    encoding

    The file’s encoding

    overwrite

    Set to false to prevent overwiting current environment variables

  • Reads a DotEnv file from the supplied path.

    let path: String
    let encoding: String.Encoding
    let file = try DotEnv.read(path: path, encoding: encoding)
    for line in file.lines {
        print("\(line.key)=\(line.value)")
    }
    file.load() // loads lines into the process
    print(Environment.process.FOO) // BAR
    

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

    Declaration

    Swift

    public static func read(path: String, encoding: String.Encoding = .utf8) throws -> DotEnv

    Parameters

    path

    Absolute or relative path of the dotenv file.

    encoding

    Encoding of the file

    Return Value

    DotEnv

  • All KEY=VALUE pairs found in the file.

    Declaration

    Swift

    public let lines: [Line]
  • Creates a new DotEnv

    Declaration

    Swift

    init(lines: [Line])
  • Loads this file’s KEY=VALUE pairs into the current process.

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

    Declaration

    Swift

    public func load(overwrite: Bool = true)

    Parameters

    overwrite

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