Skip to content

Commit

Permalink
fix installation directories and files paths for windows based hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
PertsevRoman committed May 18, 2021
1 parent a710b03 commit ab7aaba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ tfswitch*

build-script.sh

.idea

/vendor
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3 // indirect
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/hashicorp/hcl/v2 v2.10.0 // indirect
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 // indirect
github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
github.com/kiranjthomas/terraform-config-inspect v0.0.0-20191120205521-a1d709eb2824
github.com/lunixbochs/vtclean v0.0.0-20170504063817-d14193dfc626 // indirect
Expand Down
39 changes: 33 additions & 6 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
hashiURL = "https://releases.hashicorp.com/terraform/"
installFile = "terraform"
installVersion = "terraform_"
installPath = "/.terraform.versions/"
installPath = ".terraform.versions"
recentFile = "RECENT"
)

Expand Down Expand Up @@ -49,6 +49,33 @@ func initialize() {

}

// get install path variable value (windows os runtime support)
func getInstallPath() string {
if runtime.GOOS == "windows" {
return "\\" + installPath + "\\"
}

return "/" + installPath + "/"
}

// get versioned install filename (windows os runtime support)
func getVersionedInstallFileName(tfversion string) string {
if runtime.GOOS == "windows" {
return getInstallLocation() + installVersion + tfversion + ".exe"
}

return getInstallLocation() + installVersion + tfversion
}

// get install filename (windows os runtime support)
func getInstallFileName() string {
if runtime.GOOS == "windows" {
return getInstallLocation() + installFile + ".exe"
}

return installLocation + installFile
}

// getInstallLocation : get location where the terraform binary will be installed,
// will create a directory in the home location if it does not exist
func getInstallLocation() string {
Expand All @@ -59,7 +86,7 @@ func getInstallLocation() string {
}

/* set installation location */
installLocation = usr.HomeDir + installPath
installLocation = usr.HomeDir + getInstallPath()

/* Create local installation directory if it does not exist */
CreateDirIfNotExist(installLocation)
Expand Down Expand Up @@ -97,7 +124,7 @@ func Install(tfversion string, binPath string) {
}

/* check if selected version already downloaded */
fileExist := CheckFileExist(installLocation + installVersion + tfversion)
fileExist := CheckFileExist(getVersionedInstallFileName(tfversion))

/* if selected version already exist, */
if fileExist {
Expand All @@ -110,7 +137,7 @@ func Install(tfversion string, binPath string) {
}

/* set symlink to desired version */
CreateSymlink(installLocation+installVersion+tfversion, binPath)
CreateSymlink(getVersionedInstallFileName(tfversion), binPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
os.Exit(0)
Expand All @@ -136,7 +163,7 @@ func Install(tfversion string, binPath string) {
}

/* rename unzipped file to terraform version name - terraform_x.x.x */
RenameFile(installLocation+installFile, installLocation+installVersion+tfversion)
RenameFile(getInstallFileName(), getVersionedInstallFileName(tfversion))

/* remove zipped file to clear clutter */
RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip")
Expand All @@ -149,7 +176,7 @@ func Install(tfversion string, binPath string) {
}

/* set symlink to desired version */
CreateSymlink(installLocation+installVersion+tfversion, binPath)
CreateSymlink(getVersionedInstallFileName(tfversion), binPath)
fmt.Printf("Switched terraform to version %q \n", tfversion)
AddRecent(tfversion) //add to recent file for faster lookup
os.Exit(0)
Expand Down

0 comments on commit ab7aaba

Please sign in to comment.