Skip to content

Commit

Permalink
added install for custom location
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed Jun 4, 2019
1 parent 093f9fb commit a29bb2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
34 changes: 20 additions & 14 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -33,26 +33,32 @@ 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 */
for path := next(); len(path) > 0; path = next() {
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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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.")
}
Expand All @@ -93,15 +96,15 @@ 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)
}
} 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()
Expand All @@ -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
Expand All @@ -139,5 +142,5 @@ func installOption(listAll bool) {
os.Exit(1)
}

lib.Install(tfversion)
lib.Install(tfversion, *custBinPath)
}

0 comments on commit a29bb2e

Please sign in to comment.