tfutils / tfenv

Terraform version manager
MIT License
4.48k stars 454 forks source link

Creating an environment variable bringing the current tfenv version name ? #298

Closed ginolegigot closed 2 years ago

ginolegigot commented 2 years ago

Hello! When we activate a pyenv, an environment variable named VIRTUAL_ENV is created. This would be very convenient to have the same feature with tfenv, especially to print the current used tf version in a zsh prompt. Do you think it would be possible to implement such a feature ?

OJFord commented 2 years ago

Not an environment variable, but you can get it from tfenv list | grep '^*' | cut -d' ' -f2.

I'm not sure it would work that well as an env var, since the results could be unexpected (as it would only change on tfenv invocations`) - for example:

$ cd ../A && tfenv use 0.14.0
$ cd ../B && tfenv use 1.0.0
$ cd ../A && echo $TFENV_HYPOTHETICAL_VERSION_VAR
1.0.0
ginolegigot commented 2 years ago

Thank you for your answer, I ended up writing a function i pasted in my .zshrc.

function tfu {
  tfenv use $1 && export TFENVVERSION=$(tfenv version-name)
}

So that after each tfuse i'm sure to have a proper environment TFENVVERSION environment variable i can quickly obtain which enhances a lot my powerlevel10k prompt customization. Cheers!

OJFord commented 2 years ago

If you only change version by tfenv use I suppose that's fine, just beware of the caveat I mentioned above if you might ever use a .terraform-version file, or if you tfenv use min-required for that matter.

ginolegigot commented 2 years ago

All right, thanks!