updater

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCacheTTL = 24 * time.Hour

DefaultCacheTTL is the default time between update checks.

Variables

This section is empty.

Functions

func AtomicReplace

func AtomicReplace(newBinary, currentBinary string) error

AtomicReplace replaces the binary at currentBinary with the new binary at newBinary. It writes to a temp file in the same directory and renames, which is atomic on Linux and macOS when on the same filesystem.

The temp file uses a random, exclusive name (os.CreateTemp → O_CREAT|O_EXCL), so an attacker who can write to the directory cannot pre-plant it as a symlink and have us write the new binary through that symlink onto an arbitrary file.

func CacheDir

func CacheDir() string

CacheDir returns the default cache directory for vmflow. Priority: XDG_CACHE_HOME/vmflow > ~/.cache/vmflow

func CacheFilePath added in v0.3.0

func CacheFilePath() string

CacheFilePath returns the path to vmflow's default update-check cache file. Cleanup callers should remove this file rather than recursively deleting the parent cache directory, which may be shared with other applications.

func ExtractBinaryFromTarGz

func ExtractBinaryFromTarGz(archivePath, binaryName, destDir string) (string, error)

ExtractBinaryFromTarGz extracts the binary named binaryName from a .tar.gz archive, writing it to destDir and returning the path to the extracted file.

func SelfPath

func SelfPath() (string, error)

SelfPath returns the absolute path of the currently running executable, resolving any symlinks.

func SelfUpdateSupported added in v0.3.0

func SelfUpdateSupported() bool

SelfUpdateSupported reports whether the running platform can replace its own executable in place.

func ShouldCheck

func ShouldCheck(cf CacheFile, ttl time.Duration) bool

ShouldCheck returns true if the cache is expired (past TTL) or missing.

func VerifySHA256

func VerifySHA256(filePath, expectedHex string) (bool, error)

VerifySHA256 verifies that the file at filePath matches the expected hex checksum.

func WriteCache

func WriteCache(cf CacheFile) error

WriteCache writes the cache file to disk, creating the directory if needed.

func WriteCacheAt

func WriteCacheAt(dir string, cf CacheFile) error

WriteCacheAt writes the cache file to a specific directory. Empty dir falls back to CacheDir().

Types

type Asset

type Asset struct {
	Name string `json:"name"`
	URL  string `json:"url"`
	Size int64  `json:"size"`
}

Asset represents a single release asset.

type CacheFile

type CacheFile struct {
	LastCheck     time.Time `json:"last_check"`
	LatestVersion string    `json:"latest_version"`
}

CacheFile represents the on-disk update check cache.

func ReadCache

func ReadCache() (CacheFile, error)

ReadCache reads the cache file from disk. Returns zero-value CacheFile if not found.

func ReadCacheAt

func ReadCacheAt(dir string) (CacheFile, error)

ReadCacheAt reads the cache file from a specific directory. Empty dir falls back to CacheDir().

type CheckResult

type CheckResult struct {
	CurrentVersion  string
	LatestVersion   string
	UpdateAvailable bool
	Release         *Release
}

CheckResult is returned by check operations.

type Config

type Config struct {
	Repo        string
	BinaryName  string
	CurrentVer  string
	GitHubToken string
	CacheDir    string
	CacheTTL    time.Duration
	HTTPClient  *http.Client
}

Config holds configuration for the updater.

type Release

type Release struct {
	TagName string  `json:"tag_name"`
	Assets  []Asset `json:"assets"`
}

Release represents a GitHub release.

type Updater

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

Updater handles checking, downloading, and applying updates.

func New

func New(cfg Config) *Updater

New creates a new Updater with the given config.

func (*Updater) CheckForUpdate

func (u *Updater) CheckForUpdate(ctx context.Context) (*CheckResult, error)

CheckForUpdate queries the GitHub Releases API and compares versions. It uses the cache to avoid redundant API calls within CacheTTL.

func (*Updater) CheckSpecificVersion

func (u *Updater) CheckSpecificVersion(ctx context.Context, tag string) (*CheckResult, error)

CheckSpecificVersion checks for a specific version tag.

func (*Updater) DownloadAndInstall

func (u *Updater) DownloadAndInstall(ctx context.Context, release *Release, progress io.Writer) error

DownloadAndInstall downloads the archive for the current OS/arch, verifies the SHA-256 checksum, and atomically replaces the binary.

Jump to

Keyboard shortcuts

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