terraform-switcher
terraform-switcher copied to clipboard
tfwitch blindly removes default terraform symlink, when bin is passed and version does not exist locally
Consider the following scenario, we want to have 2 versions of terraform with 2 different binary values.
terraformterraform0.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
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 what version of tfswitch are you using?
tfswitch -v
tfswitch -v
Version: 0.13.1201
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)
}
}
PR to fix
@warrensbox what do you think?
This code suggestion is already present in the current code base. I will close this ticket