selfupdate

package
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 25 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 checks GitHub for new releases of the CLI, installs them, and renders their release notes in the terminal.

Index

Constants

View Source
const (
	// CheckInterval is how often GitHub is asked for a new release, and how
	// often the "new version available" message is shown.
	CheckInterval = 24 * time.Hour
)
View Source
const ClientTimeout = 15 * time.Second

ClientTimeout is how long a request from the update check or `shipyard upgrade` waits for GitHub before giving up.

View Source
const DefaultAPIBaseURL = "https://api.github.com"

DefaultAPIBaseURL is the GitHub API origin releases are read from.

View Source
const DefaultNotesLines = 40

DefaultNotesLines caps how much of the release notes is printed after an upgrade, so a jump across several releases doesn't flood the terminal.

View Source
const NoCheckEnv = "SHIPYARD_NO_UPDATE_CHECK"

NoCheckEnv turns the update check off.

Variables

This section is empty.

Functions

func AfterUpgrade

func AfterUpgrade(ctx context.Context, w io.Writer, c *Client, statePath, current, installed string, latest *Release, now time.Time) error

AfterUpgrade reports an upgrade from current to installed, where latest is the release that was asked for, prints the notes for the versions in between, and records them as seen in the state at statePath (skipped when statePath is empty). installed differs from latest when Homebrew's formula is behind the GitHub release.

func AssetName

func AssetName(goos, goarch string) string

AssetName is the release file for this platform, as .goreleaser.yaml names it.

func ChecksOff

func ChecksOff(enabled bool) bool

ChecksOff reports whether update checks are turned off: by NoCheckEnv, by running in CI, or by the update_check setting, which the caller passes as enabled. The terminal notice and the MCP server's notice both honor it.

func CleanupOld

func CleanupOld()

CleanupOld removes what an earlier upgrade left next to the binary: the binary a Windows upgrade renamed out of the way, and downloads abandoned by Ctrl-C, which skips the deferred removal. Downloads under an hour old may belong to an upgrade running in another shell, so they're kept.

func Executable

func Executable() (string, error)

Executable returns the running binary's path with symlinks resolved, which is where a direct install has to write the new binary.

func HomeDir

func HomeDir() string

HomeDir is the home directory whose state file to use: the invoking user's under sudo, which on Linux sets HOME to root's, so that notes recorded as seen by `sudo shipyard upgrade` aren't shown to the user again.

func IsNewer

func IsNewer(current, candidate string) bool

IsNewer reports whether candidate is a newer version than current. It is false when either fails to parse, so a dev build is never told to upgrade.

func IsRelease

func IsRelease(v string) bool

IsRelease reports whether v looks like a released version, as opposed to a local build ("undefined", "1.9.0-8-g94afae8") or a goreleaser snapshot ("1.9.1-SNAPSHOT-abc"). A local build parses as a pre-release older than the tag it's based on, so treating it as a release would "upgrade" it backwards.

func RenderNotes

func RenderNotes(w io.Writer, releases []Release, maxLines int)

RenderNotes prints the releases' notes, newest first, as terminal text: headings in bold, bullets as "•", code in cyan, links as their text. Output stops after maxLines with a pointer to the full notes.

func StatePath

func StatePath(home string) string

StatePath is where the state is kept: ~/.shipyard/update-state.json.

Types

type Asset

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

Asset is a file attached to a release.

type Client

type Client struct {
	HTTP    *http.Client
	BaseURL string
}

Client reads releases from the GitHub API.

func NewClient

func NewClient(timeout time.Duration) *Client

NewClient returns a client whose requests give up after timeout.

func (*Client) Between

func (c *Client) Between(ctx context.Context, from, to string) ([]Release, error)

Between returns the releases newer than from and no newer than to, newest first: the notes a user moving from one version to the other has missed. Pre-releases are left out unless to is itself that pre-release.

func (*Client) Latest

func (c *Client) Latest(ctx context.Context, prerelease bool) (*Release, error)

Latest returns the newest stable release, or the newest release of any kind when prerelease is true.

func (*Client) Releases

func (c *Client) Releases(ctx context.Context) ([]Release, error)

Releases returns the most recent releases, newest first as GitHub orders them.

type Installer

type Installer struct {
	HTTP   *http.Client
	Out    io.Writer
	Exe    string
	Method Method
	// Current is the running version; a Homebrew upgrade that leaves it in
	// place is reported as a failure, not a success.
	Current string
	// Brew runs a brew subcommand; tests replace it.
	Brew func(ctx context.Context, out io.Writer, args ...string) error
	// BrewVersion returns the version of shipyard Homebrew has installed.
	BrewVersion func(ctx context.Context) (string, error)
}

Installer upgrades the running binary to a release.

func NewInstaller

func NewInstaller(out io.Writer, current string) (*Installer, error)

NewInstaller returns an installer for the running binary, at version current.

func (*Installer) Install

func (in *Installer) Install(ctx context.Context, rel *Release) (string, error)

Install replaces the running binary with the release's and returns the version now installed. Homebrew can install an older version than rel when its formula lags the GitHub release.

type Method

type Method int

Method is how the running binary was installed, which decides how to upgrade it.

const (
	// MethodDirect is a binary placed on disk by the install script, a
	// download from the releases page, or a previous self-upgrade.
	MethodDirect Method = iota
	// MethodHomebrew is a binary in Homebrew's Cellar. Overwriting it would
	// leave Homebrew recording the old version, so upgrades go through brew.
	MethodHomebrew
)

func DetectMethod

func DetectMethod(exe string) Method

DetectMethod reports how the binary at the resolved path exe was installed.

type Notice

type Notice struct {
	Text string
	// contains filtered or unexported fields
}

Notice is the text to print after the command. Call Shown once it has been printed, so the same notes and message aren't printed again.

func (*Notice) Shown

func (nt *Notice) Shown()

Shown records that the notice was printed. It keeps a newer seen version: while the command ran, another shell may have upgraded further and shown those notes, which would otherwise be shown again.

type Notifier

type Notifier struct {
	Current   string
	StatePath string
	Client    *Client
	Now       func() time.Time
	// LatestOnly checks for a newer release and leaves the upgrade notes
	// alone, for a caller that never shows the notice (the MCP server): it
	// would otherwise fetch notes nobody sees and use up the terminal's retry.
	LatestOnly bool
}

Notifier works out what to tell the user after a command: the notes for a version they upgraded to since the last run, and whether a newer release is out. It never asks anything, so it can't block a script or an agent.

func (*Notifier) Check

func (n *Notifier) Check(ctx context.Context) *Notice

Check makes the network requests and returns what to print, which may be empty. It saves what it learns from GitHub straight away; what the user has seen is only recorded by Notice.Shown.

type Release

type Release struct {
	TagName    string  `json:"tag_name"`
	Body       string  `json:"body"`
	HTMLURL    string  `json:"html_url"`
	Draft      bool    `json:"draft"`
	Prerelease bool    `json:"prerelease"`
	Assets     []Asset `json:"assets"`
}

Release is the subset of a GitHub release the updater uses.

func (Release) Asset

func (r Release) Asset(name string) (Asset, bool)

Asset returns the attached file with exactly this name.

func (Release) Version

func (r Release) Version() string

Version returns the release's version without the leading "v".

type State

type State struct {
	// LastChecked is when GitHub last answered the latest-release check. Before
	// each attempt it's set back so that, if the attempt fails or the command
	// exits first, the next check is due in checkRetry.
	LastChecked time.Time `json:"last_checked"`
	// LatestVersion is the newest release that check found.
	LatestVersion string `json:"latest_version,omitempty"`
	// NotifiedAt is when the "new version available" message was last shown;
	// it's shown at most once per CheckInterval.
	NotifiedAt time.Time `json:"notified_at,omitzero"`
	// LastSeenVersion is the version whose release notes the user has seen,
	// or the version they first ran. A newer running version means it was
	// upgraded since, by any method, and its notes are shown once.
	LastSeenVersion string `json:"last_seen_version,omitempty"`
	// NotesTried is when the notes for an upgrade made elsewhere were last
	// fetched; a failed fetch is retried at most hourly.
	NotesTried time.Time `json:"notes_tried,omitzero"`
}

State is what the updater remembers between runs. It lives in its own file, not config.yaml, because commands such as set_org rewrite config.yaml.

func LoadState

func LoadState(path string) State

LoadState reads the state, returning an empty one if the file is missing or unreadable: losing it only means checking again.

func (State) Save

func (s State) Save(path string) error

Save writes the state atomically, so two shells starting at once can't leave a half-written file.

type Version

type Version struct {
	Major, Minor, Patch int
	Pre                 string
}

Version is a parsed semantic version: MAJOR.MINOR.PATCH with an optional pre-release suffix such as "rc.1". Build metadata after "+" is ignored.

func ParseVersion

func ParseVersion(s string) (Version, error)

ParseVersion accepts "1.9.0", "v1.9.0" and "1.10.0-rc.1".

func (Version) Compare

func (v Version) Compare(o Version) int

Compare returns -1, 0 or 1 as v is older than, equal to, or newer than o, following semver precedence: 1.9.0 < 1.10.0, and 1.10.0-rc.1 < 1.10.0.

func (Version) String

func (v Version) String() string

Jump to

Keyboard shortcuts

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