Skip to content

Commit

Permalink
Added optional parameter to list-all versions
Browse files Browse the repository at this point in the history
  • Loading branch information
warrensbox committed May 17, 2019
1 parent ed66523 commit 60d48e5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
16 changes: 10 additions & 6 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type tfVersionList struct {
}

//GetTFList : Get the list of available terraform version given the hashicorp url
func GetTFList(hashiURL string) ([]string, error) {
func GetTFList(hashiURL string, listAll bool) ([]string, error) {

/* Get list of terraform versions from hashicorp releases */
resp, errURL := http.Get(hashiURL)
Expand All @@ -37,7 +37,10 @@ func GetTFList(hashiURL string) ([]string, error) {

for i := range result {
//getting versions from body; should return match /X.X.X/
r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)(-\w+\d+)?\/`)
r, _ := regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)?\/`)
if listAll {
r, _ = regexp.Compile(`\/(\d+)(\.)(\d+)(\.)(\d+)(-\w+\d*)?\/`)
}

if r.MatchString(result[i]) {
str := r.FindString(result[i])
Expand Down Expand Up @@ -90,12 +93,13 @@ func RemoveDuplicateVersions(elements []string) []string {
}

// ValidVersionFormat : returns valid version format
// For example: The 0.1.2 = valid
// For example: The a.1.2 = invalid
// For example: The 0.1. 2 = invalid
// For example: 0.1.2 = valid
// For example: 0.1.2-beta1 = valid
// For example: a.1.2 = invalid
// For example: 0.1. 2 = invalid
func ValidVersionFormat(version string) bool {

semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d+)?\z`)
semverRegex := regexp.MustCompile(`\A\d+(\.\d+){2}(-\w+\d*)?\z`)
semverRegex.MatchString(version)
return semverRegex.MatchString(version)
}
45 changes: 38 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

/*
* Version 0.3.0
* Version 0.5.0
* Compatible with Mac OS X ONLY
*/

Expand Down Expand Up @@ -36,6 +36,7 @@ const (
var version = "0.5.0\n"

func main() {
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")
_ = versionFlag
Expand All @@ -54,16 +55,19 @@ func main() {
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)
} else {

if len(args) == 1 { //if tf version is provided in command line

if lib.ValidVersionFormat(args[0]) {
requestedVersion := args[0]
listAll := true //set list all true - all versions including beta and rc will be displayed

//check if version exist before downloading it
tflist, _ := lib.GetTFList(hashiURL)
exist := lib.VersionExist(requestedVersion, tflist)
tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions
exist := lib.VersionExist(requestedVersion, tflist) //check if version exist before downloading it

if exist {
lib.AddRecent(requestedVersion) //add to recent file for faster lookup
Expand All @@ -79,6 +83,7 @@ func main() {
}

} else if _, err := os.Stat(rcfile); err == nil && len(args) == 0 { //if there is a .tfswitchrc file, and no commmand line arguments
fmt.Println("Reading required terraform version ...")

fileContents, err := ioutil.ReadFile(rcfile)
if err != nil {
Expand All @@ -89,16 +94,15 @@ func main() {
tfversion := strings.TrimSuffix(string(fileContents), "\n")

if lib.ValidVersionFormat(tfversion) { //check if version is correct
fmt.Println("Reading required terraform version ...")
lib.AddRecent(string(tfversion)) //add to RECENT file for faster lookup
lib.Install(string(tfversion))
} else {
log.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

tflist, _ := lib.GetTFList(hashiURL)
listAll := false
tflist, _ := lib.GetTFList(hashiURL, listAll)
recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file
tflist = append(recentVersions, tflist...) //append recent versions to the top of the list
tflist = lib.RemoveDuplicateVersions(tflist) //remove duplicate version
Expand Down Expand Up @@ -130,3 +134,30 @@ func usageMessage() {
getopt.PrintUsage(os.Stderr)
fmt.Println("Supply the terraform version as an argument, or choose from a menu")
}

/* 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) {

tflist, _ := lib.GetTFList(hashiURL, listAll) //get list of versions
recentVersions, _ := lib.GetRecentVersions() //get recent versions from RECENT file
tflist = append(recentVersions, tflist...) //append recent versions to the top of the list
tflist = lib.RemoveDuplicateVersions(tflist) //remove duplicate version

/* prompt user to select version of terraform */
prompt := promptui.Select{
Label: "Select Terraform version",
Items: tflist,
}

_, tfversion, errPrompt := prompt.Run()

if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
os.Exit(1)
}

lib.AddRecent(tfversion) //add to recent file for faster lookup
lib.Install(tfversion)
}

0 comments on commit 60d48e5

Please sign in to comment.