Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to handle .env files #69

Open
arifmahmudrana opened this issue Aug 17, 2017 · 5 comments
Open

Add helper function to handle .env files #69

arifmahmudrana opened this issue Aug 17, 2017 · 5 comments

Comments

@arifmahmudrana
Copy link

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 *.env files or at least .env file & when I move to parent directory it will restore the previous values of env variable that was modified e.g PATH 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 hyperupcall/autoenv#30 (comment) 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
Copy link
Collaborator

blueyed commented Aug 17, 2017

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
Copy link
Author

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
Copy link
Collaborator

blueyed commented Aug 18, 2017

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
Copy link
Author

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

@blueyed blueyed changed the title [Question]How to add env & remove/unload the env added & restore previous one Add helper function to handle .env files Sep 14, 2018
@otosky
Copy link

otosky commented Oct 23, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants