-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsources.go
174 lines (158 loc) · 4.81 KB
/
sources.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
package updaterini
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"time"
)
var ErrorResponseCodeIsNotOK = errors.New("error. response code is not OK")
const (
SourceLabelGitRepo = "SourceGitRepo"
SourceLabelServer = "SourceServer"
)
type UpdateSource interface {
SourceLabel() string
getSourceVersions(cfg ApplicationConfig) ([]Version, SourceStatus)
}
type UpdateSourceGitRepo struct {
UserName string
RepoName string
UseDraftVersions bool // on true releases marked as draft load and validate as others
PersonalAccessToken string // ONLY FOR DEBUG PURPOSE
}
func (sGit *UpdateSourceGitRepo) SourceLabel() string {
return SourceLabelGitRepo
}
func (sGit *UpdateSourceGitRepo) getSourceUrl() string {
link := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases", sGit.UserName, sGit.RepoName)
return link
}
func (sGit *UpdateSourceGitRepo) getLoadFileUrl(fileId int) string {
link := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/assets/%d", sGit.UserName, sGit.RepoName, fileId)
return link
}
func (sGit *UpdateSourceGitRepo) getSourceVersions(cfg ApplicationConfig) (resultVersions []Version, srcStatus SourceStatus) {
srcStatus.Source = sGit
var customHeaders map[string]string
if sGit.PersonalAccessToken != "" {
customHeaders = make(map[string]string, 1)
customHeaders["Authorization"] = fmt.Sprintf("token %s", sGit.PersonalAccessToken)
}
resp, err := doGetRequest(sGit.getSourceUrl(), cfg, customHeaders, nil)
if err != nil {
srcStatus.appendError(err, true)
return nil, srcStatus
}
defer func() {
tmpErr := resp.Body.Close()
if tmpErr != nil {
srcStatus.appendError(tmpErr, false)
}
}()
var data []gitData
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
srcStatus.appendError(err, true)
return nil, srcStatus
}
for _, gData := range data {
if gData.Draft && !sGit.UseDraftVersions {
continue
}
gVersion, err := newVersionGit(cfg, gData, *sGit)
if err != nil {
if cfg.ShowPrepareVersionErr {
srcStatus.appendError(err, false)
}
continue
}
resultVersions = append(resultVersions, &gVersion)
}
return resultVersions, srcStatus
}
func (sGit *UpdateSourceGitRepo) loadSourceFile(cfg ApplicationConfig, fileId int) (io.ReadCloser, error) {
customHeaders := make(map[string]string, 2)
customHeaders["Accept"] = "application/octet-stream"
if sGit.PersonalAccessToken != "" {
customHeaders["Authorization"] = fmt.Sprintf("token %s", sGit.PersonalAccessToken)
}
resp, err := doGetRequest(sGit.getLoadFileUrl(fileId), cfg, customHeaders,
map[int]interface{}{200: struct{}{}, 302: struct{}{}})
if err != nil {
return nil, err
}
return resp.Body, nil
}
type UpdateSourceServer struct {
UpdatesMapURL string
}
func (sServ *UpdateSourceServer) SourceLabel() string {
return SourceLabelServer
}
func (sServ *UpdateSourceServer) getSourceVersions(cfg ApplicationConfig) (resultVersions []Version, srcStatus SourceStatus) {
srcStatus.Source = sServ
resp, err := doGetRequest(sServ.UpdatesMapURL, cfg, nil, nil)
if err != nil {
srcStatus.appendError(err, true)
return nil, srcStatus
}
defer func() {
tmpErr := resp.Body.Close()
if tmpErr != nil {
srcStatus.appendError(tmpErr, false)
}
}()
var sData []ServData
err = json.NewDecoder(resp.Body).Decode(&sData)
if err != nil {
srcStatus.appendError(err, true)
return nil, srcStatus
}
for _, data := range sData {
version, err := newVersionServ(cfg, data, *sServ)
if err != nil {
if cfg.ShowPrepareVersionErr {
srcStatus.appendError(err, false)
}
continue
}
resultVersions = append(resultVersions, &version)
}
return resultVersions, srcStatus
}
func (sServ *UpdateSourceServer) loadSourceFile(cfg ApplicationConfig, serverFolderUrl, filename string) (io.ReadCloser, error) {
resp, err := doGetRequest(serverFolderUrl+filename, cfg, nil, map[int]interface{}{200: struct{}{}})
if err != nil {
return nil, err
}
return resp.Body, nil
}
var reqHTTP = &http.Client{
Timeout: 5 * time.Minute,
Transport: &http.Transport{
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
},
}
func doGetRequest(url string, appConfig ApplicationConfig, customHeaders map[string]string, okCodes map[int]interface{}) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", fmt.Sprintf(`updaterini %s (%s %s-%s)`, appConfig.currentVersion.version.String(), runtime.Version(), runtime.GOOS, runtime.GOARCH))
for key, customHeader := range customHeaders {
req.Header.Set(key, customHeader)
}
resp, err := reqHTTP.Do(req)
if err != nil {
return nil, err
}
if _, ok := okCodes[resp.StatusCode]; (len(okCodes) != 0 || resp.StatusCode != 200) && !ok {
_ = resp.Body.Close()
return nil, ErrorResponseCodeIsNotOK
}
return resp, nil
}