Tarrasch / zsh-autoenv

Autoenv for zsh
700 stars 30 forks source link

Add helper function to handle .env files #69

Open arifmahmudrana opened 7 years ago

arifmahmudrana commented 7 years ago

After seeing the docs I am not fully clear so I am asking this question.

I have a .env or .env files e.g production.env or local.env in a repository where my different configs & credentials stored. I am looking for whenever I cd into that directory I want to load my all ```.envfiles or at least.envfile & when I move to parent directory it will restore the previous values of env variable that was modified e.gPATH``` variable & will remove the other ones that were added.

I am using oh-my-zsh I have tried autoenv but that didn't work also I saw a comment from this repos contributor @blueyed https://github.com/kennethreitz/autoenv/issues/30#issuecomment-149721449 where he says this unload feature is available in this project.

Also in oh-my-zsh when we type a directory & press enter & it just cd into that directory, no need to type cd in oh-my-zsh. So is it already implemented to load .env files without use cd just typing the directory name & going into the directory.

Thanks

blueyed commented 7 years ago

Yes, files are loaded regardless of how you cd.

I suggest having a single .autoenv file (I am using AUTOENV_FILE_LEAVE=.autoenv.zsh to have the same file for entering and leaving).

You can use something like the following then:

local root=${autoenv_env_file:h}
if [[ $autoenv_event == 'enter' ]]; then
  autoenv_source_parent

  (( $+functions[zsh_setup_pyenv] )) && zsh_setup_pyenv

  local envdir=$root/envdir

  autostash PATH=$root/bin:$PATH

  # Simulate "envdir envdir $SHELL".
  # echo "Exporting env from $envdir."
  for i in $envdir/*~*.*(.); do
    # echo "Exporting $i:t"
    # eval "export ${i:t}='$(<$i)'"
    eval "autostash ${i:t}='$(<$i)'"
  done

  autostash alias pi='p --dynamodb-test-url http://localhost:9000/'
fi

Using autostash ensures that the values are restored when leaving the directory.

Check out export AUTOENV_DEBUG=1 to see what is going on behind the scenes.

If you feel that the doc can be improved, a PR for this is welcome of course.

arifmahmudrana commented 7 years ago

Thanks @blueyed for your reply. Sorry I couldn't understand the code & also can you explain how to use this code.

I have gone through Variable stashing & it did work but first for every .env file in every line I can't add stash word e.g I am using Laravel so there is already an .env file & what I want is to load the .env file.

Thanks once again

blueyed commented 7 years ago

You would put the code from above in an .autoenv file. How does an .env file look? Lines of ENV=value?

You would have to read that and apply autostash on it - similar to the "Simulate "envdir envdir $SHELL"." code from above. We could add a helper method for this.

arifmahmudrana commented 7 years ago

How does an .env file look? Lines of ENV=value?

Yes, you can check this https://github.com/laravel/laravel/blob/master/.env.example also in other languages like nodejs or go all uses in this format ENV=value

You would put the code from above in an .autoenv file

Yes I can put the code above but my php package that's also reading .env file will malfunction

We could add a helper method for this

sounds good

otosky commented 2 years ago

I think I have the same question as OP where I would like to convert this .autoenv.zsh script from being per-dir to being global from a parent directory:

if [[ $autoenv_event == 'enter' ]]; then
  if [[ -f .env ]]; then
    autostash $(sed '/^#/d' .env | xargs)
  fi
fi

Is there a recipe to getting this behavior for any subdir of $HOME, if the above code is placed in a file at $HOME/.autoenv.zsh? I tried naively adding the autostash snippet to the python venv example, and the variables get stashed, but they won't unstash on leaving the directory.