selfupdate

package
v1.5.145 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

This file caches the latest-release check so golder's startup banner can show "update available" without a network call on every launch (US-004, FR-10). The cache lives at $GOLDER_HOME/update-check.json (or ~/.golder/update-check.json) and records the last check time plus the latest tag seen. CachedLatest reads it synchronously (fast, local); StartBackgroundCheck refreshes it off the hot path when older than the TTL, so a fresh result shows on the next launch. All failures are silent: a missing or corrupt cache, an unresolvable home, or a network error never surfaces an error or blocks startup.

This file implements golder's binary self-replacement for `golder update` (issue #466). Given the current build version it discovers the latest release (via version.go), downloads the matching goreleaser archive for the running GOOS/GOARCH, verifies its SHA256 against the release's checksums.txt, and atomically replaces the running executable.

The archive naming mirrors .goreleaser.yaml and install.sh exactly, so this stays a single source of truth with the release tooling. Replacement is atomic: the new binary is written to a temp file in the target's directory and os.Rename'd over the current executable, so a failure mid-download never leaves a truncated binary in place.

Package selfupdate provides version discovery and comparison for golder's self-update feature (issue #465). It queries the GitHub Releases API for the latest published tag of the golder repository and compares it against the build-time version injected into the main package, so both `golder update` and the interactive startup banner can decide whether a newer release exists.

The build version ("dev" for `go build`/`go run` from source, a real vX.Y.Z for goreleaser builds) is not owned by this package; callers pass it in. Non-release versions ("", "dev", "unknown") are treated as non-comparable so a source build never reports a spurious update.

Index

Constants

View Source
const CheckInterval = time.Hour

CheckInterval is shared by the desktop GUI and the interactive TUI.

View Source
const Repo = "getan/golder"

Repo is the GitHub "owner/name" whose releases back golder's self-update. It matches the release target in .goreleaser.yaml and install.sh.

View Source
const SiteLatestURL = "https://golder-cli.pages.dev/api/latest"

SiteLatestURL is the golder site's latest-release endpoint (golder-site repo). It resolves the tag server-side and caches it on Cloudflare's edge: no token, no anonymous rate limit, and reachable from mainland China where api.github.com often is not. install.sh resolves the version the same way.

Variables

This section is empty.

Functions

func CachedLatest

func CachedLatest() (latest string, fresh bool)

CachedLatest returns the latest tag recorded in the cache and whether the cache is still fresh (younger than checkTTL). A missing, corrupt, or unreadable cache yields ("", false). It never returns an error — the banner must not break on a bad cache.

func CheckLatest added in v1.5.94

func CheckLatest(ctx context.Context, current string) (string, error)

CheckLatest refreshes release metadata regardless of the startup cache's TTL. This is a metadata-only request, not a download or installation. Successful checks also refresh the shared cache for later startups.

func IsReleaseVersion

func IsReleaseVersion(v string) bool

IsReleaseVersion reports whether v is a real release version that can be compared against a tag. The build defaults ("dev", "unknown") and the empty string are not release versions.

func LatestForStartup added in v1.5.62

func LatestForStartup(current string, wait time.Duration) string

LatestForStartup uses a fresh cache or waits briefly for a background check. A slow check continues to fill the cache, but never interrupts an active UI.

func LatestTag

func LatestTag(ctx context.Context, client *http.Client, repo, current string) (string, error)

LatestTag resolves repo's latest release tag (e.g. "v0.4.0"). The golder site endpoint is tried first (reachable from mainland China, no anonymous rate limit, cached on the edge); the GitHub API is the fallback and is the path that honors GITHUB_TOKEN. If client is nil a client with a short timeout is used.

current is the running build's version, or "" when the caller has none. The site endpoint is edge-cached for a short while and can lag several releases behind GitHub. Returning its tag as soon as it looks newer than current would make `golder update` install an intermediate version and require another run. Both sources are therefore always consulted and the newer tag wins; if GitHub is unreachable (common in mainland China), the site's answer still stands.

func Monitor added in v1.5.94

func Monitor(ctx context.Context, check func())

Monitor schedules serialized metadata checks until the frontend closes. Startup checks and installation remain explicit frontend decisions.

func Run

func Run(ctx context.Context, current string, out, errOut io.Writer) int

Run performs `golder update` (self-update golder). It discovers the latest release, compares it to current, and replaces the running binary when a newer release exists. When current is a source build ("dev"), it cannot compare and proceeds to install the latest. Returns a process exit code.

func SkipVersion added in v1.5.62

func SkipVersion(tag string) error

SkipVersion atomically records the exact offered version for later startups.

func StartBackgroundCheck

func StartBackgroundCheck(current string)

StartBackgroundCheck refreshes the cache off the hot path when it is stale (older than checkTTL). It returns immediately; the actual network check runs in a goroutine so it never blocks banner rendering or first input. When current is not a release version (dev/unknown) it does nothing — there is nothing to compare against. All errors are swallowed: a failed check simply leaves the cache untouched for next time.

func StartupOffer added in v1.5.62

func StartupOffer(current, latest string) bool

StartupOffer reports whether latest is a newer, unskipped stable release.

func UpdateAvailable

func UpdateAvailable(current, latest string) (available, comparable bool)

UpdateAvailable compares the current build version against a release tag and reports whether the tag is strictly newer. The comparable return is false when current is not a release version (source builds) or when either value cannot be parsed as a version — callers should treat non-comparable as "no update to offer" rather than an error.

Types

type Updater

type Updater struct {
	HTTPClient *http.Client
	// Repo is "owner/name"; ReleaseBaseURL overrides the download host in tests.
	Repo           string
	ReleaseBaseURL string // e.g. https://github.com/getan/golder/releases/download
	GOOS, GOARCH   string
	// ExecPath is the executable to replace; defaults to os.Executable().
	ExecPath string
	// BeforeReplace optionally rechecks the target after downloading, immediately
	// before the atomic replacement. An error leaves the executable untouched.
	BeforeReplace func() error
	// Mirrors are URL prefixes tried, in order, after the direct GitHub
	// download fails (each applied as prefix+url). Empty means no fallback.
	// NewUpdater fills it from GOLDER_MIRROR or the built-in defaults; tests
	// set it explicitly.
	Mirrors []string
	// MinRateBytesPerSec aborts an attempt whose windowed average throughput
	// falls below this floor (see defaultMinRate); zero means the default.
	MinRateBytesPerSec int64
	// RateWindow is the measurement window for MinRateBytesPerSec, and also
	// the time an attempt may deliver no data before it is dropped; zero
	// means defaultRateWindow.
	RateWindow time.Duration
}

Updater performs a self-replacement. Its fields are seams for testing; NewUpdater fills them with production defaults.

func NewUpdater

func NewUpdater() (*Updater, error)

NewUpdater returns an Updater configured for the running process.

func (*Updater) Apply

func (u *Updater) Apply(ctx context.Context, tag string, out io.Writer) error

Apply downloads the release identified by tag, verifies its checksum, and atomically replaces the target executable. tag is like "v0.4.0".

The archive is accepted from the first source whose bytes match the trusted digest — a mirror serving a corrupt or stale copy simply fails over to the next source. The digest itself follows a trust ladder (see fetchChecksums): GitHub direct first, mirrors only when corroborated.

Jump to

Keyboard shortcuts

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