1package agent
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "golang.org/x/mod/semver"
8 "net/http"
9 "os"
10 "path/filepath"
11 "strings"
12 "sync"
13 "time"
14)
15
16// updateCheckTTL is how often we hit the GitHub API to look for a new
17// release. Half a day is frequent enough to notice the same day a
18// release ships without spamming the API on every zot launch.
19const updateCheckTTL = 12 * time.Hour
20
21// updateCheckFile is the on-disk cache, keyed to the current binary
22// version. Writes a JSON blob with the last observed latest tag and
23// when we checked, so we can skip the network call on most launches.
24const updateCheckFile = "update-check.json"
25
26// githubReleasesAPI is the REST endpoint we query. Using the API (not
27// the HTML redirect) because the JSON response is stable and small.
28const githubReleasesAPI = "https://api.github.com/repos/patriceckhart/zot/releases/latest"
29
30// UpdateInfo describes the result of an update check. Zero-value means
31// "no update available, no error, don't show anything".
32type UpdateInfo struct {
33 Current string // e.g. "0.0.4"
34 Latest string // e.g. "0.0.5"
35 Available bool // true when latest > current
36 URL string // release page url for the changelog link
37}
38
39// updateCache is the on-disk structure written to $ZOT_HOME.
40type updateCache struct {
41 CheckedAt time.Time `json:"checked_at"`
42 // The version that was current when we last checked. Invalidates
43 // the cache if the binary itself has been updated since.
44 CurrentAt string `json:"current_at"`
45 Latest string `json:"latest"`
46 URL string `json:"url"`
47}
48
49// CheckForUpdate returns info about a newer release, using a cached
50// result when one is fresh enough. Designed to be called at tui
51// startup and rendered as a dismissible banner.
52//
53// Always returns a usable UpdateInfo (zero-value on error). The
54// banner renderer skips the display when Available is false, so a
55// network failure silently no-ops; we never block startup on this.
56func CheckForUpdate(ctx context.Context, zotHome, currentVersion string) UpdateInfo {
57 // Dev builds ("0.0.0") never have an update to offer. Skip.
58 if currentVersion == "" || currentVersion == "dev" || currentVersion == "0.0.0" {
59 return UpdateInfo{}
60 }
61
62 cachePath := filepath.Join(zotHome, updateCheckFile)
63 if c, ok := readUpdateCache(cachePath); ok {
64 // Cache is fresh and tracks the same binary version.
65 // Additional guard: only trust the cache when it already
66 // reports an available update. If the cache says "up to
67 // date" (Latest <= Current) we re-check anyway so a
68 // release published after the last launch is picked up
69 // without waiting out the full TTL. The API call is a
70 // single 4s request; cheap to do on every up-to-date
71 // launch, and skips entirely for the common "installed a
72 // new version, cache reports update available" path.
73 if time.Since(c.CheckedAt) < updateCheckTTL && c.CurrentAt == currentVersion {
74 info := buildInfo(currentVersion, c.Latest, c.URL)
75 if info.Available {
76 return info
77 }
78 // fall through to re-check
79 }
80 }
81
82 latest, url, err := fetchLatestRelease(ctx)
83 if err != nil {
84 // Network or auth failure (common while the repo is private
85 // and no GITHUB_TOKEN is set). Silent no-op; we'll try again
86 // after the TTL on the next launch.
87 return UpdateInfo{}
88 }
89
90 _ = writeUpdateCache(cachePath, updateCache{
91 CheckedAt: time.Now().UTC(),
92 CurrentAt: currentVersion,
93 Latest: latest,
94 URL: url,
95 })
96
97 return buildInfo(currentVersion, latest, url)
98}
99
100// CheckForUpdateAsync runs CheckForUpdate in a goroutine, delivers the
101// result to the returned channel, and never blocks startup. The
102// channel is always closed; receivers should `ok`-check.
103func CheckForUpdateAsync(zotHome, currentVersion string) <-chan UpdateInfo {
104 ch := make(chan UpdateInfo, 1)
105 go func() {
106 defer close(ch)
107 ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
108 defer cancel()
109 ch <- CheckForUpdate(ctx, zotHome, currentVersion)
110 }()
111 return ch
112}
113
114func buildInfo(current, latest, url string) UpdateInfo {
115 info := UpdateInfo{
116 Current: current,
117 Latest: strings.TrimPrefix(latest, "v"),
118 URL: url,
119 }
120 info.Available = versionLess(info.Current, info.Latest)
121 return info
122}
123
124// versionLess reports whether a is an older semantic version than b.
125// Invalid versions return false so update checks never offer a downgrade.
126func versionLess(a, b string) bool {
127 comparison, err := compareVersions(a, b)
128 return err == nil && comparison < 0
129}
130
131func compareVersions(a, b string) (int, error) {
132 a = normalizedVersion(a)
133 b = normalizedVersion(b)
134 if !semver.IsValid(a) {
135 return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(a, "v"))
136 }
137 if !semver.IsValid(b) {
138 return 0, fmt.Errorf("invalid semantic version %q", strings.TrimPrefix(b, "v"))
139 }
140 return semver.Compare(a, b), nil
141}
142
143func normalizedVersion(v string) string {
144 v = versionOnly(v)
145 if !strings.HasPrefix(v, "v") {
146 v = "v" + v
147 }
148 return v
149}
150
151// fetchLatestRelease queries the GitHub API for the latest published
152// release. Honours $GITHUB_TOKEN for private repos.
153func fetchLatestRelease(ctx context.Context) (tag, url string, err error) {
154 req, err := http.NewRequestWithContext(ctx, "GET", githubReleasesAPI, nil)
155 if err != nil {
156 return "", "", err
157 }
158 req.Header.Set("accept", "application/vnd.github+json")
159 req.Header.Set("x-github-api-version", "2022-11-28")
160 if tok := os.Getenv("GITHUB_TOKEN"); tok != "" {
161 req.Header.Set("authorization", "Bearer "+tok)
162 }
163
164 client := &http.Client{Timeout: 4 * time.Second}
165 resp, err := client.Do(req)
166 if err != nil {
167 return "", "", err
168 }
169 defer resp.Body.Close()
170 if resp.StatusCode != 200 {
171 return "", "", fmt.Errorf("github api %d", resp.StatusCode)
172 }
173
174 var body struct {
175 TagName string `json:"tag_name"`
176 HTMLURL string `json:"html_url"`
177 }
178 if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
179 return "", "", err
180 }
181 return body.TagName, body.HTMLURL, nil
182}
183
184// readUpdateCache loads the last check result. Returns ok=false on
185// any error (missing file, bad json) so callers just treat it as a
186// cache miss and re-fetch.
187func readUpdateCache(path string) (updateCache, bool) {
188 b, err := os.ReadFile(path)
189 if err != nil {
190 return updateCache{}, false
191 }
192 var c updateCache
193 if err := json.Unmarshal(b, &c); err != nil {
194 return updateCache{}, false
195 }
196 return c, true
197}
198
199func writeUpdateCache(path string, c updateCache) error {
200 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
201 return err
202 }
203 b, err := json.MarshalIndent(c, "", " ")
204 if err != nil {
205 return err
206 }
207 return os.WriteFile(path, b, 0o600)
208}
209
210// cachedLatest, cachedLatestOnce: used elsewhere in the binary if we
211// want a synchronous read without triggering a network call.
212var (
213 cachedLatest string
214 cachedLatestOnce sync.Once
215)