forked from groupon/Selenium-Grid-Extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest.go
150 lines (129 loc) · 2.93 KB
/
latest.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
package main
import (
"io"
"strings"
"os"
"log"
"encoding/json"
"net/http"
"io/ioutil"
)
type Releases []Release
type Release struct {
URL string
Assets_URL string
Upload_URL string
Html_URL string
Id int
Tag_name string
Target_commitish string
Name string
Draft bool
Authors []Author
Prelease bool
Created_at string
Published_at string
Assets []Asset
Tarball_URL string
Zipball_URL string
Body string
}
type Author struct {
Author_info []string
}
type Asset struct {
URL string
Id int
Name string
Label string
Uploaders Uploader
Content_type string
State string
Size int
Download_count int
Created_at string
Updated_at string
Browser_download_URL string
}
type Uploader struct {
Login string
Id int
Avatar_URL string
Gravatar_id string
URL string
Html_URL string
Followers_URL string
Following_URL string
Gists_URL string
Starred_URL string
Subscriptions_URL string
Organizations_URL string
Repos_URL string
Events_URL string
Received_events_URL string
Type string
Site_admin bool
}
func get_latest_release() {
resp, err := http.Get("https://api.github.com/repos/groupon/Selenium-Grid-Extras/releases")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatal(resp.Request.URL, " => Response.StatusCode: ", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var releases Releases
err = json.Unmarshal(body, &releases)
if err != nil {
log.Fatal(err)
}
// the first assets is the latest release
for _, asset := range releases[0].Assets {
// determine filename
tokens := strings.Split(asset.Browser_download_URL, "/")
filename := tokens[len(tokens)-1]
// SeleniumGridExtras-*-jar-with-dependencies.jar
if asset.Content_type == "application/java-archive" {
// prevent re-downloading file if already exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
downloadFromUrl(asset.Browser_download_URL, filename)
// create symlink to latest release
os.Symlink(filename, "SeleniumGridExtras-jar-with-dependencies.jar")
}
if err != nil {
log.Fatal(err)
}
}
}
}
func main() {
get_latest_release()
}
// https://github.com/thbar/golang-playground/blob/master/download-files.go
func downloadFromUrl(url string, fileName string) {
log.Println("Downloading", url, "to", fileName)
// TODO: check file existence first with io.IsExist
output, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
defer output.Close()
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Fatal(resp.Request.URL, " => Response.StatusCode: ", resp.StatusCode)
}
n, err := io.Copy(output, resp.Body)
if err != nil {
log.Fatal(err)
}
log.Println(n, "bytes downloaded.")
}