Documentation
¶
Overview ¶
Package update provides CLI auto-update functionality.
Index ¶
- Variables
- func CompareVersions(v1, v2 string) (int, error)
- func IsChecksumError(err error) bool
- func IsNewer(currentVersion, newVersion string) (bool, error)
- func IsUpdateError(err error) bool
- func IsVersionError(err error) bool
- type Asset
- type Checker
- func (c *Checker) CheckForUpdate(ctx context.Context) (*ReleaseInfo, bool, error)
- func (c *Checker) FindAssetForPlatform(release *ReleaseInfo, platform Platform) (*Asset, error)
- func (c *Checker) FindChecksumAsset(release *ReleaseInfo) (*Asset, error)
- func (c *Checker) GetLatestRelease(ctx context.Context) (*ReleaseInfo, error)
- type CheckerOptions
- type ChecksumError
- type CommandExecutor
- type DefaultExecutor
- type DownloadResult
- type Downloader
- func (d *Downloader) Cleanup(result *DownloadResult) error
- func (d *Downloader) Download(ctx context.Context, asset *Asset) (*DownloadResult, error)
- func (d *Downloader) DownloadChecksums(ctx context.Context, asset *Asset) (map[string]string, error)
- func (d *Downloader) VerifyChecksum(filePath, expectedChecksum string) error
- type DownloaderOptions
- type HTTPClient
- type Installer
- type InstallerOptions
- type Manager
- func (m *Manager) CheckAndUpdate(ctx context.Context) (*UpdateResult, error)
- func (m *Manager) CheckOnly(ctx context.Context) (*ReleaseInfo, bool, error)
- func (m *Manager) GetCurrentVersion() string
- func (m *Manager) IsUpdateDisabled() bool
- func (m *Manager) Restart(ctx context.Context, args []string) error
- func (m *Manager) Rollback(ctx context.Context) error
- func (m *Manager) Update(ctx context.Context) (*UpdateResult, error)
- type ManagerOptions
- type Platform
- type ReleaseInfo
- type SemVer
- type UpdateError
- type UpdateResult
- type VersionError
Constants ¶
This section is empty.
Variables ¶
var ( ErrNoUpdateAvailable = errors.New("already at latest version") ErrAssetNotFound = errors.New("no compatible binary found for platform") ErrChecksumNotFound = errors.New("checksum file not found") ErrChecksumMismatch = errors.New("checksum verification failed") ErrDownloadFailed = errors.New("failed to download binary") ErrDownloadInterrupted = errors.New("download interrupted") ErrInstallFailed = errors.New("failed to install binary") ErrPermissionDenied = errors.New("permission denied") ErrRestartFailed = errors.New("failed to restart") ErrBinaryNotFound = errors.New("binary not found in archive") ErrNoReleasesFound = errors.New("no releases found") )
Predefined errors for common conditions.
Functions ¶
func CompareVersions ¶
CompareVersions compares two version strings. Returns: -1 if v1 < v2, 0 if equal, 1 if v1 > v2.
func IsChecksumError ¶
IsChecksumError checks if an error is a ChecksumError.
func IsUpdateError ¶
IsUpdateError checks if an error is an UpdateError.
func IsVersionError ¶
IsVersionError checks if an error is a VersionError.
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker queries GitHub for the latest release.
func NewChecker ¶
func NewChecker(opts *CheckerOptions) *Checker
NewChecker creates a new Checker with the given options.
func (*Checker) CheckForUpdate ¶
CheckForUpdate checks if a newer version is available.
func (*Checker) FindAssetForPlatform ¶
func (c *Checker) FindAssetForPlatform(release *ReleaseInfo, platform Platform) (*Asset, error)
FindAssetForPlatform finds the appropriate binary asset for the platform.
func (*Checker) FindChecksumAsset ¶
func (c *Checker) FindChecksumAsset(release *ReleaseInfo) (*Asset, error)
FindChecksumAsset finds the checksum file (SHA256SUMS or similar).
func (*Checker) GetLatestRelease ¶
func (c *Checker) GetLatestRelease(ctx context.Context) (*ReleaseInfo, error)
GetLatestRelease retrieves the latest release from GitHub.
type CheckerOptions ¶
type CheckerOptions struct {
RepoOwner string
RepoName string
CurrentVersion string
Executor CommandExecutor
Timeout time.Duration // Timeout for GitHub API queries (default: 10s)
}
CheckerOptions configures the update checker.
func DefaultCheckerOptions ¶
func DefaultCheckerOptions(currentVersion string) *CheckerOptions
DefaultCheckerOptions returns CheckerOptions with defaults.
type ChecksumError ¶
ChecksumError represents a checksum verification failure.
func (*ChecksumError) Error ¶
func (e *ChecksumError) Error() string
type CommandExecutor ¶
type CommandExecutor interface {
CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd
}
CommandExecutor abstracts exec.Command for testing.
type DefaultExecutor ¶
type DefaultExecutor struct{}
DefaultExecutor uses the real exec.CommandContext.
func (*DefaultExecutor) CommandContext ¶
func (e *DefaultExecutor) CommandContext(ctx context.Context, name string, args ...string) *exec.Cmd
CommandContext creates a new exec.Cmd with context.
type DownloadResult ¶
DownloadResult contains the result of a download operation.
type Downloader ¶
type Downloader struct {
// contains filtered or unexported fields
}
Downloader handles binary downloads and verification.
func NewDownloader ¶
func NewDownloader(opts *DownloaderOptions) *Downloader
NewDownloader creates a new Downloader with the given options.
func (*Downloader) Cleanup ¶
func (d *Downloader) Cleanup(result *DownloadResult) error
Cleanup removes downloaded files.
func (*Downloader) Download ¶
func (d *Downloader) Download(ctx context.Context, asset *Asset) (*DownloadResult, error)
Download downloads and extracts the binary from the given asset.
func (*Downloader) DownloadChecksums ¶
func (d *Downloader) DownloadChecksums(ctx context.Context, asset *Asset) (map[string]string, error)
DownloadChecksums downloads and parses the checksums file.
func (*Downloader) VerifyChecksum ¶
func (d *Downloader) VerifyChecksum(filePath, expectedChecksum string) error
VerifyChecksum verifies a file against a checksum.
type DownloaderOptions ¶
type DownloaderOptions struct {
TempDir string
HTTPClient HTTPClient
VerifyChecksum bool
OnProgress func(downloaded, total int64)
AllowInsecure bool // For testing only - allows HTTP URLs
}
DownloaderOptions configures the binary downloader.
func DefaultDownloaderOptions ¶
func DefaultDownloaderOptions() *DownloaderOptions
DefaultDownloaderOptions returns DownloaderOptions with defaults.
type HTTPClient ¶
HTTPClient abstracts HTTP operations for testing.
type Installer ¶
type Installer struct {
// contains filtered or unexported fields
}
Installer handles binary replacement and restart.
func NewInstaller ¶
func NewInstaller(opts *InstallerOptions) *Installer
NewInstaller creates a new Installer with the given options.
func (*Installer) GetCurrentBinaryPath ¶
GetCurrentBinaryPath returns the path to the currently running binary.
type InstallerOptions ¶
type InstallerOptions struct {
BinaryPath string
BackupSuffix string
Executor CommandExecutor
}
InstallerOptions configures the binary installer.
func DefaultInstallerOptions ¶
func DefaultInstallerOptions() *InstallerOptions
DefaultInstallerOptions returns InstallerOptions with defaults.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager orchestrates the update process.
func NewManager ¶
func NewManager(opts *ManagerOptions) *Manager
NewManager creates a new Manager with the given options.
func (*Manager) CheckAndUpdate ¶
func (m *Manager) CheckAndUpdate(ctx context.Context) (*UpdateResult, error)
CheckAndUpdate performs the full update flow.
func (*Manager) GetCurrentVersion ¶
GetCurrentVersion returns the current version being managed.
func (*Manager) IsUpdateDisabled ¶
IsUpdateDisabled returns whether updates are disabled.
type ManagerOptions ¶
type ManagerOptions struct {
AutoUpdate bool
DisableUpdates bool
CheckerOptions *CheckerOptions
DownloaderOptions *DownloaderOptions
InstallerOptions *InstallerOptions
OnPrompt func(current, latest string) bool
OnProgress func(status string)
}
ManagerOptions configures the update manager.
func DefaultManagerOptions ¶
func DefaultManagerOptions(currentVersion string) *ManagerOptions
DefaultManagerOptions returns ManagerOptions with defaults.
type Platform ¶
Platform represents the current OS/architecture.
func CurrentPlatform ¶
func CurrentPlatform() Platform
CurrentPlatform returns the current runtime platform.
type ReleaseInfo ¶
type ReleaseInfo struct {
Version string
TagName string
PublishedAt time.Time
Assets []Asset
HTMLURL string
Body string
}
ReleaseInfo contains information about a GitHub release.
type SemVer ¶
type SemVer struct {
Major int
Minor int
Patch int
Prerelease string // e.g., "beta.1", "rc.2"
Build string // e.g., "20240101"
}
SemVer represents a parsed semantic version.
func ParseVersion ¶
ParseVersion parses a semantic version string.
func (*SemVer) Compare ¶
Compare compares two versions. Returns: -1 if v < other, 0 if equal, 1 if v > other.
func (*SemVer) IsNewerThan ¶
IsNewerThan returns true if v is newer than other.
type UpdateError ¶
type UpdateError struct {
Operation string // Failed operation (e.g., "check", "download", "install")
Message string // Error message
Err error // Underlying error
}
UpdateError represents an update operation error.
func NewUpdateError ¶
func NewUpdateError(operation, message string, err error) *UpdateError
NewUpdateError creates a new UpdateError.
func (*UpdateError) Error ¶
func (e *UpdateError) Error() string
func (*UpdateError) Unwrap ¶
func (e *UpdateError) Unwrap() error
type UpdateResult ¶
type UpdateResult struct {
Updated bool
PreviousVersion string
NewVersion string
RestartRequired bool
Message string
}
UpdateResult represents the outcome of an update operation.
type VersionError ¶
VersionError represents a version comparison/parsing error.
func NewVersionError ¶
func NewVersionError(version, message string, err error) *VersionError
NewVersionError creates a new VersionError.
func (*VersionError) Error ¶
func (e *VersionError) Error() string
func (*VersionError) Unwrap ¶
func (e *VersionError) Unwrap() error