|
| 1 | +// Package updater checks for newer CLI versions and caches the result. |
| 2 | +package updater |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "golang.org/x/mod/semver" |
| 13 | + |
| 14 | + "github.com/NodeOps-app/createos-cli/internal/pkg/version" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + githubRepo = "NodeOps-app/createos-cli" |
| 19 | + cacheFile = ".version-check" |
| 20 | + cacheTTL = 24 * time.Hour |
| 21 | + checkTimeout = 3 * time.Second |
| 22 | +) |
| 23 | + |
| 24 | +type versionCache struct { |
| 25 | + CheckedAt int64 `json:"checked_at"` |
| 26 | + Latest string `json:"latest"` |
| 27 | +} |
| 28 | + |
| 29 | +// LatestVersion returns the latest available stable version if it is newer |
| 30 | +// than the running binary. Returns empty string if up to date, offline, or |
| 31 | +// on the nightly channel. |
| 32 | +func LatestVersion() string { |
| 33 | + // Nightly users use commit-based comparison via `upgrade` — skip notification |
| 34 | + if version.Channel == "nightly" || version.Version == "dev" { |
| 35 | + return "" |
| 36 | + } |
| 37 | + |
| 38 | + latest := cachedVersion() |
| 39 | + if latest == "" { |
| 40 | + latest = fetchLatest() |
| 41 | + saveCache(latest) |
| 42 | + } |
| 43 | + |
| 44 | + if latest == "" { |
| 45 | + return "" |
| 46 | + } |
| 47 | + |
| 48 | + if semver.Compare(version.Version, latest) < 0 { |
| 49 | + return latest |
| 50 | + } |
| 51 | + return "" |
| 52 | +} |
| 53 | + |
| 54 | +func cacheFilePath() string { |
| 55 | + home, err := os.UserHomeDir() |
| 56 | + if err != nil { |
| 57 | + return "" |
| 58 | + } |
| 59 | + return filepath.Join(home, ".createos", cacheFile) |
| 60 | +} |
| 61 | + |
| 62 | +// cachedVersion returns the cached latest version if the cache is fresh. |
| 63 | +func cachedVersion() string { |
| 64 | + path := cacheFilePath() |
| 65 | + if path == "" { |
| 66 | + return "" |
| 67 | + } |
| 68 | + |
| 69 | + data, err := os.ReadFile(path) //nolint:gosec |
| 70 | + if err != nil { |
| 71 | + return "" |
| 72 | + } |
| 73 | + |
| 74 | + var c versionCache |
| 75 | + if err := json.Unmarshal(data, &c); err != nil { |
| 76 | + return "" |
| 77 | + } |
| 78 | + |
| 79 | + if time.Since(time.Unix(c.CheckedAt, 0)) > cacheTTL { |
| 80 | + return "" // stale — trigger a fresh fetch |
| 81 | + } |
| 82 | + |
| 83 | + return c.Latest |
| 84 | +} |
| 85 | + |
| 86 | +func saveCache(latest string) { |
| 87 | + path := cacheFilePath() |
| 88 | + if path == "" { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + data, err := json.Marshal(versionCache{ |
| 93 | + CheckedAt: time.Now().Unix(), |
| 94 | + Latest: latest, |
| 95 | + }) |
| 96 | + if err != nil { |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + _ = os.WriteFile(path, data, 0o600) |
| 101 | +} |
| 102 | + |
| 103 | +func fetchLatest() string { |
| 104 | + url := "https://api.github.com/repos/" + githubRepo + "/releases/latest" |
| 105 | + |
| 106 | + ctx, cancel := context.WithTimeout(context.Background(), checkTimeout) |
| 107 | + defer cancel() |
| 108 | + |
| 109 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) |
| 110 | + if err != nil { |
| 111 | + return "" |
| 112 | + } |
| 113 | + req.Header.Set("Accept", "application/vnd.github+json") |
| 114 | + |
| 115 | + resp, err := http.DefaultClient.Do(req) |
| 116 | + if err != nil { |
| 117 | + return "" |
| 118 | + } |
| 119 | + defer resp.Body.Close() //nolint:errcheck |
| 120 | + |
| 121 | + if resp.StatusCode != http.StatusOK { |
| 122 | + return "" |
| 123 | + } |
| 124 | + |
| 125 | + var result struct { |
| 126 | + TagName string `json:"tag_name"` |
| 127 | + } |
| 128 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 129 | + return "" |
| 130 | + } |
| 131 | + |
| 132 | + return result.TagName |
| 133 | +} |
0 commit comments