|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/civitai/cli/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +// updateCacheName is the cache file (sibling of config.yaml) that backs the |
| 13 | +// non-blocking daily update notice. It records when we last checked GitHub, the |
| 14 | +// latest version we saw, and when we last actually showed the notice. |
| 15 | +const updateCacheName = "update-check.json" |
| 16 | + |
| 17 | +// updateCacheTTL is how often the background refresh is allowed to hit GitHub. |
| 18 | +const updateCacheTTL = 24 * time.Hour |
| 19 | + |
| 20 | +// updateNotifyTTL throttles how often the notice is shown — at most once a day |
| 21 | +// even while the user stays behind, so it's a gentle nudge, not nagware. |
| 22 | +const updateNotifyTTL = 24 * time.Hour |
| 23 | + |
| 24 | +// updateCache is the on-disk JSON state for the cached update notice. |
| 25 | +type updateCache struct { |
| 26 | + LastCheck time.Time `json:"last_check"` |
| 27 | + LatestVersion string `json:"latest_version"` |
| 28 | + LastNotified time.Time `json:"last_notified"` |
| 29 | +} |
| 30 | + |
| 31 | +// updateCachePath returns ~/.config/civitai/update-check.json (the same dir as |
| 32 | +// config.yaml). Returns an error only if the config dir can't be resolved. |
| 33 | +func updateCachePath() (string, error) { |
| 34 | + dir, err := config.Dir() |
| 35 | + if err != nil { |
| 36 | + return "", err |
| 37 | + } |
| 38 | + return filepath.Join(dir, updateCacheName), nil |
| 39 | +} |
| 40 | + |
| 41 | +// readUpdateCache loads the cache. A missing or corrupt file is treated as an |
| 42 | +// empty cache with no error — the notice path must never fail a command. |
| 43 | +func readUpdateCache(path string) updateCache { |
| 44 | + b, err := os.ReadFile(path) |
| 45 | + if err != nil { |
| 46 | + return updateCache{} |
| 47 | + } |
| 48 | + var c updateCache |
| 49 | + if err := json.Unmarshal(b, &c); err != nil { |
| 50 | + return updateCache{} // corrupt => empty, fail-silent. |
| 51 | + } |
| 52 | + return c |
| 53 | +} |
| 54 | + |
| 55 | +// writeUpdateCache persists the cache atomically (0600 temp + rename) into the |
| 56 | +// config dir. The dir is created if needed. Errors are returned to the caller, |
| 57 | +// but the only caller (the detached refresh) ignores them — a failed write just |
| 58 | +// means the next run refreshes again. |
| 59 | +func writeUpdateCache(path string, c updateCache) error { |
| 60 | + dir := filepath.Dir(path) |
| 61 | + if err := os.MkdirAll(dir, 0o700); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + b, err := json.Marshal(c) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + tmp, err := os.CreateTemp(dir, ".update-check-*.json.tmp") |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + tmpName := tmp.Name() |
| 73 | + defer func() { _ = os.Remove(tmpName) }() |
| 74 | + if _, err := tmp.Write(b); err != nil { |
| 75 | + _ = tmp.Close() |
| 76 | + return err |
| 77 | + } |
| 78 | + if err := tmp.Close(); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + return os.Rename(tmpName, path) |
| 82 | +} |
| 83 | + |
| 84 | +// cacheIsFresh reports whether the cached check is younger than the TTL, so no |
| 85 | +// background refresh is needed. A zero LastCheck (empty cache) is never fresh. |
| 86 | +func cacheIsFresh(c updateCache, now time.Time) bool { |
| 87 | + if c.LastCheck.IsZero() { |
| 88 | + return false |
| 89 | + } |
| 90 | + return now.Sub(c.LastCheck) < updateCacheTTL |
| 91 | +} |
| 92 | + |
| 93 | +// noticeDecision describes what the post-run hook should do, derived purely from |
| 94 | +// the cache + current version + the clock (no I/O, so it's unit-testable). |
| 95 | +type noticeDecision struct { |
| 96 | + // show is true when a one-line "newer version" notice should be printed. |
| 97 | + show bool |
| 98 | + // notice is the text to print (only meaningful when show is true). |
| 99 | + notice string |
| 100 | + // refresh is true when a background GitHub refresh should be kicked off. |
| 101 | + refresh bool |
| 102 | +} |
| 103 | + |
| 104 | +// decideNotice computes whether to show the notice and/or refresh the cache, |
| 105 | +// from the cached state and the resolved current version. Pure function. |
| 106 | +// |
| 107 | +// Rules: |
| 108 | +// - refresh when the cache is stale (older than updateCacheTTL or empty). |
| 109 | +// - show the notice only when: the cached latest parses, current parses, |
| 110 | +// latest > current, AND we haven't notified within updateNotifyTTL. |
| 111 | +func decideNotice(c updateCache, current string, now time.Time) noticeDecision { |
| 112 | + d := noticeDecision{refresh: !cacheIsFresh(c, now)} |
| 113 | + |
| 114 | + if c.LatestVersion == "" || !isParseableVersion(c.LatestVersion) { |
| 115 | + return d // no usable cached value yet (e.g. first run) => no notice. |
| 116 | + } |
| 117 | + // Only nag when we KNOW the current version and it's genuinely behind. |
| 118 | + if !isParseableVersion(current) { |
| 119 | + return d |
| 120 | + } |
| 121 | + if compareVersions(current, c.LatestVersion) != -1 { |
| 122 | + return d // up to date or ahead. |
| 123 | + } |
| 124 | + // Throttle: at most one notice per updateNotifyTTL. |
| 125 | + if !c.LastNotified.IsZero() && now.Sub(c.LastNotified) < updateNotifyTTL { |
| 126 | + return d |
| 127 | + } |
| 128 | + d.show = true |
| 129 | + d.notice = formatUpdateNotice(c.LatestVersion, current) |
| 130 | + return d |
| 131 | +} |
| 132 | + |
| 133 | +// formatUpdateNotice renders the single dim stderr line. |
| 134 | +func formatUpdateNotice(latest, current string) string { |
| 135 | + return "A new version of civitai is available: " + latest + |
| 136 | + " (you have " + current + "). Run 'civitai upgrade' to update." |
| 137 | +} |
0 commit comments