Skip to content

Commit

Permalink
*recent to for recent versions. Options to install all versions inclu…
Browse files Browse the repository at this point in the history
…ding betas and RCs
  • Loading branch information
warrensbox committed May 20, 2019
1 parent 15253c0 commit a55e6bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ brew:
skip_upload: false

# Packages that conflict with your package.
# conflicts:
# - terraform
conflicts:
- terraform

# So you can `brew test` your formula.
# Default is empty.
Expand Down
14 changes: 13 additions & 1 deletion lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ func init() {
//Install : Install the provided version in the argument
func Install(tfversion string) {

if !ValidVersionFormat(tfversion) {
fmt.Printf("The provided terraform version does not exist - %s. Try `tfswitch -l` to see all available versions.\n", tfversion)
os.Exit(0)
}

goarch := runtime.GOARCH
goos := runtime.GOOS

Expand Down Expand Up @@ -155,20 +160,27 @@ func GetRecentVersions() ([]string, error) {
if fileExist {

lines, errRead := ReadLines(installLocation + recentFile)
appendRecent := []string{}

if errRead != nil {
fmt.Printf("Error: %s\n", errRead)
return nil, errRead
}

for _, line := range lines {
/* checks if versions in the recent file are valid.
If any version is invalid, it will be consider dirty
and the recent file will be removed
*/
if !ValidVersionFormat(line) {
RemoveFiles(installLocation + recentFile)
return nil, errRead
}

appendRecent = append(appendRecent, fmt.Sprintf("%s *recent", line))
}

return lines, nil
return appendRecent, nil
}

return nil, nil
Expand Down
9 changes: 5 additions & 4 deletions lib/list_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func RemoveDuplicateVersions(elements []string) []string {
encountered := map[string]bool{}
result := []string{}

for v := range elements {
if encountered[elements[v]] == true {
for _, val := range elements {
versionOnly := strings.Trim(val, " *recent")
if encountered[versionOnly] == true {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[v]] = true
encountered[versionOnly] = true
// Append to result slice.
result = append(result, elements[v])
result = append(result, val)
}
}
// Return the new slice.
Expand Down
8 changes: 2 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ 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
tflist = lib.RemoveDuplicateVersions(tflist) //remove duplicate version

/* prompt user to select version of terraform */
prompt := promptui.Select{
Expand All @@ -134,6 +134,7 @@ func installOption(listAll bool) {
}

_, tfversion, errPrompt := prompt.Run()
tfversion = strings.Trim(tfversion, " *recent") //trim versions with the word *recent included

if errPrompt != nil {
log.Printf("Prompt failed %v\n", errPrompt)
Expand All @@ -143,8 +144,3 @@ func installOption(listAll bool) {
lib.AddRecent(tfversion) //add to recent file for faster lookup
lib.Install(tfversion)
}

// func appendRecent(tflist []string) {

// for _, vals := range
// }

0 comments on commit a55e6bf

Please sign in to comment.