update

package
v0.10.5 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package update provides version checking and self-update functionality for autospec.

The package includes:

  • Semantic version parsing and comparison (version.go)
  • GitHub API client for fetching release info (check.go)
  • Binary download with progress display (download.go)
  • Binary installation with backup and rollback (install.go)

The update check is designed to be non-blocking when used with the version command, using goroutines to fetch release info without delaying the display of version information. The update command handles the complete download, verification, and installation flow with checksum verification and atomic file operations.

Index

Constants

View Source
const (
	// GitHubAPIURL is the endpoint for fetching the latest release.
	GitHubAPIURL = "https://api.github.com/repos/ariel-frischer/autospec/releases/latest"

	// DefaultHTTPTimeout is the default timeout for HTTP requests.
	DefaultHTTPTimeout = 5 * time.Second
)

Variables

This section is empty.

Functions

func ExtractBinary

func ExtractBinary(archivePath, destDir string) (string, error)

ExtractBinary extracts the autospec binary from a tar.gz archive. Returns the path to the extracted binary.

func ParseChecksum

func ParseChecksum(content, assetName string) (string, error)

ParseChecksum extracts the checksum for a specific asset from checksums.txt format. Format: "<checksum> <filename>"

func VerifyChecksum

func VerifyChecksum(filePath, expected string) error

VerifyChecksum computes the SHA256 hash of a file and compares it to expected.

Types

type Asset

type Asset struct {
	Name               string `json:"name"`
	BrowserDownloadURL string `json:"browser_download_url"`
	Size               int64  `json:"size"`
}

Asset represents a single release asset.

type AsyncCheckResult

type AsyncCheckResult struct {
	Check *UpdateCheck
	Error error
}

AsyncCheckResult wraps an update check result for async operations.

type Checker

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

Checker provides update checking functionality.

func NewChecker

func NewChecker(timeout time.Duration) *Checker

NewChecker creates a new update checker with the given timeout.

func (*Checker) CheckForUpdate

func (c *Checker) CheckForUpdate(ctx context.Context, currentVersion string) (*UpdateCheck, error)

CheckForUpdate checks GitHub for a newer version of autospec.

func (*Checker) CheckForUpdateAsync

func (c *Checker) CheckForUpdateAsync(ctx context.Context, currentVersion string) <-chan AsyncCheckResult

CheckForUpdateAsync starts an update check in a goroutine and returns a channel for the result. The result is sent on the channel when the check completes or fails. The channel is closed after the result is sent.

func (*Checker) SetAPIURL

func (c *Checker) SetAPIURL(url string)

SetAPIURL sets the API URL for the checker. This is intended for testing purposes.

type Downloader

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

Downloader handles downloading and verifying release binaries.

func NewDownloader

func NewDownloader(client *http.Client) *Downloader

NewDownloader creates a new downloader with the given HTTP client.

func (*Downloader) DownloadBinary

func (d *Downloader) DownloadBinary(ctx context.Context, url string, onProgress func(current, total int64)) (string, error)

DownloadBinary downloads a binary archive from the given URL to a temp file. Returns the path to the downloaded file.

func (*Downloader) FetchChecksum

func (d *Downloader) FetchChecksum(ctx context.Context, checksumURL, assetName string) (string, error)

FetchChecksum downloads the checksums.txt file and returns the checksum for the given asset.

type Installer

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

Installer handles binary installation with backup and rollback.

func NewInstaller

func NewInstaller() (*Installer, error)

NewInstaller creates a new installer for the current executable.

func (*Installer) CheckWritePermission

func (i *Installer) CheckWritePermission() error

CheckWritePermission checks if we have write access to the executable location.

func (*Installer) CleanupBackup

func (i *Installer) CleanupBackup() error

CleanupBackup removes the backup file after successful installation.

func (*Installer) CreateBackup

func (i *Installer) CreateBackup() error

CreateBackup renames the current binary to .bak extension.

func (*Installer) GetBackupPath

func (i *Installer) GetBackupPath() string

GetBackupPath returns the path where the backup will be stored.

func (*Installer) GetExecutablePath

func (i *Installer) GetExecutablePath() string

GetExecutablePath returns the path to the current executable.

func (*Installer) InstallBinary

func (i *Installer) InstallBinary(newBinaryPath string) error

InstallBinary moves a new binary into place. Uses rename for same-filesystem moves, falls back to copy for cross-device.

func (*Installer) Rollback

func (i *Installer) Rollback() error

Rollback restores the backup if something goes wrong.

func (*Installer) SetPermissions

func (i *Installer) SetPermissions() error

SetPermissions ensures the binary is executable.

type ProgressWriter

type ProgressWriter struct {
	Writer   io.Writer
	Total    int64
	Current  int64
	OnUpdate func(current, total int64)
}

ProgressWriter wraps an io.Writer to report download progress.

func (*ProgressWriter) Write

func (pw *ProgressWriter) Write(p []byte) (int, error)

Write implements io.Writer and reports progress.

type ReleaseInfo

type ReleaseInfo struct {
	TagName     string    `json:"tag_name"`
	PublishedAt time.Time `json:"published_at"`
	Assets      []Asset   `json:"assets"`
}

ReleaseInfo represents a GitHub release.

type UpdateCheck

type UpdateCheck struct {
	CurrentVersion  string
	LatestVersion   string
	UpdateAvailable bool
	DownloadURL     string
	ChecksumURL     string
	AssetName       string
}

UpdateCheck contains the result of an update check.

type Version

type Version struct {
	Major int
	Minor int
	Patch int
	Raw   string
}

Version represents a parsed semantic version.

func ParseVersion

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

ParseVersion parses a version string in the format "v0.6.1" or "0.6.1". Returns an error if the version string is invalid. The "dev" version is a special case that returns a zero version.

func (*Version) Compare

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

Compare compares two versions and returns:

  • -1 if v < other
  • 0 if v == other
  • 1 if v > other

Dev versions are always considered less than any proper version.

func (*Version) IsDev

func (v *Version) IsDev() bool

IsDev returns true if this is a development build (not a proper release version).

func (*Version) IsNewerThan

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

IsNewerThan returns true if v is newer than other.

func (*Version) String

func (v *Version) String() string

String returns the version string in "vMAJOR.MINOR.PATCH" format.

Jump to

Keyboard shortcuts

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