8 releases (breaking)

0.8.0 Dec 24, 2021
0.7.1 Jun 25, 2021
0.6.0 May 26, 2021
0.5.0 May 22, 2021
0.1.0 May 16, 2021

#12 in #dotenv

Download history 584/week @ 2024-01-12 212/week @ 2024-01-19 372/week @ 2024-01-26 280/week @ 2024-02-02 784/week @ 2024-02-09 442/week @ 2024-02-16 443/week @ 2024-02-23 499/week @ 2024-03-01 976/week @ 2024-03-08 864/week @ 2024-03-15 700/week @ 2024-03-22 683/week @ 2024-03-29 628/week @ 2024-04-05 693/week @ 2024-04-12 354/week @ 2024-04-19 491/week @ 2024-04-26

2,376 downloads per month

GPL-3.0-or-later

23KB
393 lines

.zenv

Dotenv (.env) loader written in rust 🦀

build docs crates.io

✨ Features

  • Fast as it is written in rust
  • Use as lib/crate or as a standalone cli
  • Support variable expansion

🚀 Installation

Crate

Add zenv with a version of your choice in the Cargo.toml

[dependencies]
zenv = "<version>" # Make sure it's the latest version

CLI

  • Using cargo
cargo install zenv --features=cli
  • Arch Linux
# Using `yay`
yay -S zenv

# Using `pamac`
pamac build zenv
  • From binaries

Check out the Release page for prebuild binaries for zenv, available for different operating systems.

🤞 Usage

Crate

fn main() {
    zenv::Zenv::new(".env", false).configure().ok();

    // or use macro, which expands to above statement

    zenv::zenv!()
}

Read the full documention

CLI

zenv
Dotenv (.env) loader written in rust

USAGE:
    zenv [FLAGS] [OPTIONS] -- <binary> [args]...

FLAGS:
    -v, --version       Prints version
    -h, --help          Prints help information
    -x, --expand        Enable variable expansion

OPTIONS:
    -f, --file          Path to .env file

ARGS:
    <binary>            Command that needs to be executed
    [args]...           Arguments for the command

Examples:
    zenv -f .env -- node index.js
    zenv -f .env -- npm run dev
    zenv -f .env -- terraform apply

🙌 Good to Know

Basic

PORT=5000
NODE_ENV=production

# Single and double quotes are also supported
S_QUOTE='single_quoted'
D_QUOTE='double_quoted'

Comments

Comments can be added by using # character.

# COMMENTED=commented
AT_THE_END=comment_at_the_end # I am here

# If you want # in you value then wrap the value in single or double quotes
QUOTED="quote_#_quoted" # I'll be removed

New Line and Escaping

New lines can added by new line (\n) character and this only works if the values is surrounded by double quotes.

PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nadflhsdlfsjkldfjklsdjf\n-----END RSA PRIVATE KEY-----"

# or like this
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
adflhsdlfsjkldfjklsdjf
asdffwejdjf983283lk
-----END RSA PRIVATE KEY-----"

If you want to escape the new line character you can use the escape (\)

ESCAPED="escaped\\nnew\\nline"

Substitution

Zenv also supports variable substitution (off by default) from the current file or from the operating system. Substitution only works if the values is double quoted ie.e " and can be achieved by the following:

  • Using ${VAR} pattern (recommended)
  • Starting the variable name by $ character, which terminates after reaching a character which is not _ or alphanumeric.
BASIC=basic
EXPANDED='${BASIC}_expanded' # expands to 'basic_expanded'

# System variables (assuming `PATH` is available)
SYSTEM_VARIABLE="${PATH},/this/is/new/path"

🙏 Credits

Dependencies