warrensbox/terraform-switcher

Add an auto install / auto switch mode like tfenv

Closed this issue ยท 6 comments

Is your feature request related to a problem? Please describe.
Unless I didn't find it, it seems that tfswitch doesn't have an auto install / auto switch mode like tfenv, so the current situation is forcing us to type the tfswitch command to install or switch to the right version.

Describe the solution you'd like
I would like to do this :
Current situation

$ tfswitch 1.1.0
Switched terraform to version "1.1.0"
$ terraform --version
Terraform v1.1.0
$ cd myapp/terraform/
$ terraform --version
Terraform v1.1.0
$ tfswitch
Reading required version from terraform file
Reading required version from constraint: >= 1.0.3
Matched version: 1.2.9
Switched terraform to version "1.2.9"
$ terraform --version
Terraform v1.2.9

What I would like :

$ tfswitch 1.1.0
Switched terraform to version "1.1.0"
$ terraform --version
Terraform v1.1.0
$ cd myapp/terraform/
$ terraform --version
Reading required version from terraform file
Reading required version from constraint: >= 1.0.3
Matched version: 1.2.9
Switched terraform to version "1.2.9"
Terraform v1.2.9

Describe alternatives you've considered
For the moment, I use tfenv which works like this, BUT which doesn't have a nice semantic version parsing, so it installs the min-required version instead of the latest configured in terraform.tf

Additional context
It would be very nice to have this feature since it would easier to use tfswitch in a CI like a dropdown replacement of tfenv or classical terraform , without having to add tfswitch command before each terraform command.

Thanks in advance and thanks for your work !

without having to add tfswitch command before each terraform command.

You only need to run it once before any and all TF commands in a particular directory with TF version constraint setup properly.
Or am I getting the request incorrectly?

yes, but adding this command after changing directories is not user-friendly. tfenv does this automatically, and I would like that my cicd scripts work the same way with classical terraform (unique version for all the directory of a project), or with tfenv or tfswitch (different version of terraform for each directory of a project).

Thanks !

adding this command after changing directories is not user-friendly

I do understand your request, though it's more of a "Unix Philosophy of One Tool for One Job" ("one tool doing one job well") from my personal point of view and tfswitch can be leveraged like that: https://github.com/warrensbox/terraform-switcher#automation

Hi
I just want to do that

cd my-first-app
terraform --version # == version defined in terraform.tf
cd ../my-second-app
terraform --version # == another version defined in terraform.tf

instead of

cd my-first-app
tfswitch
terraform --version # == version defined in terraform.tf
cd ../my-second-app
tfswitch
terraform --version # == another version defined in terraform.tf

Maybe it's not quite possible, or maybe, you would not alter tfswitch to do this ... tfenv does this, but it doesn't work well and it doesn't have your powerfull detection of last version ๐Ÿ˜„

Anyways, thanks for your quick answer and thanks for your work.

@tpoindessous What tfenv actually does is "substitute" terraform binary by placing terraform shell script into user's local bin dir and prepend PATH with local bin dir path, so when you run terraform you actually run a thin terraform wrapper which does the thing and executes actual terraform binary with args passed by user:

This is actually what https://github.com/warrensbox/terraform-switcher#automation suggests though in a less implicit manner.

You may want to go this path and create shell alias or function to mimic tfenv's behavior like below, though I'd stick with an explicit run of tfswitch command in your CI to eliminate any possible confusion (this below example is for Bash):

> terraform() {
  if grep -qs "required_version" *.tf; then
    echo "==> tfswitching"
    tfswitch
    echo "<=="
  fi
  command -p terraform "$@"
}
> export -f terraform
> type terraform
terraform is a function
terraform ()
{
    if grep --color=auto -qs "required_version" *.tf; then
        echo "==> tfswitching";
        tfswitch;
        echo "<==";
    fi;
    command terraform "$@"
}
> cd /path/to/dir/with/terraform/code/
> terraform version
==> tfswitching
Reading configuration from home directory for .tfswitch.toml
Reading required version from terraform file
Reading required version from constraint: ~> 1.1.0
Matched version: 1.1.9
Switched terraform to version "1.1.9"
<==
Terraform v1.1.9
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v4.20.1
+ provider registry.terraform.io/hashicorp/external v2.2.2

Your version of Terraform is out of date! The latest version
is 1.2.9. You can update by downloading from https://www.terraform.io/downloads.html

The function above is an example of what you can do on your end, though I'd suggest to hook it to cd command like https://github.com/warrensbox/terraform-switcher#automation suggests to allow running tfswitch only once on dir change and avoid running it on every single terraform command. Please use it at your own risk!

whaou thanks a lot for your answer. Now I understand a little more about tfswitch, thanks for your patience.

You're right, now I will be explicit in my scripts and in my CI than we want tfswitch ๐Ÿ‘

Thanks for your work and have a nice weekend !