Help us shape the future of Go! Take the 2026 Go Developer Survey

selfupdate

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package selfupdate replaces the running gotools binary with the newest release for this operating system and architecture.

A Source says where releases come from and fetches them: the latest tag, the checksum manifest and the binary. An Updater installs one: it downloads the binary, checks its SHA-256 against the release's checksums, proves the candidate runs, and swaps it in with the old binary kept as ".old" until the new one has run from its final location. Nothing is replaced unless the checksum matched and the candidate ran.

The checksum is an integrity check against the release, not proof of authorship; installing with `go install` is the stronger path.

Index

Constants

View Source
const (
	Repo = "piusalfred/gotools"
	Name = "gotools"
)

Repo is the GitHub repository releases are published to, and Name the program's name, which prefixes every release asset.

View Source
const Checksums = "checksums-sha256.txt"

Checksums is the release asset that lists the SHA-256 of every binary.

Variables

This section is empty.

Functions

func RunVersion

func RunVersion(ctx context.Context, bin string) error

RunVersion runs `<bin> version`: a binary that cannot even do that must not replace a working one. It runs only after the checksum matched.

Types

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer sends an HTTP request. *http.Client is one.

type Relation

type Relation int

Relation is how the running version compares to the latest release.

const (
	// Older: the release is newer, so there is an update.
	Older Relation = iota
	// Same: the running version is the latest release.
	Same
	// Newer: the running version is newer than the latest release, or is a
	// pre-release build.
	Newer
)

func Compare

func Compare(current, latest string) Relation

Compare says whether current needs updating to latest. A current version that is not a version at all (a development build) is Older: anything is newer. Only a strictly newer release is an update, so a build newer than the latest release is never "updated" backwards.

type Source

type Source struct {
	APIURL       string // JSON of the latest release
	DownloadBase string // <base>/<tag>/<asset>
	Client       Doer
	GOOS, GOARCH string
	UserAgent    string
}

Source says where releases come from. Tests point it at a server.

func GitHub

func GitHub(version string) Source

GitHub is the Source of the project's releases for this platform. version names the running program in the User-Agent.

func (Source) Asset

func (s Source) Asset() string

Asset is the release file for the Source's platform.

func (Source) AssetURL

func (s Source) AssetURL(tag, asset string) string

AssetURL is where the release tag's asset is downloaded from.

func (Source) Checksum

func (s Source) Checksum(ctx context.Context, tag, asset string) (string, error)

Checksum finds asset in the release's checksum manifest.

func (Source) Download

func (s Source) Download(ctx context.Context, tag, asset string, w io.Writer) (string, error)

Download streams the asset into w and returns its SHA-256.

func (Source) Latest

func (s Source) Latest(ctx context.Context) (string, error)

Latest asks for the latest release and checks the tag before it is used to build URLs: whatever the API says ends up in them.

type Updater

type Updater struct {
	Source Source
	// Check proves that the binary at the path runs. RunVersion when nil.
	Check func(ctx context.Context, bin string) error
	// Out narrates the update; Err reports a rollback.
	Out, Err io.Writer
}

Updater installs a release.

func (*Updater) Install

func (u *Updater) Install(ctx context.Context, tag, target string) error

Install downloads tag's binary for the platform, verifies it and swaps it in for target.

The candidate is written next to the target, so the final rename never crosses a filesystem, and a directory that cannot be written to ends the update before anything is downloaded. It must match the release's checksum and pass Check before it replaces anything; after the swap it must pass Check again from its final path, or the old binary is put back.

Jump to

Keyboard shortcuts

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