selfupdate

package
v1.5.39 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 22 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 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 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 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. It exists because the site endpoint is edge-cached for a short while: right after a release it can still answer with the previous tag, which is exactly the moment someone runs `golder update`. When the site's answer is NOT newer than current — the answer that would make the caller say "already up to date" — the GitHub API is consulted as a cross-check and the newer of the two wins. Shelling out to GitHub on that path costs one request in the rare case and never happens when the site already reports an upgrade, so the common case keeps its single round trip and mainland China (where api.github.com is often unreachable) still gets a usable answer: a failed cross-check leaves the site's tag standing.

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 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 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
	// 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