8000 GitHub - yuliyv/dotenv.nim: dotenv implementation for Nim. Loads environment variables from `.env`
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

yuliyv/dotenv.nim

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

dotenv implementation for Nim. Loads environment variables from `.env`

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Nim 100.0%
0