-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlatest.go
178 lines (147 loc) · 4.15 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package version
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"time"
goversion "github.com/hashicorp/go-version"
)
const (
latestVersionTimeout = 10 * time.Second
DefaultReleasesURL = "https://github.com/GitDataAI/jiaozifs/releases"
githubBaseURL = "https://api.github.com/"
GithubRepoOwner = "jiaozifs"
GithubRepoName = "jiaozifs"
)
var ErrHTTPStatus = errors.New("unexpected HTTP status code")
type RepositoryRelease struct {
TagName string `json:"tag_name,omitempty"`
Name string `json:"name,omitempty"`
Draft bool `json:"draft,omitempty"`
Prerelease bool `json:"prerelease,omitempty"`
ID int64 `json:"id,omitempty"`
URL string `json:"url,omitempty"`
}
type LatestVersionResponse struct {
CheckTime time.Time `json:"check_time"`
Outdated bool `json:"outdated"`
LatestVersion string `json:"latest_version"`
CurrentVersion string `json:"current_version"`
}
type Source interface {
FetchLatestVersion() (string, error)
}
type CachedVersionSource struct {
Source Source
lastCheck time.Time
cachePeriod time.Duration
fetchErr error
fetchResponse string
}
func NewDefaultVersionSource(cachePeriod time.Duration) Source {
gh := NewGithubReleases(GithubRepoOwner, GithubRepoName)
return NewCachedSource(gh, cachePeriod)
}
func NewCachedSource(src Source, cachePeriod time.Duration) *CachedVersionSource {
return &CachedVersionSource{
Source: src,
cachePeriod: cachePeriod,
}
}
func (cs *CachedVersionSource) FetchLatestVersion() (string, error) {
if time.Since(cs.lastCheck) > cs.cachePeriod {
cs.fetchResponse, cs.fetchErr = cs.Source.FetchLatestVersion()
cs.lastCheck = time.Now()
}
return cs.fetchResponse, cs.fetchErr
}
type GithubReleases struct {
owner string
repository string
}
func NewGithubReleases(owner, repository string) *GithubReleases {
return &GithubReleases{
owner: owner,
repository: repository,
}
}
func (gh *GithubReleases) FetchLatestVersion() (string, error) {
u, err := url.JoinPath(githubBaseURL, "repos", gh.owner, gh.repository, "releases", "latest")
if err != nil {
return "", err
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return "", err
}
client := &http.Client{
Timeout: latestVersionTimeout,
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected HTTP response %d: %w", resp.StatusCode, ErrHTTPStatus)
}
var release RepositoryRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}
return release.TagName, nil
}
func CheckLatestVersion(targetVersion string) (*LatestVersionResponse, error) {
targetV, err := goversion.NewVersion(targetVersion)
if err != nil {
return nil, fmt.Errorf("tag parse %s: %w", targetVersion, err)
}
if IsVersionUnreleased() {
return &LatestVersionResponse{
Outdated: false,
LatestVersion: targetV.String(),
CurrentVersion: UserVersion(),
}, nil
}
currentV, err := goversion.NewVersion(UserVersion())
if err != nil {
return nil, fmt.Errorf("version parse %s: %w", UserVersion(), err)
}
return &LatestVersionResponse{
Outdated: currentV.LessThan(targetV),
LatestVersion: targetV.String(),
CurrentVersion: UserVersion(),
}, nil
}
type IChecker interface {
CheckLatestVersion() (*LatestVersionResponse, error)
}
type Checker struct {
Client http.Client
Version string
latestReleases Source
}
func NewVersionChecker() *Checker {
return &Checker{
Client: http.Client{},
Version: UserVersion(),
latestReleases: NewDefaultVersionSource(time.Hour),
}
}
// CheckLatestVersion will return the latest version of the current package compared to the current version
func (a *Checker) CheckLatestVersion() (*LatestVersionResponse, error) {
if a == nil || a.latestReleases == nil {
return &LatestVersionResponse{}, nil
}
latest, err := a.latestReleases.FetchLatestVersion()
if err != nil {
return nil, err
}
result, err := CheckLatestVersion(latest)
if err != nil {
return nil, err
}
return result, nil
}