-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-version.go
141 lines (126 loc) · 3.7 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
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
package go_version
import (
"fmt"
"net/http"
"net/url"
"path"
"github.com/PuerkitoBio/goquery"
)
type GoVersionInfoLite struct {
OS string `json:"os,omitempty"`
FileName string `json:"file_name,omitempty"`
DownloadUrl string `json:"download_url,omitempty"`
Size string `json:"size"`
}
type GoVersionInfoFull struct {
GoVersionInfoLite
Arch string `json:"arch,omitempty"`
PackageType string `json:"package_type,omitempty"`
SHA256 string `json:"sha256,omitempty"`
}
type GoVersionInfoList struct {
Category string
InfoList []GoVersionInfoFull
}
type DownLoader struct {
Host string
}
func NewDownLoader(host string) *DownLoader {
return &DownLoader{Host: host}
}
// GetFeaturedDownload returns the latest version golang download info but with few extra info
func (d DownLoader) GetFeaturedDownload() ([]GoVersionInfoLite, error) {
result := make([]GoVersionInfoLite, 0)
hostUrl, _ := urlJoin(d.Host, "dl")
res, err := http.Get(hostUrl.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
doc.Find("a.downloadBox").Each(func(i int, s *goquery.Selection) {
versionInfo := GoVersionInfoLite{}
downloadPath, exist := s.Attr("href")
if exist {
downloadUrl, _ := urlJoin(d.Host, downloadPath)
versionInfo.DownloadUrl = downloadUrl.String()
}
versionInfo.OS = s.Find("div.platform").Text()
versionInfo.FileName = s.Find("span.filename").Text()
versionInfo.Size = s.Find("span.size").Text()
result = append(result, versionInfo)
})
return result, nil
}
// GetAllDownload returns the All version golang download info
func (d DownLoader) GetAllDownload() ([]GoVersionInfoList, error) {
var (
result = make([]GoVersionInfoList, 0)
)
hostUrl, _ := urlJoin(d.Host, "dl")
res, err := http.Get(hostUrl.String())
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
doc.Find("div.toggle").Each(func(i int, s *goquery.Selection) {
tag, exist := s.Attr("id")
if exist {
versionInfo := GoVersionInfoList{Category: tag}
versionInfo.InfoList = make([]GoVersionInfoFull, 0)
s.Find("div.expanded>table.codetable>tbody tr").Each(func(i int, trSelection *goquery.Selection) {
contents := trSelection.Find("td")
versionFull := GoVersionInfoFull{}
contents.Each(func(i int, tdSelection *goquery.Selection) {
if tdSelection.HasClass("filename") {
downloadSelection := tdSelection.Find("a.download")
downloadUrlAttr, exists := downloadSelection.Attr("href")
if exists {
downloadUrl, _ := urlJoin(d.Host, downloadUrlAttr)
versionFull.DownloadUrl = downloadUrl.String()
}
} else {
switch i {
case 0:
versionFull.FileName = tdSelection.Text()
case 1:
versionFull.PackageType = tdSelection.Text()
case 2:
versionFull.OS = tdSelection.Text()
case 3:
versionFull.Arch = tdSelection.Text()
case 4:
versionFull.Size = tdSelection.Text()
case 5:
versionFull.SHA256 = tdSelection.Text()
}
}
})
versionInfo.InfoList = append(versionInfo.InfoList, versionFull)
})
result = append(result, versionInfo)
}
})
return result, nil
}
func urlJoin(Host, Path string) (*url.URL, error) {
hostUrl, err := url.Parse(Host)
if err != nil {
return nil, err
}
hostUrl.Path = path.Join(hostUrl.Path, Path)
return hostUrl, nil
}