A swift DotEnv file loader inspired by vlucas/phpdotenv.
This swift package enables you to quickly and easily use a .env file in your swift project today.
Using SwiftNIO in project?
Don't worry, you can use NonBlockingFileIO to ensure everything runs smoothly.
You can easily add as a requirement with SwiftPM.
Here are some quick copypastas for you
.package(url: "https://github.com/swiftpackages/DotEnv.git", from: "3.0.0"),.product(name: "DotEnv", package: "DotEnv"),Your Package.swift file should look something like this
Package.swift
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "SuperCoolProject",
products: [
.library(
name: "SuperCoolProject",
targets: ["SuperCoolProject"]),
],
dependencies: [
.package(url: "https://github.com/swiftpackages/DotEnv.git", from: "3.0.0"),
],
targets: [
.target(
name: "SuperCoolProject",
dependencies: [
.product(name: "DotEnv", package: "DotEnv"),
]),
.testTarget(
name: "SuperCoolProject",
dependencies: ["SuperCoolProject"])
]
)Read and then load
let path = "path/to/your/.env"
var env = try DotEnv.read(path: path)
env.lines // [Line] (key=value pairs)
env.load()
print(ProcessInfo.processInfo.environment["FOO"]) // BARor
Just load
let path = "path/to/your/.env"
var env = try DotEnv.load(path: path)
env.lines // [Line] (key=value pairs)
print(ProcessInfo.processInfo.environment["FOO"]) // BARDo you have two .env files you wish to load? You can do that without breaking a sweat.
By using a load method with a suffix parameter you can easily load your shared .env.development file, and then overwrite anything you need to with your local .env file.
let path = "path/to/your/.env"
let suffix = "development"
try DotEnv.load(path: path, suffix: suffix)
print(ProcessInfo.processInfo.environment["FOO"]) // BARRead and then load
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let pool = NIOThreadPool(numberOfThreads: 1)
pool.start()
let fileio = NonBlockingFileIO(threadPool: pool)
let env = try DotEnv.read(path: filePath, fileio: fileio, on: elg.next()).wait()
env.load(overwrite: true)
print(ProcessInfo.processInfo.environment["FOO"]) // BAR
try pool.syncShutdownGracefully()
try elg.syncShutdownGracefully()or
Just load
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let pool = NIOThreadPool(numberOfThreads: 1)
pool.start()
let fileio = NonBlockingFileIO(threadPool: pool)
DotEnv.load(path: filePath, fileio: fileio, on: elg.next()).wait()
print(ProcessInfo.processInfo.environment["FOO"]) // BAR
try pool.syncShutdownGracefully()
try elg.syncShutdownGracefully()You can find the full documentation on the documentation website.
A signficant portion of this project comes from vapor/vapor under The MIT License.