GithubHelp home page GithubHelp logo

rust-dotenv's Introduction

rust-dotenv Build Status

Achtung! This is a v0.* version! Expect bugs and issues all around. Submitting pull requests and issues is highly encouraged!

Quoting bkeepers/dotenv:

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.

This library is meant to be used on development or testing environments in which setting environment variables is not practical. It loads environment variables from a .env file, if available, and mashes those with the actual environment variables provided by the operative system.

Usage

The easiest and most common usage consists on calling dotenv::dotenv when the application starts, which will load environment variables from a file named .env in the current directory or any of its parents; after that, you can just call the environment-related method you need as provided by std::os.

If you need finer control about the name of the file or its location, you can use the from_filename and from_path methods provided by the crate.

dotenv_codegen and dotenv_macros also provide the dotenv! macro, which behaves identically to env!, but first tries to load a .env file at compile time.

Examples

A .env file looks like this:

# a comment, will be ignored
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42

You can optionally prefix each line with the word export, which will conveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

extern crate dotenv;

use dotenv::dotenv;
use std::env;

fn main() {
    dotenv().ok();

    for (key, value) in env::vars() {
        println!("{}: {}", key, value);
    }
}

Using the dotenv! macro on nightly

Add dotenv_macros to your dependencies, and add #![plugin(dotenv_macros)] to the top of your crate.

Using the dotenv! macro on stable

You can use dotenv! on stable using syntex. You'll need to add dotenv_codegen and syntex to your build dependencies.

In main.rs:

include!(concat!(env!("OUT_DIR"), "/main.rs"));

In main.in.rs:

fn main() {
    println!("{}", &dotenv!("MEANING_OF_LIFE"));
}

In build.rs:

extern crate syntex;
extern crate dotenv_codegen;

use std::env;
use std::path::Path;

pub fn main() {
    let out_dir = env::var_os("OUT_DIR").unwrap();
    let mut registry = syntex::Registry::new();
    dotenv_codegen::register(&mut registry);

    let src = Path::new("tests/main.in.rs");
    let dst = Path::new(&out_dir).join("main.rs");

    registry.expand("", &src, &dst).unwrap();
}

In .env:

MEANING_OF_LIFE=42

rust-dotenv's People

Contributors

chills42 avatar sgrif avatar alicemaz avatar mfpiccolo avatar shepmaster avatar barosl avatar mcasper avatar mrmaxmeier avatar nashenas88 avatar golddranks avatar sergiobenitez avatar elifoster avatar

Watchers

Steve Klabnik avatar James Cloos avatar  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.