selfupdate

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 28 Imported by: 1 module, 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

Package selfupdate selects, verifies and installs signed GitHub releases of the running program.

A Client reads the releases of one repository through its API. Check reports the newest eligible stable release, List offers versions to choose from, and Target selects an exact one, an older one for a rollback included. Update installs the newest stable release in one call when it is newer. For a chosen target, Prepare downloads and verifies the release without changing anything, and InstallPrepared installs it. Consequences returns the data the application words its question before an installation from; all texts belong to the application.

The application supplies the repository, the versions, the archive name of each version, the program name, the signature namespace, the name of the signature file, the trusted keys and every installation path; the signature file has no default.

Always enforced

The repository API address must be https, or http on the local machine, and name /repos/owner/repository. A release is eligible only if it is published, carries a valid version not below MinimumVersion, and lists its archive, checksums.txt and the signature file exactly once each, at asset URLs under the repository's own API path. A build without a release version is refused with ErrDevelopmentBuild.

Prepare checks the signature with release.VerifyChecksums before it downloads the archive, then the archive's checksum and its contents with release.ProgramSHA256. Every archive entry must have a safe path that no other entry shares; a single leading "./" is accepted. The program is "bin/<program>" in the archive, with ".exe" added in a .zip archive, and each Companion must be one non-empty regular file. The program is at most 256 MiB and a companion at most 2 MiB. Metadata files and the archive are read only up to a size limit, and a download ends after 30 seconds without data. The release API never follows a redirect. An asset download follows a redirect only to https hosts of GitHub, at most ten times. The token is sent to the repository's API host only, never along a redirect.

A Prepared release is bound to the repository and keys of the client that prepared it and to the namespace, signature file, archive, program and companion entries of its version; InstallPrepared refuses it under any other policy. It refuses a program path that is not a regular file named after the program, or for a release of the old set after the current or the previous program, and companion paths that are not regular files named after the entries of the release's set, that are not absolute or that are not unique. An older release is installed only after ValidateRollback accepted it. Companions are installed first and the program last, each staged next to its target and renamed into place; on Windows the running program is moved aside first and CleanupLeftovers removes it later. If the program cannot be replaced, every companion is restored.

Options

  • Client.Token: a bearer token for the repository's API, for example for a private repository. Default: no token is sent.
  • Client.HTTPClient: the HTTP client for the release API and the downloads, copied with its redirect rules replaced. Default: a client with a 30 second timeout for the API; downloads are bounded by the stall limit only.
  • Client.Companions: files the archive carries next to the program, such as a manpage, installed with it and restored if the program cannot be replaced. Default: the program only.
  • Client.PreviousSignature: after an application renamed its signature namespace and file, names the old set and the newest release signed only with it. Releases up to and including that version are verified with the old set alone, every later release with the current set alone, under the same trusted keys and the same archive checks. The old set may also name the previous program and companion entries, with which the archives of those releases are read and installed. Which archive name a version has stays with ArchiveName. The installers of the release package always use the current set. Default: one set for every release.
  • Client.ValidateRollback: checks the prepared program of an older release, for example with Prepared.ReadsConfig against an isolated copy of a configuration. Default: installing an older release is refused.
  • Client.BeforeReplace: receives the verified release files right before the first installed file is replaced, for example to hand a running process over; an error ends the installation with nothing replaced. Default: nothing is called.
  • Client.AfterReplace: receives the verified release files once the program is replaced. Default: nothing is called.
  • Client.Progress: reports the bytes received and the announced size, or -1, while the archive downloads. Default: no report.

Index

Constants

This section is empty.

Variables

View Source
var ErrDevelopmentBuild = errors.New("self-update is unavailable in a development build")

ErrDevelopmentBuild reports a running build without a release version.

Functions

func CleanupLeftovers

func CleanupLeftovers(executable string) error

CleanupLeftovers removes a prior Windows replacement at the explicit executable path.

func OtherProcesses

func OtherProcesses(name string) int

OtherProcesses counts the processes of the current user besides this one whose command name is name, read through /proc. A failure is -1, because the count only informs a question.

Types

type Client

type Client struct {
	RepositoryAPI  string
	CurrentVersion string
	MinimumVersion string
	ArchiveName    func(version string) string
	UserAgent      string
	HTTPClient     *http.Client
	// Token, when set, is sent as a bearer token to the host of RepositoryAPI and to no other host.
	Token     string
	Namespace string
	// SignatureAsset is the name of the release asset that holds the SSH signature over checksums.txt,
	// verified under Namespace. It is required and has no default: a plain file name that is neither
	// checksums.txt nor the archive name.
	SignatureAsset string
	TrustedKeys    []ssh.PublicKey
	// PreviousSignature, when set, makes releases up to and including its LastVersion verify only under
	// the previous signature set instead of Namespace and SignatureAsset, and read the previous program
	// and companion entries where it names them.
	PreviousSignature *PreviousSignature
	ProgramName       string
	// Companions are installed together with the program and restored if the program cannot be replaced.
	Companions []Companion
	Executable string
	// ValidateRollback must check the prepared program against an isolated copy of the application's
	// configuration, for example with Prepared.ReadsConfig. Installing an older release requires it.
	ValidateRollback func(context.Context, *Prepared) error
	// BeforeReplace runs once everything is verified and staged, right before the first installed file is
	// replaced. An error ends the installation with nothing replaced.
	BeforeReplace func(context.Context, ReleaseFiles) error
	// AfterReplace runs once the program is replaced. The installation is complete by then.
	AfterReplace func(context.Context, ReleaseFiles)
	// Progress is called while the archive downloads, with the bytes received and the announced size, or -1
	// when the server does not announce it.
	Progress func(done, total int64)
}

Client holds the release source and policy supplied by the application.

func (*Client) Check

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

Check reports the newest eligible stable release without downloading artifacts.

func (*Client) Consequences

func (c *Client) Consequences(t Target) Consequences

Consequences describes what installing t does on this platform now.

func (*Client) InstallPrepared

func (c *Client) InstallPrepared(ctx context.Context, p *Prepared) (Result, error)

InstallPrepared installs a verified release at the caller supplied paths: companions first, the program last. If the program cannot be replaced, the companions are restored.

func (*Client) List

func (c *Client) List(ctx context.Context) ([]Entry, error)

List shows ten newest stable releases, newer prereleases, and the running version if eligible.

func (*Client) Prepare

func (c *Client) Prepare(ctx context.Context, target Target) (*Prepared, error)

Prepare downloads and verifies the selected release after caller confirmation. It verifies the signature over checksums.txt before it downloads the archive, and the archive before it reads it.

func (*Client) Target

func (c *Client) Target(ctx context.Context, tag string) (Target, error)

Target selects an exact release version, including an older version for rollback. A release that cannot be chosen is a *TargetError.

func (*Client) Update

func (c *Client) Update(ctx context.Context) (Result, error)

Update installs the newest eligible stable release when it is newer than the running version. It never installs an older release; a rollback goes through Target, Prepare and InstallPrepared.

type Companion

type Companion struct {
	// Entry is the canonical slash-separated path in the archive, for example "share/man/man1/tool.1".
	Entry string
	// Path is the absolute installation path. Its base name must equal the base name of Entry.
	Path string
}

Companion is a file that the release archive carries next to the program, such as a manpage.

type ConfigCheck

type ConfigCheck struct {
	// Config holds the bytes of the configuration. The program only ever sees a copy.
	Config []byte
	// FileName is the plain file name of the copy, such as "config.yaml".
	FileName string
	// Args returns the program's arguments for checking the copy at configPath.
	Args func(configPath string) []string
	// Timeout bounds the run; zero means 20 seconds.
	Timeout time.Duration
}

ConfigCheck asks the program of a prepared release whether it reads a configuration.

type Consequences

type Consequences struct {
	Version    string
	Prerelease bool
	Older      bool
	// RenamesRunning reports that the running executable is moved aside to a ".old" file, which
	// CleanupLeftovers removes later, because the platform cannot replace a running program in place.
	RenamesRunning bool
	// OtherProcesses is the number of other processes of the current user that run the program, or -1 when
	// it cannot be counted.
	OtherProcesses int
}

Consequences is the data an application words its question before an installation from.

type Entry

type Entry struct {
	Version    string
	Prerelease bool
	Current    bool
	Older      bool
}

Entry is a version offered in the release list.

type Prepared

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

Prepared holds a downloaded and verified release and can only be made by Prepare outside this package.

func (*Prepared) Archive

func (p *Prepared) Archive() []byte

Archive returns a copy of the verified archive.

func (*Prepared) ArchiveName

func (p *Prepared) ArchiveName() string

ArchiveName is the file name of the archive, by which checksums.txt lists it.

func (*Prepared) Checksums

func (p *Prepared) Checksums() []byte

Checksums returns a copy of the verified checksums.txt.

func (*Prepared) ReadsConfig

func (p *Prepared) ReadsConfig(ctx context.Context, check ConfigCheck) (bool, error)

ReadsConfig runs the verified program of the release in a fresh private directory, against a copy of the configuration, with an environment that points every home, configuration, data, cache, runtime and temporary directory into that directory and carries nothing else of the caller but PATH and the Windows system root. Exit status 0 means the program reads the configuration, any other exit status means it does not. A run that cannot be carried out is an error, not an answer. The program's output is discarded, because it is not trusted and may quote the configuration.

func (*Prepared) Signature

func (p *Prepared) Signature() []byte

Signature returns a copy of the signature over checksums.txt.

func (*Prepared) Version

func (p *Prepared) Version() string

Version is the tag of the prepared release.

type PreviousSignature added in v0.13.0

type PreviousSignature struct {
	// LastVersion is the newest release signed only with the previous set, such as "v1.4.0".
	LastVersion string
	// Namespace is the signature namespace of those releases.
	Namespace string
	// SignatureAsset is the name of the release asset that holds their signature over checksums.txt, with
	// the same rules as Client.SignatureAsset.
	SignatureAsset string
	// ProgramName, when set, is the program name in the archives of those releases, which then carry
	// "bin/<ProgramName>". It must differ from Client.ProgramName. Empty keeps Client.ProgramName.
	ProgramName string
	// ReplaceCompanions makes Companions the companions of those releases instead of Client.Companions;
	// an empty Companions then means that their archives carry none. Without it Companions must be empty.
	ReplaceCompanions bool
	// Companions are the companion entries of those releases and their installation paths, with the rules
	// of Client.Companions.
	Companions []Companion
}

PreviousSignature describes the release set of the oldest releases after an application renamed its signature namespace and signature file, and possibly its program and companion files. A release up to and including LastVersion is verified only with this namespace and signature file, and its archive is read with this program name and these companions where they are given; every later release uses only the current Client.Namespace, Client.SignatureAsset, Client.ProgramName and Client.Companions. A later release that still carries the previous file is not verified with it. The same Client.TrustedKeys and the same archive checks apply to both sets, and the installers of the release package always use the current set.

type ReleaseFiles

type ReleaseFiles struct {
	Checksums, Signature, Archive string
	// ArchiveName is the archive's file name in the release, by which Checksums lists it.
	ArchiveName string
	// Rollback reports that the release is older than the running version.
	Rollback bool
}

ReleaseFiles names copies of the verified release files in a private directory that exists until InstallPrepared returns. All paths are absolute.

type Result

type Result struct {
	Current         string
	Latest          string
	UpdateAvailable bool
	Updated         bool
	// Companions lists the companion paths that were written; a companion whose content is unchanged is
	// left alone.
	Companions []string
}

Result describes the newest eligible stable release or, from InstallPrepared, the installed release.

type Target

type Target struct {
	Entry
	// contains filtered or unexported fields
}

Target is a selected release. Its metadata is retained for later verification.

type TargetError

type TargetError struct {
	Version string
	Reason  TargetReason
}

TargetError reports a release that cannot be chosen. The application words its message from Reason.

func (*TargetError) Error

func (e *TargetError) Error() string

type TargetReason

type TargetReason int

TargetReason says why a release cannot be chosen.

const (
	// TargetInvalidVersion is a version that is not a v-prefixed semantic version.
	TargetInvalidVersion TargetReason = iota + 1
	// TargetBelowMinimum is a version older than Client.MinimumVersion.
	TargetBelowMinimum
	// TargetNotFound is a version the repository has no published release for.
	TargetNotFound
	// TargetIneligible is a release that is a draft, unsigned, incomplete, or has assets outside the
	// repository.
	TargetIneligible
	// TargetRunning is the running version, which has nothing to install.
	TargetRunning
)

type UnsupportedInstallationError

type UnsupportedInstallationError struct {
	Path   string
	Reason string
}

UnsupportedInstallationError reports an installed file that cannot be replaced safely in place.

func (*UnsupportedInstallationError) Error

Jump to

Keyboard shortcuts

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