update

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package update powers the passive update nudge (CachedCheck) and the `ana update` self-update verb (SelfUpdate). All logic sits behind injected deps so cmd/ana stays thin wiring.

Stdlib-only; mirrors goreleaser's archive layout and install.sh's URL template.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CachePath

func CachePath(env func(string) string) (string, error)

CachePath resolves $XDG_CACHE_HOME/ana/update-check.json with a $HOME/.cache/... fallback; both unset → error so callers skip the check.

func CachedCheck

func CachedCheck(ctx context.Context, deps CacheDeps, ttl time.Duration, currentVersion string) (string, bool, error)

CachedCheck returns the newest observed release tag plus a notify flag that is true only when currentVersion is older than that tag. When the cache is fresh (age < ttl) no HTTP call is made. A stale or missing cache triggers a LatestRelease call and an atomic cache rewrite. Any error short-circuits to (_, false, err); callers ignore the error since the nudge is best-effort.

func CmpSemver

func CmpSemver(a, b string) int

CmpSemver returns -1/0/+1 comparing two three-int semver strings. Accepts optional leading `v` and an optional `-prerelease` suffix. Prerelease versions sort below their non-prerelease counterparts at the same X.Y.Z (so goreleaser's `prerelease: auto` beta→stable path notifies correctly). Malformed input on either side returns 0 — a bad tag must never trigger a nudge.

func LatestRelease

func LatestRelease(ctx context.Context, client HTTPDoer) (string, error)

LatestRelease returns the `tag_name` from GitHub's /releases/latest.

func ParseInterval

func ParseInterval(s *string) (time.Duration, bool)

ParseInterval interprets the Config.UpdateCheckInterval pointer: nil → (4h, true). "0" or "disable" → (0, false). Any time.ParseDuration-friendly string → (d, true). A malformed duration silently falls back to the default — we never want a bad config value to block the user's verb.

func SelfUpdate

func SelfUpdate(ctx context.Context, deps Deps, currentVersion string, w io.Writer, jsonOut bool) error

SelfUpdate runs the full self-update flow: resolve latest, compare, skip if already current, else download + verify + extract + atomic replace. jsonOut selects a single-object JSON summary on w; otherwise plain-text progress is written line by line. "Already up to date" is success (nil error).

Types

type CacheDeps

type CacheDeps struct {
	Env  func(string) string
	Now  func() time.Time
	HTTP HTTPDoer
}

CacheDeps is the injection boundary for CachedCheck.

type Deps

type Deps struct {
	HTTP    HTTPDoer
	GOOS    string
	GOARCH  string
	ExePath func() (string, error)
	Rename  func(old, newp string) error
	TempDir func() (string, error)
}

Deps is the injection boundary for SelfUpdate; zero fields fall through to stdlib defaults via resolveDeps.

func DefaultDeps

func DefaultDeps() Deps

DefaultDeps wires Deps against the process environment.

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPDoer is the narrow HTTP interface this package consumes. http.Client satisfies it; tests pass a fake that returns canned responses.

Jump to

Keyboard shortcuts

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