Git Product home page Git Product logo

autoenv's Introduction

Autoenv: Directory-based Environments Build Status

Magic per-project shell environments.

What is it?

If a directory contains an .env file, it will automatically be executed when you cd into it. And, if a directory contains an .env.leave file (and AUTOENV_ENABLE_LEAVE is a non-empty string), the file will automatically be executed when cd'ing away from the directory that contains that file.

This is great for...

  • auto-activating virtualenvs
  • auto-deactivating virtualenvs
  • project-specific environment variables
  • making millions

You can also nest envs within each other. How awesome is that!?

When executing, autoenv, will walk up the directories until the mount point and execute all .env files beginning at the top.

Usage

Follow the white rabbit:

$ echo "echo 'whoa'" > ./project/.env
$ cd ./project
whoa

Mind blown GIF

Installation (automated)

# with cURL
curl -#fLo- 'https://raw.githubusercontent.com/hyperupcall/autoenv/master/scripts/install.sh' | sh

# with wget
wget --show-progress -o /dev/null -O- 'https://raw.githubusercontent.com/hyperupcall/autoenv/master/scripts/install.sh' | sh

If you encounter some variant of a curl: command not found or wget: command not found error, please install either cURL or wget (with your package manager) and try again.

Installation (manual)

When installing manually, you first install autoenv with either Homebrew, npm, or Git. Then, you run a command to ensure autoenv is loaded when you open a terminal (this command depends on your default shell).

Installation Method

Note that depending on your shell and operating system, you may need to write to .zprofile instead of .zshrc, or write to .bash_profile instead of .bashrc (or visa-versa).

Using Homebrew

Prefer this if you're running macOS. Homebrew must be installed.

Click to expand content

First, download the autoenv homebrew formulae:

$ brew install 'autoenv'

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source $(brew --prefix autoenv)/activate.sh" >> ~/.bash_profile

Using npm

Prefer this if you're running Linux or an unsupported version of macOS. npm must be installed (usually through NodeJS).

Click to expand content

First, download the @hyperupcall/autoenv npm package:

$ npm install -g '@hyperupcall/autoenv'

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source $(npm root -g)/@hyperupcall/autoenv/activate.sh" >> ~/.bash_profile

Using Git

Use this if you cannot install with Homebrew or npm.

Click to expand content

First, clone this repository:

$ git clone 'https://github.com/hyperupcall/autoenv' ~/.autoenv

Then, execute one of the following to ensure autoenv is loaded when you open a terminal:

# For Zsh shell (on Linux or macOS)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> "${ZDOTDIR:-$HOME}/.zprofile"

# For Bash shell (on Linux)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> ~/.bashrc

# For Bash shell (on macOS)
$ printf '%s\n' "source ~/.autoenv/activate.sh" >> ~/.bash_profile

Configuration

Before sourceing activate.sh, you can set the following variables:

  • AUTOENV_AUTH_FILE: Files authorized to be sourced; defaults to ~/.autoenv_authorized if it exists, otherwise, ~/.local/state/autoenv/authorized_list
  • AUTOENV_NOTAUTH_FILE: Files not authorized to be sourced; defaults to ~/.autoenv_not_authorized if it exists, otherwise, ~/.local/state/autoenv/not_authorized_list (master branch only)
  • AUTOENV_ENV_FILENAME: Name of the .env file; defaults to .env
  • AUTOENV_LOWER_FIRST: Set this variable to a non-empty string to flip the order of .env files executed
  • AUTOENV_ENV_LEAVE_FILENAME: Name of the .env.leave file; defaults to .env.leave
  • AUTOENV_ENABLE_LEAVE: Set this to a non-empty string in order to enable source env when leaving
  • AUTOENV_ASSUME_YES: Set this variable to a non-empty string to silently authorize the initialization of new environments
  • AUTOENV_VIEWER: Program used to display env files prior to authorization; defaults to cat (master branch only)
  • AUTOENV_PRESERVE_CD: Set this variable to a non-empty string to prevent the cd builtin from being overridden (to active autoenv, you must invoke autoenv_init within a cd function of your own) (master branch only)

Shells

autoenv is tested on:

  • Bash
  • Zsh
  • Dash
  • Fish is supported by autoenv_fish
  • More to come

Disclaimer

Autoenv overrides cd (unless AUTOENV_PRESERVE_CD is set to a non-empty string). If you already do this, invoke autoenv_init within your custom cd after sourcing activate.sh.

Autoenv can be disabled via unset -f cd if you experience I/O issues with certain file systems, particularly those that are FUSE-based (such as smbnetfs).

Other info

To uninstall autoenv, see ./docs/uninstall.md.

To update autoenv, see ./docs/updating.md.

Attributions

Autoenv was originally created by @kennethreitz. Later, ownership was transfered to @inishchith. As of August 22nd, 2021, Edwin Kofler (@hyperupcall) owns and maintains the project.

autoenv's People

Contributors

ashokrajar avatar carhartl avatar chunyang-wen avatar cuonglm avatar dasj avatar elzilrac avatar groovecoder avatar hyperupcall avatar inishchith avatar joneskoo avatar joshuamorton avatar jsatt avatar kennethreitz avatar kylef avatar mahmoudhossam avatar mhuggins avatar mkomitee avatar mpapis avatar msztolcman avatar mwarkentin avatar noamt avatar nvie avatar pashaosipyants avatar pimuzzo avatar pravj avatar unixorn avatar waxzce avatar wesleycoder avatar yedpodtrzitko avatar zeveisenberg 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

autoenv's Issues

Can't seem to get it to work.

I'm using it on a virtualenv, installed autoenv via pip and it seems to install correctly.

Inside the virtualenv, I run the example code:

$ touch project/.env
$ echo "echo 'woah'" > project/.env
$ cd project

and.... nothing happens :(
I'm using zsh and I'm on Arch, not sure if this affects this is any way.
Thanks

`autoenv_check_authz_and_run:20: = not found` when entering into a new directory with `.env` file

I've just installed autoenv from source onto Ubuntu Trusty (commit 7858eaa) and it gives me this error when entering into a new directory with .env file:

autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/art/theproject/.env:
autoenv:
autoenv:     --- (begin contents) ---------------------------------------
autoenv:     # my token
autoenv:     TOKEN=*****
autoenv:
autoenv:     --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
autoenv_check_authz_and_run:20: = not found

disable for subfolders

$ cd autoenv_folder # works, activates .env
$ cd autoenv_subfolder # works, activates .env from parent folder

any way to force sourcing of .env only when in folder and not subfolders?

Implement autoenv unload

In general I use autoenv to automatically activate my Python virtualenvs. It works pretty nice, but I miss a feature to automatically deactivate the virtualenv. It should deactivate the virtualenv if I change the directory to a path that is not inside the path which enabled the virtualenv. For example:

mkdir spam
echo "workon spam" > spam/.env
echo "deactivate" > spam/.env-unload
cd spam # run `source .env`, activating virtualenv 'spam'

# working in this directory...
mkdir eggs
cd eggs
touch ham
cd ..
echo "finished" > README.markdown
# finished working.

cd .. # runs `source .env-unload`, deactivating virtualenv

Another approach is to define bash functions inside .env (like autoenv_load and autoenv_unload) and call these functions instead of just sourcing .env/.env-unload.

noclobber triggers bash: cannot overwrite existing file error

When entering an autoenv for the first time, after authorizing the .env file, bash raises:
bash: /home/user/.autoenv_authorized: cannot overwrite existing file

This is triggered by set -o noclobber, which will cause the
\grep -Gv "$envfile:" "$AUTOENV_AUTH_FILE.tmp" > $AUTOENV_AUTH_FILE
in autoenv_deauthorize_env() to fail.

Using >| to force overwriting will make it compatible with noclobber.

autoenv executes even in subfolder

Python project's top folder has .env file with following contents

  source venv/bin/activate

This command executed whenever cd to any sub folder of the project. Then throws

  -bash: venv/bin/activate: No such file or directory

Why it executes even in subfolder?

does not work with spaces in the path

cd ~
mkdir my\ dir
ls
cd my\ dir/
echo "ls -al" > .env
cd ..
cd my\ dir/
cd ..
cd my\ dir/

I get:
/Users/simon/my dir
shasum: dir/.env:
-bash: dir/.env: No such file or directory
shasum: /Users/simon/my:
-bash: /Users/simon/my: No such file or directory

cheers

Incorrect install instructions for OSX in the README

/usr/local/opt/autoenv/activate.sh does not exist when installed via brew (for me at least). Also, if a user has installed brew into a different location, the path to autoenv may be different and brew --prefix should be used.

$(brew --prefix autoenv)/activate.sh should always be the correct path.

Allow .env file to be ignored permanently

I have a use case where I'd like to no longer be prompted to source a particular .env. The particular use case is a Django project template folder where the .env is not valid as it's a Django template w/ {{ project_name }} that is not yet rendered, but there might be other reasons someone wants to ignore a .env file.

Thoughts around implementation would be to add an ignore option to the "Are you sure you want to allow this?" prompt. If the individual chooses to ignore the file, write it to ~/.autoenv_ignored. I don't see a reason to include the hash in .autoenv_ignored, but can if desired. To unignore a .env file you'd have to manually edit .autoenv_ignored, or we'd have to create a function to unignore.

Thoughts on adding this and the implementation plan? I can hack on the code a bit if this sounds like an acceptable plan.

Can I deactivate used env if I cd to folder

Could current activated env be deactivated if I cd to folder which is not subfolder of current directory?

Also if I using such .env:
source oenv/bin/activate
it works when I enter to this folder, but then when I go to subfolder I'm getting:
bash: oenv/bin/activate: No such file or directory
I can fix it using absolute path, but it will be activated twice, isn't it.

Support for Mac OS X Terminal.app new tabs

When I CD into a directory, the autoenv is loaded. But when I open a new tab in Terminal.app, I land in the same directory but the environment script is not run.

I'm not sure if there's a way around this (I guess Terminal.app just doesn't use your cd) but it would be really nice if it was fixed.

Test Failure

I'm running Ubuntu 11.10:

$ make test
dtf tests/*
/bin/sh: dtf: not found
make: *** [test] Error 127

I followed the steps to echo 'woah' for a simple test directory -- blank.

I did not, however, create a virtualenv inside this test dir, though.

Doesn't work when path to directory has spaces.

Bad form on my part to have spaces, but I think this should probably be fixed. Autoenv doesn't seem to work when the path to the working directory has spaces.

If the pwd is "/Users/me/my project/", I'd get an error like:
shasum: project/.env:
autoenv_check_authz_and_run:source:19: no such file or directory: project/.env
shasum: /Users/me/my
autoenv_check_authz_and_run:source:19: no such file or directory: /Users/me/my

Fails on install

brew install autoenv
source /usr/local/opt/autoenv/activate.sh

-bash: /usr/local/opt/autoenv/activate.sh: line 115: syntax error near unexpected token `('
-bash: /usr/local/opt/autoenv/activate.sh: line 115: `cd() {'

"This is the first time you are about to source..." every single time

Every time I enter a folder containing a .env file, I am prompted with "This is the first time you are about to source /Users/mhuggins/Development/project/.env". I answer "Y" every time, and the next time I enter that folder, I'm prompted again. How can I stop this behavior?

autoenv is forcing mc to going halt by warning message

When I first time entered in the directory with .env file, mc is going to respondless state util I type "killall mc". Well, if I already have this .env file in my .autoenv_authorized, autoenv is doesn't do anything as I expecting.
OS: Mac OS X, shell: zsh.

pip installing also needs source, but documentation does not state it

Installed into my system python using pip, and of course I needed to source the activate.sh file, but this is not mentioned in the README.md. Maybe it would be nice for novice users that it needs to be sourced. Of course, the location depends on where pip installs it. For me, on Mac it is:

/usr/local/bin/activate.sh

Global configuration

Is there a way to use a global env configuration with this? I don't want to pollute various directories with .env, and having a global ~/.autoenv.yaml or whatever would be much easier for me to track in my dotfiles repo and sync across computers.

Provide a way to access the default `cd`

I wanted to add this to my .env file;

PROJECT_NAME="$( basename "$( cd "$(dirname "${BASH_SOURCE}" )" && pwd )")";

However, as you can see the line includes a call to cd because of this, the script enters an infinite loop and hangs.

I'm not actually sure if/how this can be improved.

Make work with virtualenvwrapper

I'm not sure if this is a problem with my setup or if it's just a limitation, but it would be nice if I could get autoenv source the .env when I use workon from virtualenvwrapper to change to a project directory. As it is, I have to cd .. and then cd back in to trigger autoenv.

Does not seem to work with Foreman .env files

The docs claim that Foreman .env should work however the standard format of a .env file is KEY=VALUE pairs. It looks like autoenv just sources the .env file so these pairs are not being exported.

Am I doing something wrong or does autoenv need to be a bit smarter and know how to export these pairs?

Return exit status of cd

You may handle this in the Python, but at a quick glance, you aren't returning the original cd exit value at the end of your overloaded cd. This may be deliberate, but it can cause trouble in scripts that rely on cd's exit status.

Compare how rvm handles it maybe.

Compatibility with .rvmrc

Hey,

After brew install autoenv my .rvmrc broke.

Both tools compete for overriding cd, any ideas on this?

use_env() doesn't work on MacOS X

I've tried to use source for use_env() from Cookbook, but it didn't work. There was an error in first if-condition and grep expression didn't work also.

I've fixed it to make it work for me:

use_env() {
  typeset venv
  venv="$1"
  if [[ `basename "${VIRTUAL_ENV:t}"` != "$venv" ]]; then
    if workon | grep $venv > /dev/null; then
      workon "$venv"
    else
      echo -n "Create virtualenv \"$venv\" now? (Yn) "
      read answer
      if [[ "$answer" == "Y" ]]; then
        mkvirtualenv "$venv"
      fi
    fi
  fi
}

By the way, my .env file is:

use_env myenv

Add a utility command for creating a new .env file

Since we're interested in super duper automatic things, why not add a utility funciton to create a generic env file? For example, it could determine the present working directory, then create a file .env with the contents use_env <pwd>.

Something like autoenv_new. Then users could alias it to newenv or even .env.

no shasum

Hi,

no shasum on fedora 16, but sha{1,224,256,384,512}sum. Which one to choose?

Thx,
Antoine.

How to uninstall autoenv?

Hello,

I installed autoenv on Linux Mint 17 with:

$ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
$ echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc

Can you please tell me how to unstinstall and remove related files?

I see there is folder and files now in Home > .autoenv.

Thank You.

How to get the path of the current .env file?

I'm using autoenv with gvm(golang) and there is a command that i must execute only once to create a symlink.
How can i get the path of the .env file? If i get this, i can check inside the gvm if the link is created.

Error when sourcing activate.sh

This error message pops up on OSX (using Bash 4.2.20), OpenBSD (Bash 4.2.10) and Debian (Bash 3.2.39):

dirname: illegal option -- b
usage: dirname path

The problem, I think, stems from the difference between sourcing the file and running it as a script.

See: https://bitbucket.org/dhellmann/virtualenvwrapper/issue/7/virtualenv_wrapper_bin-causes-warning for the same problem and for a potential solution: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in.

Invalid Option Name

I installed autoenv, but I get the following whnever I try to use it

gmp13@dena:CONT10km$ cd GEM
-bash: set: �[01: invalid option name
bash: 31mallexport: command not found...

My shell is bash, I activated it, etc.. I was asked to accept the .env file:

autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/gmp13/gm/experiments/dena/CONT10km/GEM/.env:
autoenv:
...

The contents of my .env file is to source a file, and that file is full of exports

Change without .env file

I did some research on it in the past, and I am now using a bash script I made, which achieves autoenv activation with or without .env file, by replacing cd command, the same way you do.

I made this script picking code from everywhere, and combining them together.

Please, take a look and perhaps you can think of something to improve autoenv.
I use .envs a lot, but sometimes I just forget to create.

https://gist.github.com/lovato/9913038

I wrote a tool to generate a python tree structure (basically CookieCutter does a bigger job) and this tool already creates the .env file. https://github.com/lovato/machete

Best,
Lovato

Check .env is a readable file before trying to use it

% cd                                      github/znc (master ⚡) collision
shasum: /home/kylef/.env: 
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/kylef/.env:
autoenv:
autoenv:     --- (begin contents) ---------------------------------------
sed: read error on /home/kylef/.env: Is a directory
autoenv:     --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) n

Doesn't handle [white]spaces correctly in paths

While I know it's a terrible practice to have spaces in the name of your folders, it should still work. Here is a "test case" demonstrating that it doesn't work for me.

~ % mkdir 'hello world'
~ % cd hello\ world 
hello world % echo 'echo You should print this' > .env

And now I get some weird behaviour:

hello world % cd .
shasum: world/.env: 
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source world/.env:
autoenv:
autoenv:     --- (begin contents) ---------------------------------------
sed: can't read world/.env: No such file or directory
autoenv:
autoenv:     --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
shasum: world/.env: 
autoenv_check_authz_and_run:source:22: no such file or directory: world/.env
shasum: /home/arash/hello: 
autoenv:
autoenv: WARNING:
autoenv: This is the first time you are about to source /home/arash/hello:
autoenv:
autoenv:     --- (begin contents) ---------------------------------------
sed: can't read /home/arash/hello: No such file or directory
autoenv:
autoenv:     --- (end contents) -----------------------------------------
autoenv:
autoenv: Are you sure you want to allow this? (y/N) y
shasum: /home/arash/hello: 
autoenv_check_authz_and_run:source:22: no such file or directory: /home/arash/hello

Also. Thank you so much for this awesome tool!!

Clarify installation instructions

Maybe people should know well enough to spot this, but your installation instructions are dangerous:

# clobbers my ~/.bashrc file
echo 'source ~/.autoenv/activate.sh' > ~/.bashrc


# safe
echo 'source ~/.autoenv/activate.sh' >> ~/.bashrc

Add a new tag with an updated version

I was using homebrew to install the package but the version is from 2 years ago(0.1.0).
If I edit the formula to always link to the master then that may cause problems in the future.

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.