- Notifications
You must be signed in to change notification settings - Fork53
Library to help supply environment variables in testing and development
License
dotenv-rs/dotenv
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Achtung! This is a v0.* version! Expect bugs and issues all around.Submitting pull requests and issues is highly encouraged!
Quotingbkeepers/dotenv:
Storingconfiguration in the environmentis one of the tenets of atwelve-factor app.Anything that is likely to change between deployment environments–such asresource handles for databases or credentials for external services–shouldbe extracted from the code into environment variables.
This library is meant to be used on development or testing environments inwhich setting environment variables is not practical. It loads environmentvariables from a.env
file, if available, and mashes those with the actualenvironment variables provided by the operative system.
The easiest and most common usage consists on callingdotenv::dotenv
when theapplication 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 callthe environment-related method you need as provided bystd::os
.
If you need finer control about the name of the file or its location, you canuse thefrom_filename
andfrom_path
methods provided by the crate.
dotenv_codegen
provides thedotenv!
macro, whichbehaves identically toenv!
, but first tries to load a.env
file at compiletime.
A.env
file looks like this:
# a comment, will be ignoredREDIS_ADDRESS=localhost:6379MEANING_OF_LIFE=42
You can optionally prefix each line with the wordexport
, which willconveniently allow you to source the whole file on your shell.
A sample project using Dotenv would look like this:
externcrate dotenv;use dotenv::dotenv;use std::env;fnmain(){dotenv().ok();for(key, value)in env::vars(){println!("{}: {}", key, value);}}
It's possible to reuse variables in the.env
file using$VARIABLE
syntax.The syntax and rules are similar to bash ones, here's the example:
VAR=oneVAR_2=two# Non-existing values are replaced with an empty stringRESULT=$NOPE#value: '' (empty string)# All the letters after $ symbol are treated as the variable name to replaceRESULT=$VAR#value: 'one'# Double quotes do not affect the substitutionRESULT="$VAR"#value: 'one'# Different syntax, same resultRESULT=${VAR}#value: 'one'# Curly braces are useful in cases when we need to use a variable with non-alphanumeric nameRESULT=$VAR_2#value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbolRESULT=${VAR_2}#value: 'two'# The replacement can be escaped with either single quotes or a backslash:RESULT='$VAR'#value: '$VAR'RESULT=\$VAR#value: '$VAR'# Environment variables are used in the substutution and always override the local variablesRESULT=$PATH#value: the contents of the $PATH environment variablePATH="My local variable value"RESULT=$PATH#value: the contents of the $PATH environment variable, even though the local variable is defined
Dotenv will parse the file, substituting the variables the way it's described in the comments.
Adddotenv_codegen
to your dependencies, and add the following to the top ofyour crate:
#[macro_use]externcrate dotenv_codegen;
Then, in your crate:
fnmain(){println!("{}", dotenv!("MEANING_OF_LIFE"));}
About
Library to help supply environment variables in testing and development
Resources
License
Uh oh!
There was an error while loading.Please reload this page.