-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
167 lines (150 loc) · 3.8 KB
/
github.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
package httpfuncs
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/KJHJason/Cultured-Downloader-Logic/constants"
cdlerrors "github.com/KJHJason/Cultured-Downloader-Logic/errors"
"github.com/KJHJason/Cultured-Downloader-Logic/progress"
)
var (
ErrProcessLatestVer = fmt.Errorf(
"github error %d: unable to process the latest version",
cdlerrors.DEV_ERROR,
)
ErrProcessVer = fmt.Errorf(
"github error %d: unable to process the current version",
cdlerrors.DEV_ERROR,
)
)
func processVer(apiResVer string) (*versionInfo, error) {
// split the version string by "."
ver := strings.Split(apiResVer, ".")
if len(ver) != 3 {
return nil, ErrProcessLatestVer
}
// convert the version string to int
verSlice := make([]int, 3)
for i, v := range ver {
verInt, err := strconv.Atoi(v)
if err != nil {
return nil, ErrProcessLatestVer
}
verSlice[i] = verInt
}
return &versionInfo{
Major: verSlice[0],
Minor: verSlice[1],
Patch: verSlice[2],
}, nil
}
// check if the latest version is greater than the current version.
// returns true if the current version is outdated.
func checkIfVerIsOutdated(curVer *versionInfo, latestVer *versionInfo) bool {
if latestVer.Major > curVer.Major {
return true
}
if latestVer.Major == curVer.Major {
if latestVer.Minor > curVer.Minor {
return true
}
if latestVer.Minor == curVer.Minor {
if latestVer.Patch > curVer.Patch {
return true
}
}
}
return false
}
// check for the latest version of the program
func CheckVer(repo string, ver string, showProg bool, progBar progress.ProgressBar) (bool, error) {
url := fmt.Sprintf(constants.GITHUB_API_URL_FORMAT, repo)
if !constants.GITHUB_VER_REGEX.MatchString(ver) {
return false, fmt.Errorf(
"github error %d: unable to process the current version, %q",
cdlerrors.DEV_ERROR,
ver,
)
}
hasProgBar := showProg && progBar != nil
if hasProgBar {
progBar.UpdateBaseMsg("Checking for the latest version...")
progBar.UpdateErrorMsg("Failed to check for the latest version, please refer to the logs for more details...")
progBar.Start()
}
res, err := CallRequest(
&RequestArgs{
Url: url,
Method: "GET",
Timeout: 5,
CheckStatus: false,
Http3: false,
Http2: true,
},
)
if err != nil || res.StatusCode != 200 {
errMsg := fmt.Errorf(
"github error %d: unable to check for the latest version",
cdlerrors.CONNECTION_ERROR,
)
if err != nil {
errMsg = fmt.Errorf("%w, more info => %w", errMsg, err)
}
if showProg && progBar != nil {
progBar.Stop(true)
}
return false, errMsg
}
var apiRes GithubApiRes
if err := LoadJsonFromResponse(res, &apiRes); err != nil {
errMsg := fmt.Sprintf(
"github error %d: unable to marshal the response from the API into an interface",
cdlerrors.UNEXPECTED_ERROR,
)
if hasProgBar {
progBar.Stop(true)
}
return false, errors.New(errMsg)
}
latestVer, err := processVer(apiRes.TagName)
if err != nil {
errMsg := fmt.Sprintf(
"github error %d: unable to process the latest version",
cdlerrors.UNEXPECTED_ERROR,
)
if hasProgBar {
progBar.UpdateErrorMsg(errMsg)
progBar.Stop(true)
}
return false, err
}
programVer, err := processVer(ver)
if err != nil {
errMsg := fmt.Sprintf(
"error %d: unable to process the program version",
cdlerrors.DEV_ERROR,
)
if hasProgBar {
progBar.UpdateErrorMsg(errMsg)
progBar.Stop(true)
}
return false, ErrProcessVer
}
outdated := checkIfVerIsOutdated(programVer, latestVer)
if hasProgBar {
if outdated {
progBar.UpdateErrorMsg(
fmt.Sprintf(
"Warning: this program is outdated, the latest version %q is available at %s",
apiRes.TagName,
apiRes.HtmlUrl,
),
)
} else {
progBar.UpdateSuccessMsg("This program is up to date!")
}
progBar.Stop(outdated)
}
return outdated, nil
}