update

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package update handles checking for and downloading application updates.

Index

Constants

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

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

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

func CopyAppToLocation(targetDir string) (string, error)

CopyAppToLocation copies the current app to the target directory. Returns the path to the copied app.

func CreateDesktopShortcut

func CreateDesktopShortcut(appPath string) error

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 DeleteApp

func DeleteApp(appPath string) error

DeleteApp removes the app at the specified path with safety checks.

func GetInstaller

func GetInstaller() ([]byte, error)

GetInstaller returns an error when the installer is not embedded. Build with -tags embed_installer to embed the installer binary.

func GetTargetPath

func GetTargetPath() (string, error)

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

func GetUserApplicationsDir() (string, error)

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

func LaunchAppAndExit(appPath string) error

LaunchAppAndExit launches the app at the given path. Returns ErrAppLaunched on success - the caller should exit with code 0.

func LaunchInstaller

func LaunchInstaller(stagingDir, targetPath string) error

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

func ValidateCleanupPath(appPath string) error

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

func NewChecker(currentVersion, cacheDir string) *Checker

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

func (c *Checker) CurrentVersion() string

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.

func (*Checker) SetLogger

func (c *Checker) SetLogger(logger *slog.Logger)

SetLogger sets the logger for the checker.

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 NewDownloader

func NewDownloader() *Downloader

NewDownloader creates a new downloader.

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.

Jump to

Keyboard shortcuts

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