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
- Variables
- func Backoff(n int) time.Duration
- func Due(statePath string, now time.Time) bool
- func IsRelease(s string) bool
- func MarkNotified(statePath, version string, now time.Time)
- func Newer(a, b string) bool
- func Soaked(published, now time.Time) bool
- func StatePath() (string, error)
- func Update(statePath string, fn func(*State))
- func WriteState(path string, s State) error
- type Asset
- type AutoState
- type Client
- type Manager
- type Notice
- type Release
- type State
Constants ¶
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" )
const CheckInterval = 24 * time.Hour
CheckInterval is how long a release check is reused before asking again.
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.
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.
const StateFile = "state.yml"
StateFile is the file in the state directory that records the last check.
Variables ¶
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
Backoff is the wait after the nth background upgrade failure in a row: 1 day, 2, 4, then 8 days at most.
func Due ¶
Due reports whether the last check recorded at statePath is older than CheckInterval, so a new one may go to the network.
func IsRelease ¶
IsRelease reports whether s is a semantic version (with or without "v"). Development builds ("dev", "test", a git describe string) are not.
func MarkNotified ¶
MarkNotified records that the notice for version was printed at now.
func Newer ¶
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
Soaked reports whether a release published at published is at least SoakPeriod old at now. An unknown publication time is not soaked.
func Update ¶ added in v0.6.0
Update applies fn to the state at statePath and saves it (best effort).
func WriteState ¶
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
BackingOff reports whether a failed background upgrade still blocks a new attempt at now.
func (*AutoState) RecordAutoFailure ¶ added in v0.6.0
RecordAutoFailure records a failed background upgrade to version and schedules the next attempt with Backoff.
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.
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 ¶
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.
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.
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 ¶
ReadState loads the check state from path. A missing or unreadable file is an empty state.