selfupdate

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 18 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.

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 string) (string, error)

LatestTag queries the GitHub Releases API for repo's latest release tag (e.g. "v0.4.0"). If client is nil a client with a short timeout is used. When the GITHUB_TOKEN environment variable is set it is sent as a bearer token to raise the API rate limit, mirroring install.sh.

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
}

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".

Jump to

Keyboard shortcuts

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