Code Monkey home page Code Monkey logo

dotenv.nim's Introduction

dotenv.nim Build Status

dotenv implementation for Nim. Loads environment variables from .env

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.

Installation

dotenv can be installed using Nimble:

nimble install dotenv

Usage

Create a .env file

Create a .env file in the root of your project (or anywhere else - just be sure to specify the path when loading).

DB_HOST=localhost
DB_USER=root
DB_PASS=""
DB_NAME=test

Variables values are always strings, and can be either wrapped in quotes or left without.

Multiline strings can also be used using the """ syntax:

SOME_LONG_STRING="""This string is very long.

It will span multiple lines.
"""

You can also add comments, using the # symbol:

# Comments can fill a whole line
DB_NAME=test # Or they can follow an assignment

Loading the .env file

You can load the .env file from the current working directory as follows:

import dotenv

let env = initDotEnv()
env.load()

# You can now access the variables using os.getEnv()

Or, you can specify the path to the directory and/or file:

import dotenv

let env = initDotEnv("/some/directory/path", "custom_file_name.env")
env.load()

# You can now access the variables using os.getEnv()

By default, dotenv does not overwrite existing environment variables, though this can be done using overload rather than load:

import dotenv

let env = initDotEnv()
env.overload()

# You can now access the variables using os.getEnv()

Loading from a string

You can also load environment variables directly from a string without instantiating a DotEnv instance:

import dotenv, os

loadEnvFromString("""hello = world
    foo = bar
    """)
assert getEnv("foo") == "bar"

Planned features

  • Allow the usage of other environment variables inside variable values.
  • Add validation of variable values, specifying variables have to be integer, or boolean, or a value from a predefined set.

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.