update

package
v0.0.0-...-2b3c1a1 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package update provides CLI auto-update functionality.

Index

Constants

This section is empty.

Variables

View Source
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

func CompareVersions(v1, v2 string) (int, error)

CompareVersions compares two version strings. Returns: -1 if v1 < v2, 0 if equal, 1 if v1 > v2.

func IsChecksumError

func IsChecksumError(err error) bool

IsChecksumError checks if an error is a ChecksumError.

func IsNewer

func IsNewer(currentVersion, newVersion string) (bool, error)

IsNewer returns true if newVersion is newer than currentVersion.

func IsUpdateError

func IsUpdateError(err error) bool

IsUpdateError checks if an error is an UpdateError.

func IsVersionError

func IsVersionError(err error) bool

IsVersionError checks if an error is a VersionError.

Types

type Asset

type Asset struct {
	Name        string
	DownloadURL string
	Size        int64
	ContentType string
}

Asset represents a downloadable binary from a release.

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

func (c *Checker) CheckForUpdate(ctx context.Context) (*ReleaseInfo, bool, error)

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

type ChecksumError struct {
	Expected string
	Actual   string
	File     string
}

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

type DownloadResult struct {
	FilePath string
	Checksum string
	Size     int64
}

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

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

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

func (i *Installer) GetCurrentBinaryPath() (string, error)

GetCurrentBinaryPath returns the path to the currently running binary.

func (*Installer) Install

func (i *Installer) Install(ctx context.Context, newBinaryPath string) error

Install replaces the current binary with the new one.

func (*Installer) Restart

func (i *Installer) Restart(ctx context.Context, args []string) error

Restart restarts the application with the same arguments.

func (*Installer) Rollback

func (i *Installer) Rollback(ctx context.Context) error

Rollback restores the backup 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) CheckOnly

func (m *Manager) CheckOnly(ctx context.Context) (*ReleaseInfo, bool, error)

CheckOnly checks for updates without installing.

func (*Manager) GetCurrentVersion

func (m *Manager) GetCurrentVersion() string

GetCurrentVersion returns the current version being managed.

func (*Manager) IsUpdateDisabled

func (m *Manager) IsUpdateDisabled() bool

IsUpdateDisabled returns whether updates are disabled.

func (*Manager) Restart

func (m *Manager) Restart(ctx context.Context, args []string) error

Restart restarts the application.

func (*Manager) Rollback

func (m *Manager) Rollback(ctx context.Context) error

Rollback restores the previous version from backup.

func (*Manager) Update

func (m *Manager) Update(ctx context.Context) (*UpdateResult, error)

Update performs an immediate update without prompting.

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

type Platform struct {
	OS   string
	Arch string
}

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

func ParseVersion(v string) (*SemVer, error)

ParseVersion parses a semantic version string.

func (*SemVer) Compare

func (v *SemVer) Compare(other *SemVer) int

Compare compares two versions. Returns: -1 if v < other, 0 if equal, 1 if v > other.

func (*SemVer) IsNewerThan

func (v *SemVer) IsNewerThan(other *SemVer) bool

IsNewerThan returns true if v is newer than other.

func (*SemVer) String

func (v *SemVer) String() string

String returns the version as a string.

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

type VersionError struct {
	Version string
	Message string
	Err     error
}

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

Jump to

Keyboard shortcuts

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