GithubHelp home page GithubHelp logo

thepredators / swiftdotenv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swiftontheserver/swiftdotenv

0.0 1.0 0.0 21 KB

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

License: MIT License

Swift 100.00%

swiftdotenv's Introduction

SwiftDotEnv

⚠️ This repository is archived as I'm no longer using Swift on Linux and so maintaining isn't appropriate.

Swift 4 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.

Swift version

Version 2 of this module has been tested with Swift 4.0. Version 1 has been tested with Swift 3.0 and 3.1.

Usage

import DotEnv

let env = DotEnv(withFile: ".env")

let host = env.get("DB_HOST") ?? "localhost"
let port = env.getAsInt("DB_PORT") ?? 3306
let isEnabled = env.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 = env["DB_HOST"] ?? "localhost"

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

Note that 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.

swiftdotenv's People

Contributors

akrabat avatar mdiasdev avatar dtweston avatar thantthet avatar

Watchers

James Cloos 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.