update

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 15 Imported by: 0 modules, 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 update finds ytrack releases on GitHub, compares versions, keeps the once-a-day update check state, and tells which package manager owns an installed binary.

Index

Constants

View Source
const (
	// Repo is the GitHub repository that publishes ytrack releases.
	Repo = "BrainerVirus/youtrack-cli"
	// DefaultAPIURL is the GitHub REST API root.
	DefaultAPIURL = "https://api.github.com"
)
View Source
const CheckInterval = 24 * time.Hour

CheckInterval is how long a release check is reused before asking again.

View Source
const SoakPeriod = 24 * time.Hour

SoakPeriod is how old a release must be before the background upgrade installs it, so a broken release can be pulled before it spreads. A manual `ytrack upgrade` does not wait.

View Source
const SpawnInterval = time.Hour

SpawnInterval is the least time between two background upgrade starts, so a child that dies before it records anything is not restarted on every run.

View Source
const StateFile = "state.yml"

StateFile is the file in the state directory that records the last check.

Variables

View Source
var ErrReleaseNotFound = errors.New("release not found")

ErrReleaseNotFound is returned when the requested release does not exist.

Functions

func Backoff added in v0.6.0

func Backoff(n int) time.Duration

Backoff is the wait after the nth background upgrade failure in a row: 1 day, 2, 4, then 8 days at most.

func Due

func Due(statePath string, now time.Time) bool

Due reports whether the last check recorded at statePath is older than CheckInterval, so a new one may go to the network.

func IsRelease

func IsRelease(s string) bool

IsRelease reports whether s is a semantic version (with or without "v"). Development builds ("dev", "test", a git describe string) are not.

func MarkNotified

func MarkNotified(statePath, version string, now time.Time)

MarkNotified records that the notice for version was printed at now.

func Newer

func Newer(a, b string) bool

Newer reports whether version a is newer than b. It is false when either is not a semantic version.

func Soaked added in v0.6.0

func Soaked(published, now time.Time) bool

Soaked reports whether a release published at published is at least SoakPeriod old at now. An unknown publication time is not soaked.

func StatePath

func StatePath() (string, error)

StatePath returns the path of the check state file in the state directory.

func Update added in v0.6.0

func Update(statePath string, fn func(*State))

Update applies fn to the state at statePath and saves it (best effort).

func WriteState

func WriteState(path string, s State) error

WriteState saves the check state to path.

Types

type Asset

type Asset struct {
	Name string `json:"name"`
	URL  string `json:"browser_download_url"`
	// APIURL is the asset's REST API URL: with Accept application/octet-stream
	// it serves the same bytes when the download CDN fails.
	APIURL string `json:"url"`
	Size   int64  `json:"size"`
}

Asset is a file attached to a release.

type AutoState added in v0.6.0

type AutoState struct {
	// StartedAt is when a run last started a background upgrade.
	StartedAt time.Time `yaml:"started_at,omitempty"`

	// From, To and UpgradedAt record the last successful background upgrade;
	// Announced is set once a terminal was told about it.
	From       string    `yaml:"from,omitempty"`
	To         string    `yaml:"to,omitempty"`
	UpgradedAt time.Time `yaml:"upgraded_at,omitempty"`
	Announced  bool      `yaml:"announced,omitempty"`

	// The last failed attempt since the last success: the release it tried,
	// when, why, how many failures in a row, and when it may try again.
	FailedVersion string    `yaml:"failed_version,omitempty"`
	FailedAt      time.Time `yaml:"failed_at,omitempty"`
	Error         string    `yaml:"error,omitempty"`
	Failures      int       `yaml:"failures,omitempty"`
	RetryAt       time.Time `yaml:"retry_at,omitempty"`
}

AutoState is what the background upgrade remembers between runs.

func (*AutoState) BackingOff added in v0.6.0

func (a *AutoState) BackingOff(now time.Time) bool

BackingOff reports whether a failed background upgrade still blocks a new attempt at now.

func (*AutoState) RecordAutoFailure added in v0.6.0

func (a *AutoState) RecordAutoFailure(version string, err error, now time.Time)

RecordAutoFailure records a failed background upgrade to version and schedules the next attempt with Backoff.

func (*AutoState) RecordSuccess added in v0.6.0

func (a *AutoState) RecordSuccess(from, to string, now time.Time, background bool)

RecordSuccess records an upgrade from one version to another and clears any failure and backoff. background marks it for a one-time announcement.

type Client

type Client struct {
	HTTP       *http.Client
	APIURL     string // DefaultAPIURL when empty
	UserAgent  string
	Repository string // Repo when empty
}

Client reads releases from the GitHub REST API without credentials.

func (*Client) Latest

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

Latest returns the newest release that is neither a draft nor a prerelease.

func (*Client) Tag

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

Tag returns the release for tag (with or without the leading "v").

type Manager

type Manager struct {
	Name    string // "Homebrew", "Scoop", "WinGet"
	Command string // the command that upgrades ytrack with it
}

Manager is a package manager that owns an installed ytrack binary.

func DetectManager

func DetectManager(paths ...string) *Manager

DetectManager returns the package manager that installed the binary at any of paths (typically the executable path and its symlink target), or nil when none did.

type Notice

type Notice struct {
	Current, Latest, URL string
}

Notice is a newer release worth telling the user about.

func CheckDaily

func CheckDaily(ctx context.Context, c *Client, statePath, current string, now time.Time) (*Notice, error)

CheckDaily asks for the latest release when the last check recorded at statePath is older than CheckInterval, records this check (also when it fails, so an offline machine asks once a day, not on every run; a failed check keeps the release cached before it) and returns a Notice when that release is newer than current. It returns nil without any network call when a check already happened within the interval. A check cancelled through ctx (abandoned by the caller) records nothing, so the next run asks again.

func Pending

func Pending(statePath, current string, now time.Time) *Notice

Pending returns the cached release at statePath as a Notice when it is newer than current and was not announced within CheckInterval. It reads only the state file, never the network.

type Release

type Release struct {
	Tag        string `json:"tag_name"`
	URL        string `json:"html_url"`
	Prerelease bool   `json:"prerelease"`
	Draft      bool   `json:"draft"`
	// PublishedAt is when the release was published; the background upgrade
	// waits SoakPeriod after it.
	PublishedAt time.Time `json:"published_at"`
	Assets      []Asset   `json:"assets"`
}

Release is a published GitHub release.

func (*Release) Asset

func (r *Release) Asset(name string) *Asset

Asset returns the asset called name, or nil.

func (*Release) Version

func (r *Release) Version() string

Version returns the tag without its leading "v".

type State

type State struct {
	CheckedAt     time.Time `yaml:"checked_at"`
	LatestVersion string    `yaml:"latest_version,omitempty"`
	LatestURL     string    `yaml:"latest_url,omitempty"`
	// LatestPublishedAt is when LatestVersion was published.
	LatestPublishedAt time.Time `yaml:"latest_published_at,omitempty"`
	// NotifiedAt and NotifiedVersion record the last notice printed, so a
	// cached newer release is announced once per CheckInterval.
	NotifiedAt      time.Time `yaml:"notified_at,omitempty"`
	NotifiedVersion string    `yaml:"notified_version,omitempty"`

	Auto AutoState `yaml:"auto_upgrade,omitempty"`
}

State is what the daily check remembers between runs.

func ReadState

func ReadState(path string) State

ReadState loads the check state from path. A missing or unreadable file is an empty state.

func (*State) AutoDue added in v0.6.0

func (s *State) AutoDue(current string, now time.Time) bool

AutoDue reports whether the cached state calls for a background upgrade of current at now: a newer stable release is known and soaked, no failure backoff is active, and none was started within SpawnInterval.

func (*State) SetLatest added in v0.6.0

func (s *State) SetLatest(rel *Release)

SetLatest caches rel as the latest release.

Jump to

Keyboard shortcuts

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