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
DotEnvfiles relevant to the environment and loads them into the environment.The
suffixparameter allows you to read a secondary file. This file will be loaded first and file that thepathparameter points to will be read second. By doing this thepath.suffixenvironment settings get overwriten anypathsettings.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"]) // BARDeclaration
Swift
public static func load( path: String = ".env", suffix: String, on eventLoopGroupSource: EventLoopGroupSource = .createNew, fileio: NonBlockingFileIO, overwrite: Bool = true )Parameters
pathPath to the file you wish to load (including filename and extension)
suffixA suffix to add onto the path (for loading a seperate file)
eventLoopGroupSourceEither provides an
EventLoopGroupor tells the function to create a new onefileioNonBlockingFileIOthat is used to read the .env file(s)overwriteSet to false to prevent overwiting current environment variables
-
Reads a
DotEnvfile 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"]) // BARDeclaration
Swift
public static func load( path: String, on eventLoopGroupSource: EventLoopGroupSource = .createNew, fileio: NonBlockingFileIO, overwrite: Bool = true )Parameters
pathPath to the file you wish to load (including filename and extension)
eventLoopGroupSourceEither provides an
EventLoopGroupor tells the function to create a new one.fileioNonBlockingFileIOthat is used to read the .env file(s).overwriteSet to false to prevent overwiting current environment variables
-
Reads
DotEnvfiles 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"]) // BARDeclaration
Swift
public static func load( path: String, fileio: NonBlockingFileIO, on eventLoop: EventLoop, overwrite: Bool = true ) -> EventLoopFuture<Void>Parameters
pathPath to the file you wish to load (including filename and extension)
eventLoopEventLoopto perform async work on.fileioNonBlockingFileIOthat is used to read the .env file(s).overwriteSet 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) // BARUse
DotEnv.loadto read and load with one method.Declaration
Swift
public static func read( path: String = ".env", fileio: NonBlockingFileIO, on eventLoop: EventLoop ) -> EventLoopFuture<DotEnv>Parameters
pathAbsolute or relative path of the dotenv file.
fileioNonBlockingFileIOeventLoopEventLoopto perform async work on.Return Value
EventLoopFuture<DotEnv> -
Reads two
DotEnvfiles relevant to the environment and loads them into the environment.The
suffixparameter allows you to read a secondary file. This file will be loaded first and file that thepathparameter points to will be read second. By doing this thepath.suffixenvironment settings get overwriten anypathsettings.let path: String let suffix: String let encoding: String.Encoding try DotEnv.load(path: path, suffix: suffix, encoding: Encoding) print(ProcessInfo.processInfo.environment["FOO"]) // BARDeclaration
Swift
public static func load(path: String, suffix: String, encoding: String.Encoding = .utf8, overwrite: Bool = true) throwsParameters
pathPath to the file you wish to load (including filename and extension)
suffixA suffix to add onto the path (for loading a seperate file)
encodingThe file’s encoding
overwriteSet to false to prevent overwiting current environment variables
-
Reads a
DotEnvfile 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"]) // BARDeclaration
Swift
public static func load(path: String, encoding: String.Encoding = .utf8, overwrite: Bool = true) throwsParameters
pathPath to the file you wish to load (including filename and extension)
encodingThe file’s encoding
overwriteSet to false to prevent overwiting current environment variables
-
Reads a
DotEnvfile 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) // BARUse
DotEnv.loadto read and load with one method.Declaration
Swift
public static func read(path: String, encoding: String.Encoding = .utf8) throws -> DotEnvParameters
pathAbsolute or relative path of the dotenv file.
encodingEncoding of the file
Return Value
DotEnv -
All
KEY=VALUEpairs found in the file.Declaration
Swift
public let lines: [Line] -
Creates a new
DotEnvDeclaration
Swift
init(lines: [Line]) -
Loads this file’s
KEY=VALUEpairs into the current process.let file: DotEnv file.load(overwrite: true) // loads all lines into the processDeclaration
Swift
public func load(overwrite: Bool = true)Parameters
overwriteIf
true, values already existing in the process’ env will be overwritten. Defaults totrue.
View on GitHub
DotEnv Structure Reference