selfupdate

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package selfupdate checks GitHub for newer agent-init releases and replaces the running binary in place. It is the engine behind `agent-init upgrade`.

The flow is deliberately conservative: a download is never installed without first verifying its SHA-256 against the release's checksums.txt, and the binary is swapped atomically (write-temp-then-rename) so a failure can't leave a half-written executable on disk.

Index

Constants

View Source
const DefaultRepo = "Lillevang/agent-init"

DefaultRepo is the GitHub "owner/name" releases are pulled from.

Variables

This section is empty.

Functions

This section is empty.

Types

type Asset

type Asset struct {
	Name string
	URL  string
}

Asset is one downloadable file attached to a release.

type CheckResult

type CheckResult struct {
	Current        string
	Latest         string
	NewerAvailable bool
}

CheckResult reports the outcome of a version check.

type GitHubSource

type GitHubSource struct {
	Repo       string // "owner/name"
	APIBaseURL string // default "https://api.github.com"
	HTTPClient *http.Client
	// Token is sent as a bearer credential to lift the unauthenticated rate
	// limit. Optional; release assets are public.
	Token string
}

GitHubSource implements Source against the GitHub releases REST API.

func NewGitHubSource

func NewGitHubSource(repo string) *GitHubSource

NewGitHubSource returns a source for repo, reading an optional token from GITHUB_TOKEN or GH_TOKEN so authenticated users avoid the low anonymous rate limit.

func (*GitHubSource) Download

func (g *GitHubSource) Download(ctx context.Context, url string) ([]byte, error)

Download fetches the bytes at url, capped at maxDownloadBytes.

func (*GitHubSource) Latest

func (g *GitHubSource) Latest(ctx context.Context) (Release, error)

Latest fetches the repo's latest published release.

type Release

type Release struct {
	Tag    string
	Assets []Asset
}

Release is the subset of a GitHub release this package consumes.

type Source

type Source interface {
	Latest(ctx context.Context) (Release, error)
	Download(ctx context.Context, url string) ([]byte, error)
}

Source fetches release metadata and downloads release assets. It is the seam the CLI fills with a GitHub client and tests fill with an in-memory fake.

type Updater

type Updater struct {
	Source  Source
	Out     io.Writer
	GOOS    string
	GOARCH  string
	ExePath func() (string, error)
}

Updater drives a check or an upgrade against a Source. GOOS/GOARCH and ExePath are fields rather than hard-coded calls so tests can target arbitrary platforms and a throwaway file instead of the real executable.

func NewUpdater

func NewUpdater(src Source, out io.Writer) *Updater

NewUpdater returns an Updater wired to the current platform and the running executable's path.

func (*Updater) Check

func (u *Updater) Check(ctx context.Context, current string) (CheckResult, error)

Check fetches the latest release and compares it to current without downloading anything.

func (*Updater) Upgrade

func (u *Updater) Upgrade(ctx context.Context, opts UpgradeOptions) error

Upgrade fetches the latest release, verifies its checksum, and atomically replaces the running binary. It is a no-op (with a notice) when the current version is already the newest and Force is unset.

type UpgradeOptions

type UpgradeOptions struct {
	Current string
	// Force installs the latest release even when current is already newest (or
	// unparseable, e.g. a dev build).
	Force bool
	// DryRun downloads and verifies the release but stops before replacing the
	// binary.
	DryRun bool
}

UpgradeOptions tunes an upgrade run.

Jump to

Keyboard shortcuts

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