/
main.go
195 lines (170 loc) · 4.74 KB
/
main.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
package main
import (
"fmt"
"io"
"log" //nolint:revive
"net/http"
"os"
"regexp"
"strings"
"golang.org/x/net/html"
)
type FileInfo struct {
FileName string
Regex string
Replace string
}
func (f FileInfo) Update() error {
b, err := os.ReadFile(f.FileName)
if err != nil {
return err
}
re := regexp.MustCompile(f.Regex)
newContents := re.ReplaceAll(b, []byte(f.Replace))
err = os.WriteFile(f.FileName, newContents, 0664)
if err != nil {
return err
}
return nil
}
// removeZeroPatch cleans version in case the user provides the minor version as "1.19.0" but require "1.19"
func removeZeroPatch(version string) string {
if strings.HasSuffix(version, ".0") {
return strings.Trim(version, ".0")
}
return version
}
// findHash will search the downloads table for the hashes matching the artifacts list
func findHashes(body io.Reader, version string) (map[string]string, error) {
version = removeZeroPatch(version)
htmlTokens := html.NewTokenizer(body)
artifacts := []string{
fmt.Sprintf("go%s.linux-amd64.tar.gz", version),
fmt.Sprintf("go%s.darwin-arm64.tar.gz", version),
fmt.Sprintf("go%s.darwin-amd64.tar.gz", version),
}
var insideDownloadTable bool
var currentRow string
hashes := make(map[string]string)
for {
tokenType := htmlTokens.Next()
//if it's an error token, we either reached
//the end of the file, or the HTML was malformed
if tokenType == html.ErrorToken {
err := htmlTokens.Err()
if err == io.EOF {
//end of the file, break out of the loop
break
}
return nil, htmlTokens.Err()
}
if tokenType == html.StartTagToken {
//get the token
token := htmlTokens.Token()
if "table" == token.Data && len(token.Attr) == 1 && token.Attr[0].Val == "downloadtable" {
insideDownloadTable = true
}
if insideDownloadTable && token.Data == "a" && len(token.Attr) == 2 {
for _, f := range artifacts {
// Check if the current row matches a desired file
if strings.Contains(token.Attr[1].Val, f) {
currentRow = f
break
}
}
}
if currentRow != "" && token.Data == "tt" {
//the next token should be the page title
tokenType = htmlTokens.Next()
//just make sure it's actually a text token
if tokenType == html.TextToken {
hashes[currentRow] = htmlTokens.Token().Data
currentRow = ""
}
}
}
// Found a hash for each filename
if len(hashes) == len(artifacts) {
break
}
// Reached end of table
if tokenType == html.EndTagToken && htmlTokens.Token().Data == "table" {
return nil, fmt.Errorf("only found %d hashes expected %d: %v", len(hashes), len(artifacts), hashes)
}
}
return hashes, nil
}
func getHashes(version string) (map[string]string, error) {
resp, err := http.Get(`https://go.dev/dl/`)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return findHashes(resp.Body, version)
}
func main() {
version := os.Args[1]
// Handle situation user accidentally provides version as "v1.19.2"
if strings.HasPrefix(version, "v") {
version = strings.TrimLeft(version, "v")
}
zeroPatchVersion := removeZeroPatch(version)
hashes, err := getHashes(version)
if err != nil {
log.Panic(err)
}
files := []FileInfo{
{
FileName: ".circleci/config.yml",
Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`,
Replace: fmt.Sprintf("$1:%s", version),
},
{
FileName: "Makefile",
Regex: `(quay\.io\/influxdb\/telegraf-ci):(\d.\d*.\d)`,
Replace: fmt.Sprintf("$1:%s", version),
},
{
FileName: "scripts/ci.docker",
Regex: `(FROM golang):(\d.\d*.\d)`,
Replace: fmt.Sprintf("$1:%s", zeroPatchVersion),
},
{
FileName: "scripts/installgo_linux.sh",
Regex: `(GO_VERSION)=("\d.\d*.\d")`,
Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion),
},
{
FileName: "scripts/installgo_mac.sh",
Regex: `(GO_VERSION)=("\d.\d*.\d")`,
Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion),
},
{
FileName: "scripts/installgo_windows.sh",
Regex: `(GO_VERSION)=("\d.\d*.\d")`,
Replace: fmt.Sprintf("$1=\"%s\"", zeroPatchVersion),
},
{
FileName: "scripts/installgo_linux.sh",
Regex: `(GO_VERSION_SHA)=".*"`,
Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.linux-amd64.tar.gz", zeroPatchVersion)]),
},
{
FileName: "scripts/installgo_mac.sh",
Regex: `(GO_VERSION_SHA_arm64)=".*"`,
Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.darwin-arm64.tar.gz", zeroPatchVersion)]),
},
{
FileName: "scripts/installgo_mac.sh",
Regex: `(GO_VERSION_SHA_amd64)=".*"`,
Replace: fmt.Sprintf("$1=\"%s\"", hashes[fmt.Sprintf("go%s.darwin-amd64.tar.gz", zeroPatchVersion)]),
},
}
for _, f := range files {
fmt.Printf("Updating %s \n", f.FileName)
err := f.Update()
if err != nil {
log.Panic(err)
}
}
}