Documentation
¶
Overview ¶
Package update handles checking for and downloading application updates.
Index ¶
- Constants
- Variables
- func CopyAppToLocation(targetDir string) (string, error)
- func CreateDesktopShortcut(appPath string) error
- func DeleteApp(appPath string) error
- func GetInstaller() ([]byte, error)
- func GetTargetPath() (string, error)
- func GetUserApplicationsDir() (string, error)
- func InstallerFilename() string
- func IsInStandardLocation() bool
- func LaunchAppAndExit(appPath string) error
- func LaunchInstaller(stagingDir, targetPath string) error
- func RemoveDesktopShortcut() error
- func ValidateCleanupPath(appPath string) error
- type Checker
- func (c *Checker) Check() UpdateStatus
- func (c *Checker) CheckWithContext(ctx context.Context) UpdateStatus
- func (c *Checker) CurrentVersion() string
- func (c *Checker) ForceCheck() UpdateStatus
- func (c *Checker) ForceUpdate() UpdateStatus
- func (c *Checker) LastStatus() *UpdateStatus
- func (c *Checker) SetLogger(logger *slog.Logger)
- type DownloadProgress
- type Downloader
- func (d *Downloader) Download(url, destPath string) error
- func (d *Downloader) DownloadToStaging(url string, goos string, expectedChecksum string) (string, error)
- func (d *Downloader) SetProgressCallback(callback ProgressCallback)
- func (d *Downloader) VerifyChecksum(filePath, expectedHash string) error
- type ProgressCallback
- type ReleaseAsset
- type ReleaseInfo
- type UpdateStatus
Constants ¶
const ( // GitHubReleasesAPI is the endpoint for checking releases. // We use /releases instead of /releases/latest because /latest only returns // non-prerelease, non-draft releases. GitHubReleasesAPI = "https://api.github.com/repos/tinyrange/cc/releases" // ReleasesPageURL is the URL to the releases page for manual downloads. ReleasesPageURL = "https://github.com/tinyrange/cc/releases" // CheckInterval is how long to cache update check results. CheckInterval = 24 * time.Hour // CacheFilename is the name of the cache file. CacheFilename = "update_check.json" )
Variables ¶
var ErrAppLaunched = errors.New("app launched successfully, caller should exit")
ErrAppLaunched is returned when the app has been successfully launched after an update. The caller should exit with code 0.
var ErrInstallerLaunched = errors.New("installer launched successfully, caller should exit")
ErrInstallerLaunched is returned when the installer has been successfully launched. The caller should perform cleanup and exit with code 0.
Functions ¶
func CopyAppToLocation ¶
CopyAppToLocation copies the current app to the target directory. Returns the path to the copied app.
func CreateDesktopShortcut ¶
CreateDesktopShortcut creates a desktop shortcut/entry for the app. On Windows: Creates a .lnk file in the Start Menu Programs folder. On Linux: Creates a .desktop file in ~/.local/share/applications/. On macOS: No-op (apps in ~/Applications are already discoverable via Spotlight/Launchpad).
func GetInstaller ¶
GetInstaller returns an error when the installer is not embedded. Build with -tags embed_installer to embed the installer binary.
func GetTargetPath ¶
GetTargetPath returns the path to the current application that should be replaced. On macOS, this returns the .app bundle path. On other platforms, this returns the executable path.
func GetUserApplicationsDir ¶
GetUserApplicationsDir returns the user's Applications directory for the current platform.
func InstallerFilename ¶
func InstallerFilename() string
InstallerFilename returns the filename for the installer.
func IsInStandardLocation ¶
func IsInStandardLocation() bool
IsInStandardLocation returns true if the app is running from a standard install location.
func LaunchAppAndExit ¶
LaunchAppAndExit launches the app at the given path. Returns ErrAppLaunched on success - the caller should exit with code 0.
func LaunchInstaller ¶
LaunchInstaller extracts the embedded installer and launches it to perform the update. Returns ErrInstallerLaunched on success - the caller should perform cleanup and exit.
func RemoveDesktopShortcut ¶
func RemoveDesktopShortcut() error
RemoveDesktopShortcut removes the desktop shortcut/entry for the app.
func ValidateCleanupPath ¶
ValidateCleanupPath validates that a cleanup path is safe to delete. This provides defense-in-depth by validating before DeleteApp is called.
Types ¶
type Checker ¶
type Checker struct {
// contains filtered or unexported fields
}
Checker manages update checking with caching.
func NewChecker ¶
NewChecker creates a new update checker.
func (*Checker) Check ¶
func (c *Checker) Check() UpdateStatus
Check checks for updates, using cache if available and fresh.
func (*Checker) CheckWithContext ¶
func (c *Checker) CheckWithContext(ctx context.Context) UpdateStatus
CheckWithContext checks for updates with context for cancellation/timeout.
func (*Checker) CurrentVersion ¶
CurrentVersion returns the current application version.
func (*Checker) ForceCheck ¶
func (c *Checker) ForceCheck() UpdateStatus
ForceCheck bypasses the cache and always fetches from the API.
func (*Checker) ForceUpdate ¶
func (c *Checker) ForceUpdate() UpdateStatus
ForceUpdate returns an UpdateStatus that will trigger an update regardless of version. This is useful for testing the update flow.
func (*Checker) LastStatus ¶
func (c *Checker) LastStatus() *UpdateStatus
LastStatus returns the last known update status.
type DownloadProgress ¶
type DownloadProgress struct {
Current int64
Total int64
Status string
BytesPerSecond float64 // Current download speed in bytes per second
ETA time.Duration // Estimated time remaining (-1 if unknown)
}
DownloadProgress represents the current download progress.
type Downloader ¶
type Downloader struct {
// contains filtered or unexported fields
}
Downloader handles downloading update files.
func (*Downloader) Download ¶
func (d *Downloader) Download(url, destPath string) error
Download downloads a file from the given URL to the destination path.
func (*Downloader) DownloadToStaging ¶
func (d *Downloader) DownloadToStaging(url string, goos string, expectedChecksum string) (string, error)
DownloadToStaging downloads the update to a staging directory and returns the path. For macOS, it extracts the .app bundle from the zip. For other platforms, it downloads the binary directly. If expectedChecksum is provided (non-empty), the download is verified against it.
func (*Downloader) SetProgressCallback ¶
func (d *Downloader) SetProgressCallback(callback ProgressCallback)
SetProgressCallback sets the callback for progress updates.
func (*Downloader) VerifyChecksum ¶
func (d *Downloader) VerifyChecksum(filePath, expectedHash string) error
VerifyChecksum verifies the SHA256 checksum of a file.
type ProgressCallback ¶
type ProgressCallback func(progress DownloadProgress)
ProgressCallback is called during download with progress updates.
type ReleaseAsset ¶
type ReleaseAsset struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
Size int64 `json:"size"`
ContentType string `json:"content_type"`
}
ReleaseAsset represents a downloadable asset from a GitHub release.
type ReleaseInfo ¶
type ReleaseInfo struct {
TagName string `json:"tag_name"`
Name string `json:"name"`
HTMLURL string `json:"html_url"`
PublishedAt string `json:"published_at"`
Prerelease bool `json:"prerelease"`
Body string `json:"body"`
Assets []ReleaseAsset `json:"assets"`
}
ReleaseInfo represents a GitHub release.
type UpdateStatus ¶
type UpdateStatus struct {
Available bool
CurrentVersion string
LatestVersion string
ReleaseURL string
ReleaseNotes string
DownloadURL string
DownloadSize int64
Checksum string // SHA256 checksum (empty if not available)
ChecksumURL string // URL to checksums file (for fetching)
CheckedAt time.Time
Error error
}
UpdateStatus represents the result of an update check.