-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_version.go
50 lines (41 loc) · 1.15 KB
/
go_version.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
package aph
import (
"bytes"
"fmt"
"log"
"net/http"
"regexp"
"runtime"
"sort"
)
func checkLatestGoVersionNumber() {
latestGVN, err := latestGoVersionNumber()
if err != nil {
log.Printf("WARNING: unable to ascertain latest go version number: %s", err)
} else if runtime.Version() != latestGVN {
log.Printf("WARNING: current go version number: %s, latest: %s", runtime.Version(), latestGVN)
} else {
log.Printf("go version is latest: %s", latestGVN)
}
}
func latestGoVersionNumber() (string, error) {
url := "https://golang.org/doc/devel/release.html"
versionMatcher := "go\\d+\\.\\d+\\.\\d+"
response, err := http.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
b := bytes.Buffer{}
_, err = b.ReadFrom(response.Body)
if err != nil {
return "", err
}
matches := regexp.MustCompile(versionMatcher).FindAllString(b.String(), -1)
if len(matches) == 0 {
return "", fmt.Errorf("no strings matching '%s' found in response from '%s'", versionMatcher, url)
}
// this is mostly OK as (currently) all releases use single digit major, minor and patch numbers
sort.Strings(matches)
return matches[len(matches)-1], nil
}