upgradecheck

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 8 Imported by: 0

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

View Source
const DefaultTTL = 24 * time.Hour

DefaultTTL is how long a cache file is considered fresh.

Variables

This section is empty.

Functions

func CachePath

func CachePath() (string, error)

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

func Disabled(flagDisabled bool, stderr *os.File, currentVersion string) bool

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

func MaybeNotify(w io.Writer, flagDisabled bool, currentVersion string)

MaybeNotify writes a single-line "new version available" notice to w if an upgrade is available. No-op when Disabled() returns true.

The flow:

  1. If cache is fresh (<24h), use the cached result — no network.
  2. Otherwise, spawn a goroutine with a 2-second deadline to refresh the cache. Wait up to 500ms for it to complete; if it doesn't, we skip emitting a notice this invocation. The cache write is still committed when the goroutine finishes, so the next run picks it up.

All errors are swallowed — upgrade checks must never interfere with the primary command's output.

func WriteCache

func WriteCache(c Cache) error

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.

func ReadCache

func ReadCache() (*Cache, error)

ReadCache returns the cached result, or (nil, nil) if the cache is absent or unreadable. Never returns an error — cache misses are a normal part of operation.

func (*Cache) IsFresh

func (c *Cache) IsFresh(ttl time.Duration) bool

IsFresh returns true if c.LastChecked is within TTL of now.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL