Code Monkey home page Code Monkey logo

zenv's Introduction

.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] -- <command> [args]...

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

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

ARGS:
    <command>            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"

Export and sourcing

You may also add export in front of each line so you can source the file in bash:

export BASIC=BASIC

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

zenv's People

Contributors

berkes avatar dependabot[bot] avatar j-m-hoffmann avatar numtostr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

j-m-hoffmann djun

zenv's Issues

Allow "export FOO=bar" in .env to be able to source it in bash.

Thanks for the fantastic implementation of a .env loader. It's speed, simplicity and portability make it far superior to the ruby, java- and typescript versions I had to use before.

I'd like to suggest a feature-requests. I can work on them, if wanted. I am not sure if this is the right place to propose such a feature-request, though. Feel free to just close or delete it, if this not the right place.

In order to make my .env files a tad more portable, I used to write them with an export on each line. From bkeepers/dotenv:

You may also add export in front of each line so you can source the file in bash:

export S3_BUCKET=YOURS3BUCKET
export SECRET_KEY=YOURSECRETKEYGOESHERE

The benefit is that you can easily source this in bash. source .env.test to have the env vars in the session.

Zenv ignores these entries. It would be nice if it either fails with an error, when not reading such a line, or, better even, if it parses these too; simply ignoring the "export".

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.