updatecheck

package
v0.6.18 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package updatecheck queries GitHub for the latest commit on the CLI's main branch, caches the result for 24h, and prints a one-line stderr warning when the running binary is behind.

Design constraints:

  • Must never block the parent command for noticeable time (we cap waits at a few hundred ms).
  • Must never break the parent command on any failure (corrupt cache, network error, 4xx/5xx from GitHub — all swallowed).
  • Must be opt-out-friendly: env var, config flag, --quiet, dirty tree, and a cobra annotation for special commands (MCP stdio).

Index

Constants

View Source
const (
	DefaultAPIURL      = "https://api.github.com/repos/taufinity/cli/commits/main"
	DefaultCacheMaxAge = 24 * time.Hour
	DefaultHTTPTimeout = 2 * time.Second

	// EnvDisable, when set to "1", skips both the network check and the
	// warning at exit.
	EnvDisable = "TAUFINITY_NO_UPDATE_CHECK"

	// AnnotationSuppress is the cobra command annotation that disables the
	// update check side effects (background goroutine + warning) for that
	// command. Used by `taufinity mcp stdio`.
	AnnotationSuppress = "suppress-update-warning"
)

Defaults.

Variables

This section is empty.

Functions

func IsBehind added in v0.5.0

func IsBehind(info buildinfo.Info, cache Cache) bool

IsBehind reports whether the running binary's commit differs from the cached latest SHA. Returns false when either value is missing or too short to compare safely (dirty tree, built without VCS info, never checked). It is the pure staleness predicate — callers decide what to do with it.

func MaybeWarn

func MaybeWarn(out io.Writer, info buildinfo.Info, cache Cache, opts Options) bool

MaybeWarn writes a one-line warning to out if the current binary is behind the cached latest SHA. Returns true if it wrote.

func SaveCache

func SaveCache(c Cache) error

SaveCache writes the cache atomically: tmp file + rename. On the same filesystem the rename is atomic, so a mid-write process exit leaves either the previous valid cache or the new valid cache — never a torn file.

Types

type Cache

type Cache struct {
	// CheckedAt is when we last queried GitHub. Zero value means "never".
	CheckedAt time.Time `json:"checked_at"`

	// LatestSHA is the SHA returned by GitHub at CheckedAt, or "" if the call
	// failed (we cache the failure for 24h to avoid hammering on a broken
	// network).
	LatestSHA string `json:"latest_sha"`
}

Cache is the on-disk record of the last GitHub commits-API check.

func LoadCache

func LoadCache() Cache

LoadCache reads the cache from disk. A missing or unparseable file is NOT an error — it returns a zero-value Cache so callers can treat it as "never checked." We deliberately don't surface read errors to the caller: a corrupt cache must never break the parent CLI command.

func (Cache) IsFresh

func (c Cache) IsFresh(now time.Time, maxAge time.Duration) bool

IsFresh reports whether the cache was written less than maxAge ago.

type Options

type Options struct {
	// Quiet suppresses the warning unconditionally.
	Quiet bool

	// ConfigDisabled is the resolved value of the user-config opt-out
	// (UserConfig.UpdateCheck == "false").
	ConfigDisabled bool

	// CommandSuppress is set by the caller when the running cobra command
	// (or any ancestor) carries AnnotationSuppress.
	CommandSuppress bool
}

Options controls MaybeWarn behavior.

type Runner

type Runner struct {
	APIURL     string
	HTTPClient *http.Client
	Timeout    time.Duration
	Now        func() time.Time // for tests
	Debug      io.Writer        // non-nil to write debug lines on failure
	// contains filtered or unexported fields
}

Runner controls a background staleness check.

func (*Runner) Start

func (r *Runner) Start(ctx context.Context)

Start kicks off the network check in a background goroutine. The goroutine writes the result (or a failure marker) to the cache file when it finishes. Call Wait to block for completion up to a bounded duration.

func (*Runner) Wait

func (r *Runner) Wait(d time.Duration)

Wait blocks for the goroutine to finish or for d to elapse. If d is zero, Wait returns immediately if the goroutine hasn't started or has already finished.

Jump to

Keyboard shortcuts

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