terraform-switcher icon indicating copy to clipboard operation
terraform-switcher copied to clipboard

tfwitch blindly removes default terraform symlink, when bin is passed and version does not exist locally

Open miguelhar opened this issue 3 years ago • 6 comments

Consider the following scenario, we want to have 2 versions of terraform with 2 different binary values.

  • terraform
  • terraform0.12
$ls -ltra /usr/local/bin/terraform*
/usr/local/bin/terraform -> ~/.terraform.versions/terraform_1.0.6
/usr/local/bin/terraform0.12 -> ~/.terraform.versions/terraform_0.12.31

changing versions for terraform0.12 behaves as expected since version 0.12.31 is present(in ~/.terraform.versions/):

$tfswitch --bin=/usr/local/bin/terraform0.12 0.12.31
Switched terraform to version "0.12.31"
$ls -ltra /usr/local/bin/terraform*
/usr/local/bin/terraform -> ~/.terraform.versions/terraform_1.0.6
 /usr/local/bin/terraform0.12 -> ~/.terraform.versions/terraform_0.12.31

Now we will try with version 0.12.30 which had not been downloaded...

$tfswitch --bin=/usr/local/bin/terraform0.12 0.12.30
Downloading to: ~/.terraform.versions
29172646 bytes downloaded
Switched terraform to version "0.12.30"

As you can see the symlink /usr/local/bin/terraform -> ~/.terraform.versions/terraform_1.0.6 is removed:

$ls -ltra /usr/local/bin/terraform*
 /usr/local/bin/terraform0.12 -> ~/.terraform.versions/terraform_0.12.30

miguelhar avatar Feb 09 '22 19:02 miguelhar

I believe the culprit is that the path is being built based on the outcome of terraform when in reality this will be different depending on the value of --bin: https://github.com/warrensbox/terraform-switcher/blob/ea69e4d842b42e108ce6d2fbb164104c821e5207/lib/install.go#L38

miguelhar avatar Feb 09 '22 19:02 miguelhar

@miguelhar what version of tfswitch are you using? tfswitch -v

warrensbox avatar Feb 11 '22 01:02 warrensbox

tfswitch -v

Version: 0.13.1201

miguelhar avatar Feb 15 '22 16:02 miguelhar

removing this function fixes the issue(tested locally) as the comment states, it seems this function is not necessary.


// initialize : removes existing symlink to terraform binary// I Don't think this is needed
func initialize() {

	/* Step 1 */
	/* initilize default binary path for terraform */
	/* assumes that terraform is installed here */
	/* we will find the terraform path instalation later and replace this variable with the correct installed bin path */
	installedBinPath := "/usr/local/bin/terraform"

	/* find terraform binary location if terraform is already installed*/
	cmd := NewCommand("terraform")
	next := cmd.Find()

	/* overrride installation default binary path if terraform is already installed */
	/* find the last bin path */
	for path := next(); len(path) > 0; path = next() {
		installedBinPath = path
	}

	/* check if current symlink to terraform binary exist */
	symlinkExist := CheckSymlink(installedBinPath)

	/* remove current symlink if exist*/
	if symlinkExist {
		RemoveSymlink(installedBinPath)
	}

}

miguelhar avatar Feb 15 '22 16:02 miguelhar

PR to fix

miguelhar avatar Feb 15 '22 16:02 miguelhar

@warrensbox what do you think?

miguelhar avatar Feb 17 '22 14:02 miguelhar

This code suggestion is already present in the current code base. I will close this ticket

MatrixCrawler avatar Jun 04 '24 14:06 MatrixCrawler