update

package
v0.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCheckInterval = 24 * time.Hour

DefaultCheckInterval is the default interval between update checks.

Variables

This section is empty.

Functions

func CompareVersions

func CompareVersions(current, latest string) int

CompareVersions compares two version strings. Returns:

  • -1 if current < latest
  • 0 if current == latest
  • 1 if current > latest

func FormatUpdateMessage

func FormatUpdateMessage(current, latest string) string

FormatUpdateMessage creates a user-friendly message about an update.

func GetRecentReleases

func GetRecentReleases(ctx context.Context, limit int) ([]string, error)

GetRecentReleases fetches recent releases using the default checker.

func IsPrerelease

func IsPrerelease(version string) bool

IsPrerelease checks if a version string represents a prerelease.

func NowISO8601

func NowISO8601() string

NowISO8601 returns the current time in ISO8601 format.

func SaveStateToFile

func SaveStateToFile(path string, state *UpdateState) error

SaveStateToFile saves state to file directly (for testing).

Types

type Checker

type Checker interface {
	CheckLatestVersion(ctx context.Context) (*VersionInfo, error)
}

Checker is the interface for checking version information.

type GitHubChecker

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

GitHubChecker checks versions from GitHub releases.

func NewGitHubChecker

func NewGitHubChecker(repo string) *GitHubChecker

NewGitHubChecker creates a new GitHub checker. The repo should be in format "owner/repo" (e.g., "javinizer/Javinizer").

func NewGitHubCheckerWithBaseURL

func NewGitHubCheckerWithBaseURL(repo, baseURL string) *GitHubChecker

NewGitHubCheckerWithBaseURL creates a new GitHub checker with a custom base URL (for testing).

func (*GitHubChecker) CheckLatestVersion

func (c *GitHubChecker) CheckLatestVersion(ctx context.Context) (*VersionInfo, error)

CheckLatestVersion fetches the latest stable release from GitHub. If no stable release is found, it returns the latest release (which may be a prerelease).

type Service

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

Service handles update checking with caching and background refresh.

func NewService

func NewService(cfg *config.Config) *Service

NewService creates a new update service.

func (*Service) BackgroundCheck

func (s *Service) BackgroundCheck()

BackgroundCheck performs a non-blocking update check. Uses context.Background() to avoid cancellation when the HTTP request ends.

func (*Service) ForceCheck

func (s *Service) ForceCheck(ctx context.Context) (*UpdateState, error)

ForceCheck performs an immediate sync check and updates the cache.

func (*Service) GetLatestVersion

func (s *Service) GetLatestVersion(ctx context.Context) (string, error)

GetLatestVersion returns the latest version without checking availability.

func (*Service) GetStatus

func (s *Service) GetStatus(ctx context.Context) (*UpdateState, error)

GetStatus returns the current update status. If the cached state is stale, it performs a background check.

func (*Service) IsUpdateAvailable

func (s *Service) IsUpdateAvailable(ctx context.Context) (bool, error)

IsUpdateAvailable checks if an update is available without modifying state.

func (*Service) ShouldCheck

func (s *Service) ShouldCheck(state *UpdateState) bool

ShouldCheck determines if a check should be performed.

func (*Service) StartBackgroundCheck

func (s *Service) StartBackgroundCheck(ctx context.Context, interval time.Duration)

StartBackgroundCheck starts a background goroutine for periodic checks.

type StateStore

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

StateStore handles loading and saving update state.

func NewDefaultStateStore

func NewDefaultStateStore() *StateStore

NewDefaultStateStore creates a state store with default settings.

func NewStateStore

func NewStateStore(path string, interval time.Duration) *StateStore

NewStateStore creates a new state store with the given path and check interval.

func (*StateStore) ClearState

func (s *StateStore) ClearState() error

ClearState clears the cached state.

func (*StateStore) GetState

func (s *StateStore) GetState() *UpdateState

GetState returns the current state (thread-safe). Returns a copy to prevent race conditions.

func (*StateStore) LoadState

func (s *StateStore) LoadState() (*UpdateState, error)

LoadState loads the update state from file. Returns a copy of the state to prevent race conditions.

func (*StateStore) SaveState

func (s *StateStore) SaveState(state *UpdateState) error

SaveState saves the update state to file atomically.

func (*StateStore) SetState

func (s *StateStore) SetState(state *UpdateState)

SetState sets the state directly without file I/O (for testing).

func (*StateStore) ShouldCheck

func (s *StateStore) ShouldCheck() bool

ShouldCheck returns true if enough time has passed since last check.

type UpdateConfigSection

type UpdateConfigSection struct {
	Enabled                  bool `yaml:"enabled" json:"enabled"`
	UpdateCheckIntervalHours int  `yaml:"check_interval_hours" json:"check_interval_hours"`
}

UpdateConfigSection represents the update configuration section.

func DefaultUpdateConfig

func DefaultUpdateConfig() UpdateConfigSection

DefaultUpdateConfig returns the default update configuration.

type UpdateState

type UpdateState struct {
	Version    string `json:"version"`         // Latest version found
	CheckedAt  string `json:"checked_at"`      // ISO8601 timestamp
	Available  bool   `json:"available"`       // Whether update is available
	Prerelease bool   `json:"prerelease"`      // Whether latest is prerelease
	Source     string `json:"source"`          // "cached" or "fresh"
	Error      string `json:"error,omitempty"` // Last error message
}

UpdateState represents the cached update information.

func LoadStateFromFile

func LoadStateFromFile(path string) (*UpdateState, error)

LoadStateFromFile loads state from file directly (for testing).

type VersionInfo

type VersionInfo struct {
	Version     string `json:"version"`      // Human-readable version (e.g., "v1.6.0")
	TagName     string `json:"tag_name"`     // Git tag (e.g., "v1.6.0")
	Prerelease  bool   `json:"prerelease"`   // Whether this is a prerelease
	PublishedAt string `json:"published_at"` // ISO8601 timestamp
}

VersionInfo represents information about a GitHub release.

func CheckForUpdate

func CheckForUpdate(ctx context.Context, currentVersion string, checkPrerelease bool) (*VersionInfo, bool, error)

CheckForUpdate checks if an update is available and returns status. If checkPrerelease is true, prereleases will also be considered.

func CheckForUpdateWithChecker

func CheckForUpdateWithChecker(ctx context.Context, currentVersion string, checkPrerelease bool, checker *GitHubChecker) (*VersionInfo, bool, error)

CheckForUpdateWithChecker checks if an update is available using a custom checker (for testing).

func GetLatestStableVersion

func GetLatestStableVersion(ctx context.Context) (*VersionInfo, error)

GetLatestStableVersion checks GitHub and returns the latest stable version. Returns (version, isAvailable, error).

func ParseGitHubReleaseVersion

func ParseGitHubReleaseVersion(tagName string) (*VersionInfo, error)

ParseGitHubReleaseVersion extracts version information from a GitHub tag name.

Jump to

Keyboard shortcuts

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