Skip to content

Swift library that loads environment variables from .env files into the environment

License

Notifications You must be signed in to change notification settings

happn-app/SwiftDotEnv

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftDotEnv

Swift library that loads environment variables from .env into the environment inspired by the Ruby dotenv and PHP dotenv projects.

Why?

Storing configuration in the environment is one of the tenets of a twelve-factor app. Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.

But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. SwiftDotEnv loads variables from a .env file into ENV when the environment is bootstrapped.

Installation

Install using Swift Package Manager.

Usage

import DotEnv

DotEnv.loadDotEnvFile()
/* Or, optionally specifying the path:
     DotEnv.loadDotEnvFile(path: ".env") */

let host = DotEnv.get("DB_HOST") ?? "localhost"
let port = DotEnv.getAsInt("DB_PORT") ?? 3306
let isEnabled = DotEnv.getAsBool("IS_ENABLED") ?? true

An example .env file would be:

DB_HOST=test.com
DB_PORT=1234
IS_ENABLED=0

There are three getter methods:

  • get() returns a String?

  • getAsInt() returns an Int?

  • getAsBool() returns a Bool? where case-insensitive "true", "yes" and "1" evaluate to true

You can also use subscript access to retrieve the string version:

let host = DotEnv["DB_HOST"] ?? "localhost"

As a convenience, you can use env.all to retrieve all environment variables.

Note the .env file is referenced relative to the directory where the binary is executed from.

Caveats

Currently has a very naive parser of the .env file and so doesn’t support multi-line values.

Contribute

Contributions welcome! Please put your changes in a separate branch from master and raise a PR.

Credits

This project is a fork of https://github.com/SwiftOnTheServer/SwiftDotEnv which has been discontinued.
We’ll do our best to keep it updated when need be!

About

Swift library that loads environment variables from .env files into the environment

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 100.0%