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
- func CachedLatest() (latest string, fresh bool)
- func CheckLatest(ctx context.Context, current string) (string, error)
- func IsReleaseVersion(v string) bool
- func LatestForStartup(current string, wait time.Duration) string
- func LatestTag(ctx context.Context, client *http.Client, repo, current string) (string, error)
- func Monitor(ctx context.Context, check func())
- func Run(ctx context.Context, current string, out, errOut io.Writer) int
- func SkipVersion(tag string) error
- func StartBackgroundCheck(current string)
- func StartupOffer(current, latest string) bool
- func UpdateAvailable(current, latest string) (available, comparable bool)
- type Updater
Constants ¶
const CheckInterval = time.Hour
CheckInterval is shared by the desktop GUI and the interactive TUI.
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.
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 ¶
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
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 ¶
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
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 ¶
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 Monitor ¶ added in v1.5.94
Monitor schedules serialized metadata checks until the frontend closes. Startup checks and installation remain explicit frontend decisions.
func Run ¶
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
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
StartupOffer reports whether latest is a newer, unskipped stable release.
func UpdateAvailable ¶
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 ¶
NewUpdater returns an Updater configured for the running process.
func (*Updater) Apply ¶
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.