Swiftpack.co -  noahkamara/SwiftDotEnv as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
noahkamara/SwiftDotEnv
Swift 5 library for accessing environment variables from .env files
.package(url: "https://github.com/noahkamara/SwiftDotEnv.git", from: "v1.0.0")

SwiftDotEnv

Swift 5 library for accessing environment variables from .env files

Installation

Install using Swift Package Manager.

Add Dependency:

.package(name: "SwiftDotEnv", url: "https://github.com/noahkamara/swiftdotenv", from: "1.0.0")

Usage

import SwiftDotEnv

let envPath = ".env" // or absolute path
let env = DotEnv(withFile: envPath)

// Retrieve variable 'VAR' and default to "DEFAULTVALUE"
let var = env.value("VAR", "DEFAULTVALUE")

Getter Methods:

value(_ key: String, _ default: String? = nil) -> String? 

Returns the value forkey in the environment, returning default if not present

  • Parameter key: Variable key
  • Parameter default: Default value
int(for key: String, _ default: Int? = nil) -> Int?

Returns the integer value for key in the environment, returning default if not present

  • Parameter key: Variable key
  • Parameter default: Default value
bool(for key: String, _ default: Bool? = nil) -> Bool?

Returns the boolean value for key in the environment, returning default if not present

  • Parameter key: Variable key
  • Parameter default: Default value

Subscript Access:

You can also access variables by subscript (this will return a string!)

let var = env["VAR"]

The .env-file

This is an example for a .env file and also all supported types:

# COMMENT
STRING=ThisIsAString # Inline Comment
STRING_QUOTMARK="String with"
INT=69
BOOL_TRUE=true
BOOL_TRUE_INT=1
BOOL_TRUE_STR=yes
BOOL_FALSE=false
BOOL_FALSE_INT=0
BOOL_FALSE_STR=no

Support for Values-Types

Comments

# This is a comment
KEY=VALUE

Inline Comments

KEY=VALUE # Inline Comment

Strings

STRING=ThisIsASupportedString
QUOTES="This is also a supported String"

Integers

INTEGER=42

Booleans

# Will be evaluated as true:
BOOL1=true
BOOL2=1
BOOL3=yes

# Will be evaluated as false
BOOL4=false
BOOL5=0
BOOL6=no

GitHub

link
Stars: 0
Last commit: Yesterday

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Related Packages

Release Notes

1.0.0
3 days ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API