Git Product home page Git Product logo

swift-dotenv's Introduction

SwiftDotenv

A one-stop shop for working with environment values in a Swift program.

Overview

SwiftDotenv is a small and compact Swift package that allows you to load and save .env files at runtime and query for those values as well as system provided environemntal values via ProcessInfo. It's a single abstraction for dealing with environment variables at runtime as well as a handy mechanism keeping your secrets and private keys safe in a local configuration file that doesn't get committed to version control, rather than hardcoding secret strings into your app or framework.

What is a .env file?

.env files are used, most often in server-side applications, to inject environment variables into an application during active development. They can contain api keys, secrets, and other sensitive information and therefore should not be committed to version control. An environment file should only exist locally on a development machine; on a continuous integration system like TravisCI or CircleCI, environment variables are added via the respective UI in lieu of a .env file.

Installation

SwiftDotenv supports Swift Package Manager and can be added by adding this entry to your Package.swift manifest file:

.package(url: "https://github.com/thebarndog/swift-dotenv.git", .upToNextMajor("2.0.0"))

Usage

import SwiftDotenv

// load in environment variables
try Dotenv.configure()

// access values
print(Dotenv.apiSecret)

Dotenv

To configure the environment with values from your environment file, call Dotenv.configure(atPath:overwrite:):

try Dotenv.configure()

It can optionally be provided with a path:

try Dotenv.configure(atPath: ".custom-env")

and the ability to not overwrite currently existing environment variables:

try Dotenv.configure(overwrite: false)

To read values:

let key = Dotenv.apiKey // using dynamic member lookup
let key = Dotenv["API_KEY"] // using regular subscripting

To set new values:

Dotenv.apiKey = .string("some-secret")
Dotenv["API_KEY"] = .string("some-secret")

// set a value and turn off overwriting 
Dotenv.set(value: "false", forKey: "DEBUG_MODE", overwrite: false)

The Dotenv structure can also be given a custom delimeter, file manager, and process info:

Dotenv.delimeter = "-" // default is "="
Dotenv.processInfo = ProcessInfo() // default is `ProcessInfo.processInfo`
Dotenv.fileManager = FileManager() // default is `FileManager.default`

Contributing

If you find a bug, have an idea for a feature request, or want to help out, please open an issue describing your problem or a pull request with your feature. Please follow the Code of Conduct at all times.

Made with Swift and ❤️.

swift-dotenv's People

Contributors

thebarndog avatar keanupang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.