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
DotEnvwith the defaultconfiguration.Declaration
Swift
public init(configuration: Configuration = .init())Parameters
configurationA configuration for
DotEnv. -
Reads the source of an environment file or throws
FileError.Throws
FileErrorif 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 -> FileParameters
pathAn absolute path to an environment file in the file system.
encodingAn encoding for the source. Defaults to
utf8.pathAn 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
SyntaxErrorif the source of an environment file is invalid.Declaration
Swift
public func parseFile(_ file: File) throws -> [String : String]Parameters
fileAn 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
FileErrororSyntaxError.Throws
FileErrorif an environment file being loaded either doesn’t exist or is not encodable orSyntaxErrorif the source of an environment file is invalid.Declaration
Swift
public func parseFile(at path: String, encoding: String.Encoding = .utf8) throws -> [String : String]Parameters
pathAn absolute path to an environment file in the file system.
encodingAn 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
FileErrororSyntaxError.Throws
FileErrorif an environment file being loaded either doesn’t exist or is not encodable orSyntaxErrorif the source of an environment file is invalid.Declaration
Swift
public func load(_ file: File, overwrite: Bool = true) throwsParameters
fileAn instance of
File.overwriteA 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
FileErrororSyntaxError.Throws
FileErrorif an environment file being loaded either doesn’t exist or is not encodable orSyntaxErrorif the source of an environment file is invalid.Declaration
Swift
public func load(at path: String, encoding: String.Encoding = .utf8, overwrite: Bool = true) throwsParameters
pathAn absolute path to an environment file in the file system.
encodingAn encoding for the source. Defaults to
utf8.overwriteA 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
keyThe 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
valueThe value of an environment variable. Providing
nilor an empty string results in an empty string value.keyThe key of an environment variable.
overwriteA 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.overwriteA 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
keyThe 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
keyThe 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()
View on GitHub
Install in Dash
DotEnv Class Reference