Documentation
¶
Overview ¶
Package upgradecheck handles passive "new version available" notices emitted after unrelated commands succeed. Results are cached to disk with a 24-hour TTL so normal use never makes more than one GitHub API call per day per user.
Index ¶
Constants ¶
const DefaultTTL = 24 * time.Hour
DefaultTTL is how long a cache file is considered fresh.
Variables ¶
This section is empty.
Functions ¶
func CachePath ¶
CachePath returns the path to the cache file, creating the containing directory if needed. Follows XDG on Linux/macOS and %LOCALAPPDATA% on Windows.
func Disabled ¶
Disabled reports whether passive upgrade checks are suppressed in the current environment. The answer combines: explicit opt-out flag, TRUESTAMP_NO_UPGRADE_CHECK env var, CI detection, non-TTY stderr, and a "dev" current version.
The cmd layer is responsible for reading the --no-upgrade-check flag and passing it in via flagDisabled.
func MaybeNotify ¶
MaybeNotify writes a single-line "new version available" notice to w if an upgrade is available. No-op when Disabled() returns true.
The flow:
- If cache is fresh (<24h), use the cached result, no network.
- Otherwise, record the attempt in the cache first, keeping the latest version an earlier check found, so the attempt counts for the TTL however it ends. Then spawn a goroutine with a 2-second deadline to refresh the cache, and wait up to waitBudget for it.
- A refresh that succeeds in time overwrites the record with what it found. One that fails ends the wait at once, and the record stands. One still running when the wait runs out is abandoned when the command exits, and the record stands too. A run that cannot reach GitHub (offline, rate limited, the releases gone, packets dropped) therefore pays for one check per TTL, not the wait on every command.
All errors are swallowed, upgrade checks must never interfere with the primary command's output.
func WriteCache ¶
WriteCache atomically overwrites the cache file. Errors are returned to the caller but are typically non-fatal (the notification path ignores them).
Types ¶
type Cache ¶
type Cache struct {
LastChecked time.Time `json:"last_checked"`
LatestVersion string `json:"latest_version"`
CheckedFromVersion string `json:"checked_from_version"`
}
Cache holds the most recent upgrade check result. Time is always stored in UTC.