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
- func ExtractBinary(archivePath, destDir string) (string, error)
- func ParseChecksum(content, assetName string) (string, error)
- func VerifyChecksum(filePath, expected string) error
- type Asset
- type AsyncCheckResult
- type Checker
- type Downloader
- type Installer
- func (i *Installer) CheckWritePermission() error
- func (i *Installer) CleanupBackup() error
- func (i *Installer) CreateBackup() error
- func (i *Installer) GetBackupPath() string
- func (i *Installer) GetExecutablePath() string
- func (i *Installer) InstallBinary(newBinaryPath string) error
- func (i *Installer) Rollback() error
- func (i *Installer) SetPermissions() error
- type ProgressWriter
- type ReleaseInfo
- type UpdateCheck
- type Version
Constants ¶
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 ¶
ExtractBinary extracts the autospec binary from a tar.gz archive. Returns the path to the extracted binary.
func ParseChecksum ¶
ParseChecksum extracts the checksum for a specific asset from checksums.txt format. Format: "<checksum> <filename>"
func VerifyChecksum ¶
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 ¶
NewChecker creates a new update checker with the given timeout.
func (*Checker) CheckForUpdate ¶
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.
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 ¶
NewInstaller creates a new installer for the current executable.
func (*Installer) CheckWritePermission ¶
CheckWritePermission checks if we have write access to the executable location.
func (*Installer) CleanupBackup ¶
CleanupBackup removes the backup file after successful installation.
func (*Installer) CreateBackup ¶
CreateBackup renames the current binary to .bak extension.
func (*Installer) GetBackupPath ¶
GetBackupPath returns the path where the backup will be stored.
func (*Installer) GetExecutablePath ¶
GetExecutablePath returns the path to the current executable.
func (*Installer) InstallBinary ¶
InstallBinary moves a new binary into place. Uses rename for same-filesystem moves, falls back to copy for cross-device.
func (*Installer) SetPermissions ¶
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.
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 ¶
Version represents a parsed semantic version.
func ParseVersion ¶
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 ¶
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 ¶
IsDev returns true if this is a development build (not a proper release version).
func (*Version) IsNewerThan ¶
IsNewerThan returns true if v is newer than other.