From a29bb2e26ba874078805cdde5a9a8a23f1b9555e Mon Sep 17 00:00:00 2001 From: "warren.veerasingam@gmail.com" Date: Tue, 4 Jun 2019 17:01:16 -0500 Subject: [PATCH] added install for custom location --- lib/install.go | 34 ++++++++++++++++++++-------------- main.go | 15 +++++++++------ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/install.go b/lib/install.go index 618269d7..6d416a43 100644 --- a/lib/install.go +++ b/lib/install.go @@ -12,14 +12,14 @@ const ( hashiURL = "https://releases.hashicorp.com/terraform/" installFile = "terraform" installVersion = "terraform_" - binLocation = "/usr/local/bin/terraform" - installPath = "/.terraform.versions/" - recentFile = "RECENT" + //binLocation = "/usr/local/bin/terraform" + installPath = "/.terraform.versions/" + recentFile = "RECENT" ) var ( - installLocation = "/tmp" - installedBinPath = "/tmp" + installLocation = "/tmp" + //installedBinPath = "/tmp" ) func init() { @@ -33,12 +33,11 @@ func init() { installLocation = usr.HomeDir + installPath /* set default binary path for terraform */ - installedBinPath = binLocation + installedBinPath := "/usr/local/bin/terraform" /* find terraform binary location if terraform is already installed*/ cmd := NewCommand("terraform") next := cmd.Find() - //existed := false /* overrride installation default binary path if terraform is already installed */ /* find the last bin path */ @@ -46,13 +45,20 @@ func init() { installedBinPath = path } + /* remove current symlink if exist*/ + symlinkExist := CheckSymlink(installedBinPath) + + if symlinkExist { + RemoveSymlink(installedBinPath) + } + /* Create local installation directory if it does not exist */ CreateDirIfNotExist(installLocation) } //Install : Install the provided version in the argument -func Install(tfversion string) { +func Install(tfversion string, binPath string) { if !ValidVersionFormat(tfversion) { fmt.Printf("The provided terraform version format does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion) @@ -69,14 +75,14 @@ func Install(tfversion string) { if fileExist { /* remove current symlink if exist*/ - symlinkExist := CheckSymlink(installedBinPath) + symlinkExist := CheckSymlink(binPath) if symlinkExist { - RemoveSymlink(installedBinPath) + RemoveSymlink(binPath) } /* set symlink to desired version */ - CreateSymlink(installLocation+installVersion+tfversion, installedBinPath) + CreateSymlink(installLocation+installVersion+tfversion, binPath) fmt.Printf("Switched terraform to version %q \n", tfversion) AddRecent(tfversion) //add to recent file for faster lookup os.Exit(0) @@ -108,14 +114,14 @@ func Install(tfversion string) { RemoveFiles(installLocation + installVersion + tfversion + "_" + goos + "_" + goarch + ".zip") /* remove current symlink if exist*/ - symlinkExist := CheckSymlink(installedBinPath) + symlinkExist := CheckSymlink(binPath) if symlinkExist { - RemoveSymlink(installedBinPath) + RemoveSymlink(binPath) } /* set symlink to desired version */ - CreateSymlink(installLocation+installVersion+tfversion, installedBinPath) + CreateSymlink(installLocation+installVersion+tfversion, binPath) fmt.Printf("Switched terraform to version %q \n", tfversion) AddRecent(tfversion) //add to recent file for faster lookup os.Exit(0) diff --git a/main.go b/main.go index 523ef183..f06a5698 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ const ( var version = "0.6.0\n" func main() { + custBinPath := getopt.StringLong("bin", 'b', "/usr/local/bin/terraform", "custom binary path") listAllFlag := getopt.BoolLong("list-all", 'l', "list all versions of terraform - including beta and rc") versionFlag := getopt.BoolLong("version", 'v', "displays the version of tfswitch") helpFlag := getopt.BoolLong("help", 'h', "displays help message") @@ -51,13 +52,15 @@ func main() { } rcfile := dir + "/.tfswitchrc" + fmt.Println(*custBinPath) + if *versionFlag { fmt.Printf("\nVersion: %v\n", version) } else if *helpFlag { usageMessage() } else if *listAllFlag { listAll := true //set list all true - all versions including beta and rc will be displayed - installOption(listAll) + installOption(listAll, custBinPath) } else { if len(args) == 1 { //if tf version is provided in command line @@ -70,7 +73,7 @@ func main() { exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it if exist { - lib.Install(requestedVersion) + lib.Install(requestedVersion, *custBinPath) } else { fmt.Println("The provided terraform version does not exist. Try `tfswitch -l` to see all available versions.") } @@ -93,7 +96,7 @@ func main() { tfversion := strings.TrimSuffix(string(fileContents), "\n") if lib.ValidVersionFormat(tfversion) { //check if version is correct - lib.Install(string(tfversion)) + lib.Install(string(tfversion), *custBinPath) } else { fmt.Println("Invalid terraform version format. Format should be #.#.# or #.#.#-@# where # is numbers and @ is word characters. For example, 0.11.7 and 0.11.9-beta1 are valid versions") os.Exit(1) @@ -101,7 +104,7 @@ func main() { } else if len(args) == 0 { //if there are no commmand line arguments listAll := false //set list all false - only official release will be displayed - installOption(listAll) + installOption(listAll, custBinPath) } else { usageMessage() @@ -118,7 +121,7 @@ func usageMessage() { /* installOption : displays & installs tf version */ /* listAll = true - all versions including beta and rc will be displayed */ /* listAll = false - only official stable release are displayed */ -func installOption(listAll bool) { +func installOption(listAll bool, custBinPath *string) { tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file @@ -139,5 +142,5 @@ func installOption(listAll bool) { os.Exit(1) } - lib.Install(tfversion) + lib.Install(tfversion, *custBinPath) }