DotEnv
public final class DotEnv
Reads, parses, loads, and caches environment files and variables.
-
All read-only system and user-defined environment variables.
Declaration
Swift
public var all: [String : String] { get }
-
A configuration for
DotEnv
.Declaration
Swift
public var configuration: Configuration
-
Initializes a new instance of
DotEnv
with the defaultconfiguration
.Declaration
Swift
public init(configuration: Configuration = .init())
Parameters
configuration
A configuration for
DotEnv
. -
Reads the source of an environment file or throws
FileError
.Throws
FileError
if an environment file being loaded either doesn’t exist or is not encodable.Declaration
Swift
public func readFile(at path: String, encoding: String.Encoding = .utf8) throws -> File
Parameters
path
An absolute path to an environment file in the file system.
encoding
An encoding for the source. Defaults to
utf8
.path
An absolute path to an environment file in the file system.
Return Value
An instance of
File
. -
Parses and extracts environment variables from the source of an environment file or throws
SyntaxError
.Throws
SyntaxError
if the source of an environment file is invalid.Declaration
Swift
public func parseFile(_ file: File) throws -> [String : String]
Parameters
file
An instance of
File
.Return Value
A list of all environment variables from an environment file.
-
Reads, parses, and extracts environment variables from the source of an environment file or throws either
FileError
orSyntaxError
.Throws
FileError
if an environment file being loaded either doesn’t exist or is not encodable orSyntaxError
if the source of an environment file is invalid.Declaration
Swift
public func parseFile(at path: String, encoding: String.Encoding = .utf8) throws -> [String : String]
Parameters
path
An absolute path to an environment file in the file system.
encoding
An encoding for the source. Defaults to
utf8
.Return Value
A list of all environment variables in an environment file.
-
Parses, extracts, and sets enviroment variables from the source of an environment file or throws either
FileError
orSyntaxError
.Throws
FileError
if an environment file being loaded either doesn’t exist or is not encodable orSyntaxError
if the source of an environment file is invalid.Declaration
Swift
public func load(_ file: File, overwrite: Bool = true) throws
Parameters
file
An instance of
File
.overwrite
A boolean value to indicate whether to overwrite the value of the existing environment variable or not. Defaults to
true
. -
Reads, parses, extracts, and sets enviroment variables from the source of an environment file or throws either
FileError
orSyntaxError
.Throws
FileError
if an environment file being loaded either doesn’t exist or is not encodable orSyntaxError
if the source of an environment file is invalid.Declaration
Swift
public func load(at path: String, encoding: String.Encoding = .utf8, overwrite: Bool = true) throws
Parameters
path
An absolute path to an environment file in the file system.
encoding
An encoding for the source. Defaults to
utf8
.overwrite
A boolean value to indicate whether to overwrite the value of the existing environment variable or not. Defaults to
true
. -
Gets an environment variable.
Declaration
Swift
public func get(_ key: String) -> String?
Parameters
key
The key of an environment variable.
Return Value
The value of an environment variable if an environment variable exists.
-
Sets an environment variable.
Declaration
Swift
public func set(_ value: String?, forKey key: String, overwrite: Bool = true)
Parameters
value
The value of an environment variable. Providing
nil
or an empty string results in an empty string value.key
The key of an environment variable.
overwrite
A boolean value to indicate whether to overwrite the value of the existing environment variable or not. Defaults to
true
. -
Sets multiple environment variables.
Declaration
Swift
public func set(_ variables: [String : String], overwrite: Bool = true)
Parameters
variables
[key: value]
pairs of environment variables.overwrite
A boolean value to indicate whether to overwrite the values of the existing environment variables or not. Defaults to
true
. -
Removes a user-defined environment variable.
Declaration
Swift
public func unset(_ key: String)
Parameters
key
The key of an environment variable.
-
Removes all user-defined environment variables.
Declaration
Swift
public func reset()
-
Gets or sets an environment variable.
Declaration
Swift
public subscript(key: String) -> String? { get set }
Parameters
key
The key of an environment variable.
Return Value
The value of an environment variable if an environment variable exists.
-
Clears cached files and variables.
Declaration
Swift
public func clearCache()