-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathlist_versions.go
209 lines (179 loc) · 5.66 KB
/
list_versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package lib
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"regexp"
"strings"
)
type tfVersionList struct {
tflist []string
}
//GetTFList : Get the list of available terraform version given the hashicorp url
func GetTFList(mirrorURL string, preRelease bool) ([]string, error) {
result, error := GetTFURLBody(mirrorURL)
if error != nil {
return nil, error
}
var tfVersionList tfVersionList
var semver string
if preRelease == true {
// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
semver = `\/(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?/?"`
} else if preRelease == false {
// Getting versions from body; should return match /X.X.X/ where X is a number
// without the ending '"' pre-release folders would be tried and break.
semver = `\/(\d+\.\d+\.\d+)\/?"`
}
r, _ := regexp.Compile(semver)
for i := range result {
if r.MatchString(result[i]) {
str := r.FindString(result[i])
trimstr := strings.Trim(str, "/\"") //remove '/' or '"' from /X.X.X/" or /X.X.X"
tfVersionList.tflist = append(tfVersionList.tflist, trimstr)
}
}
if len(tfVersionList.tflist) == 0 {
fmt.Printf("Cannot get list from mirror: %s\n", mirrorURL)
}
return tfVersionList.tflist, nil
}
//GetTFLatest : Get the latest terraform version given the hashicorp url
func GetTFLatest(mirrorURL string) (string, error) {
result, error := GetTFURLBody(mirrorURL)
if error != nil {
return "", error
}
// Getting versions from body; should return match /X.X.X/ where X is a number
semver := `\/(\d+\.\d+\.\d+)\/?"`
r, _ := regexp.Compile(semver)
for i := range result {
if r.MatchString(result[i]) {
str := r.FindString(result[i])
trimstr := strings.Trim(str, "/\"") //remove '/' or '"' from /X.X.X/" or /X.X.X"
return trimstr, nil
}
}
return "", nil
}
//GetTFLatestImplicit : Get the latest implicit terraform version given the hashicorp url
func GetTFLatestImplicit(mirrorURL string, preRelease bool, version string) (string, error) {
if preRelease == true {
//TODO: use GetTFList() instead of GetTFURLBody
versions, error := GetTFURLBody(mirrorURL)
if error != nil {
return "", error
}
// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
semver := fmt.Sprintf(`\/(%s{1}\.\d+\-[a-zA-z]+\d*)\/?"`, version)
r, err := regexp.Compile(semver)
if err != nil {
return "", err
}
for i := range versions {
if r.MatchString(versions[i]) {
str := r.FindString(versions[i])
trimstr := strings.Trim(str, "/\"") //remove '/' or '"' from /X.X.X/" or /X.X.X"
return trimstr, nil
}
}
} else if preRelease == false {
listAll := false
tflist, _ := GetTFList(mirrorURL, listAll) //get list of versions
version = fmt.Sprintf("~> %v", version)
semv, err := SemVerParser(&version, tflist)
if err != nil {
return "", err
}
return semv, nil
}
return "", nil
}
//GetTFURLBody : Get list of terraform versions from hashicorp releases
func GetTFURLBody(mirrorURL string) ([]string, error) {
hasSlash := strings.HasSuffix(mirrorURL, "/")
if !hasSlash { //if does not have slash - append slash
mirrorURL = fmt.Sprintf("%s/", mirrorURL)
}
resp, errURL := http.Get(mirrorURL)
if errURL != nil {
log.Printf("[Error] : Getting url: %v", errURL)
os.Exit(1)
return nil, errURL
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Printf("[Error] : Retrieving contents from url: %s", mirrorURL)
os.Exit(1)
}
body, errBody := ioutil.ReadAll(resp.Body)
if errBody != nil {
log.Printf("[Error] : reading body: %v", errBody)
os.Exit(1)
return nil, errBody
}
bodyString := string(body)
result := strings.Split(bodyString, "\n")
return result, nil
}
//VersionExist : check if requested version exist
func VersionExist(val interface{}, array interface{}) (exists bool) {
exists = false
switch reflect.TypeOf(array).Kind() {
case reflect.Slice:
s := reflect.ValueOf(array)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
exists = true
return exists
}
}
}
return exists
}
//RemoveDuplicateVersions : remove duplicate version
func RemoveDuplicateVersions(elements []string) []string {
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := []string{}
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[versionOnly] = true
// Append to result slice.
result = append(result, val)
}
}
// Return the new slice.
return result
}
// ValidVersionFormat : returns valid version format
/* For example: 0.1.2 = valid
// For example: 0.1.2-beta1 = valid
// For example: 0.1.2-alpha = valid
// For example: a.1.2 = invalid
// For example: 0.1. 2 = invalid
*/
func ValidVersionFormat(version string) bool {
// Getting versions from body; should return match /X.X.X-@/ where X is a number,@ is a word character between a-z or A-Z
// Follow https://semver.org/spec/v1.0.0-beta.html
// Check regular expression at https://rubular.com/r/ju3PxbaSBALpJB
semverRegex := regexp.MustCompile(`^(\d+\.\d+\.\d+)(-[a-zA-z]+\d*)?$`)
return semverRegex.MatchString(version)
}
// ValidMinorVersionFormat : returns valid MINOR version format
/* For example: 0.1 = valid
// For example: a.1.2 = invalid
// For example: 0.1.2 = invalid
*/
func ValidMinorVersionFormat(version string) bool {
// Getting versions from body; should return match /X.X./ where X is a number
semverRegex := regexp.MustCompile(`^(\d+\.\d+)$`)
return semverRegex.MatchString(version)
}