Giter Site home page Giter Site logo

venthur / dotenv-cli Goto Github PK

View Code? Open in Web Editor NEW
26.0 3.0 7.0 166 KB

Executes commands with environment variables set from .env file. Zero dependencies.

Home Page: https://dotenv-cli.readthedocs.io

License: MIT License

Makefile 10.64% Python 83.38% Shell 5.98%
dotenv python cli bash-completion dotenv-cli debian

dotenv-cli's Introduction

dotenv CLI

Dotenv-CLI provides the dotenv command. dotenv loads the .env file from the current directory, puts the contents in the environment by either changing existing- or adding new environment variables, and executes the given command.

dotenv supports alternative .env files like .env.development via the -e or --dotenv parameters.

With the --replace flag, dotenv also provides an option to completely replace the environment variables with the ones from the .env file, allowing you to control exactly which environment variables are set.

dotenv provides bash completion, so you can use dotenv like this:

$ dotenv make <TAB>
all      clean    docs     lint     release  test

Install

Using PyPi

dotenv-cli is available on PyPi, you can install it via:

$ pip install dotenv-cli

On Debian and Ubuntu

Alternatively, you can install dotenv-cli on Debian based distributions via:

# apt-get install dotenv-cli

Usage

Create an .env file in the root of your project and populate it with some values like so:

SOME_SECRET=donttrythisathome
SOME_CONFIG=foo

Just prepend the command you want to run with the extra environment variables from the .env file with dotenv:

$ dotenv some-command

and those variables will be available in your environment variables.

Rules

The parser understands the following:

  • Basic unquoted values (BASIC=basic basic)
  • Lines starting with export (export EXPORT=foo), so you can source the file in bash
  • Lines starting with # are ignored (# Comment)
  • Empty values (EMPTY=) become empty strings
  • Inner quotes are maintained in basic values: INNER_QUOTES=this 'is' a test or INNER_QUOTES2=this "is" a test
  • White spaces are trimmed from unquoted values: TRIM_WHITESPACE= foo and maintained in quoted values: KEEP_WHITESPACE=" foo "
  • Interpret escapes (e.g. \n) in double quoted values, keep them as-is in single quoted values.

Example .env file:

BASIC=basic basic
export EXPORT=foo
EMPTY=
INNER_QUOTES=this 'is' a test
INNER_QUOTES2=this "is" a test
TRIM_WHITESPACE= foo
KEEP_WHITESPACE="  foo  "
MULTILINE_DQ="multi\nline"
MULTILINE_SQ='multi\nline'
MULTILINE_NQ=multi\nline
#
# some comment

becomes:

$ dotenv env
BASIC=basic basic
EXPORT=foo
EMPTY=
INNER_QUOTES=this 'is' a test
INNER_QUOTES2=this "is" a test
TRIM_WHITESPACE=foo
KEEP_WHITESPACE=  foo
MULTILINE_DQ=multi
line
MULTILINE_SQ=multi\nline
MULTILINE_NQ=multi\nline

dotenv-cli's People

Contributors

dependabot[bot] avatar mymedia2 avatar venthur 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

Watchers

 avatar  avatar  avatar

dotenv-cli's Issues

Support cascading/multiple env files option

I want such option that npm dotenv-cli package have like below

Borrow from npm dotenv-cli package documentation:

Cascading env variables

Some applications load from .env, .env.development, .env.local, and .env.development.local (see #37 for more information). dotenv-cli supports this using the -c flag for just .env and .env.local and -c development for the ones above. The -c flag can be used together with the -e flag. The following example will cascade env files located one folder up in the directory tree (../.env followed by ../.env.local):

dotenv -e ../.env -c

multiple env variables

Multiple .env files can be specified, and will be processed in order:

$ dotenv -e .env3 -e .env4 -- <command with arguments>

Add option to replace environment instead of adding to it

Current behavior is to add new environment variables and replace existing ones

    # update env
    env = os.environ.copy()
    env.update(dotenv)

I would like to have some flag to completely replace environemnt.

It would be useful to run build scripts, such as unreal engine build tools, which triggers full rebuild of all sources in case if any environment variable has changed.

And this might thake up to 3 hours.

Unicode support is broken in double quoted values

As described in the Python documentation the unicode_escape text encoding interprets the bytes as ASCII (or rather ISO-8859-1, I think). Meaning if you write any Unicode characters in a doubled quoted string the result will be the typical "UTF-8 interpreted as ISO-8859-1" encoding error.

Meaning this .env file:

FOO=ä
BAR="ä"
BAZ='ä'

Is equivalent to this JSON:

{ "FOO": "ä", "BAR": "ä", "BAZ": "ä" }

The line in question:

value = bytes(value, "utf-8").decode("unicode-escape")

The only way to fix this is to implement escape sequence parsing by hand.

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.