update

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: ISC Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckWritePermission

func CheckWritePermission(binaryPath string) error

CheckWritePermission verifies the binary's directory is writable before downloading. Returns nil if writable, or an error with an actionable message.

func IsHomebrew

func IsHomebrew(binaryPath string) bool

IsHomebrew returns true if the resolved binary path indicates Homebrew management. Checks both Intel (/usr/local/Caskroom/, /usr/local/Cellar/) and Apple Silicon (/opt/homebrew/Caskroom/, /opt/homebrew/Cellar/) paths.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache manages the update check cooldown file.

func NewCache

func NewCache(dir string) *Cache

NewCache creates a Cache that stores state in dir/update-check.json.

func (*Cache) Path

func (c *Cache) Path() string

Path returns the full path to the cache file.

func (*Cache) Read

func (c *Cache) Read() (*cacheData, error)

Read loads and unmarshals the cache file.

func (*Cache) ShouldCheck

func (c *Cache) ShouldCheck(cooldown time.Duration) bool

ShouldCheck returns true if the cache is missing, corrupt, or the cooldown has expired. A true return means the caller should query the GitHub API.

func (*Cache) Write

func (c *Cache) Write(latestVersion, releaseURL string) error

Write persists the cache to disk using atomic write-then-rename. Pattern replicates FileTokenStore.Write() from internal/auth/tokenstore.go.

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker queries GitHub Releases for the latest version.

func NewChecker

func NewChecker(currentVersion string, cache *Cache, logger *slog.Logger) *Checker

NewChecker creates a Checker for the given current version. If currentVersion cannot be parsed as semver (e.g. "dev"), IsDev() returns true and all checks are no-ops.

func (*Checker) Check

func (c *Checker) Check(ctx context.Context) (*Result, error)

Check queries the GitHub Releases API and compares the latest tag against the current version. It does NOT read or write the cache — use CheckWithCooldown for cache-gated checks.

func (*Checker) CheckWithCooldown

func (c *Checker) CheckWithCooldown(ctx context.Context, cooldown time.Duration) (*Result, error)

CheckWithCooldown is the background-safe entry point. It respects the dev guard and 24h cooldown cache before hitting the GitHub API.

func (*Checker) FormatCheckOutput

func (c *Checker) FormatCheckOutput(r *Result) string

FormatCheckOutput returns the --check-update formatted output. Returns empty string if result is nil.

func (*Checker) FormatNotification

func (c *Checker) FormatNotification(r *Result) string

FormatNotification returns the two-line gh CLI style notification. Returns empty string if no update is available or result is nil.

func (*Checker) IsDev

func (c *Checker) IsDev() bool

IsDev returns true if the binary is a local development build. Dev builds never trigger version checks or network calls.

func (*Checker) SetAPIURL

func (c *Checker) SetAPIURL(url string)

SetAPIURL overrides the GitHub API URL. Intended for testing.

type ProgressFunc

type ProgressFunc func(msg string)

ProgressFunc is called with status messages during the update process. All messages go to stderr. The caller provides the callback.

type ReleaseAsset

type ReleaseAsset struct {
	Name               string `json:"name"`
	BrowserDownloadURL string `json:"browser_download_url"`
}

ReleaseAsset represents a downloadable file attached to a GitHub release.

type Result

type Result struct {
	UpdateAvailable bool           `json:"update_available"`
	CurrentVersion  string         `json:"current_version"`
	LatestVersion   string         `json:"latest_version"`
	ReleaseURL      string         `json:"release_url"`
	Assets          []ReleaseAsset `json:"assets,omitempty"`
}

Result holds the outcome of a version check.

type Updater

type Updater struct {
	// contains filtered or unexported fields
}

Updater downloads, verifies, and installs binary updates from GitHub Releases.

func NewUpdater

func NewUpdater(checker *Checker, logger *slog.Logger) *Updater

NewUpdater creates an Updater that uses the given Checker for version lookups.

func (*Updater) Update

func (u *Updater) Update(ctx context.Context, binaryPath string, progress ProgressFunc) error

Update performs the full self-update sequence:

  1. Check for latest version via GitHub API
  2. Verify not already up to date
  3. Find platform-specific archive and checksums in release assets
  4. Download checksums.txt, parse expected hash
  5. Download platform archive, verify SHA256
  6. Extract binary from tar.gz to temp file
  7. Rename current binary to .bak (rollback preservation)
  8. Rename new binary to target path (atomic replace)

On any failure after step 7, the .bak is restored. binaryPath is the resolved path to the current executable.

Jump to

Keyboard shortcuts

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