version

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package version exposes the build-time version metadata of the drift binary and the self-upgrade workflow.

The Version, Commit and BuildDate variables are intended to be set via -ldflags at build time:

go build -ldflags "\
  -X github.com/Alei-001/drift/internal/version.Version=v0.1.0 \
  -X github.com/Alei-001/drift/internal/version.Commit=$(git rev-parse --short HEAD) \
  -X github.com/Alei-001/drift/internal/version.BuildDate=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
  ./cmd/drift

When unset (e.g. a plain `go build` or `go install` without ldflags), Info falls back to runtime/debug.ReadBuildInfo, which for `go install`-built binaries carries the module version and VCS revision.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNetwork          = errors.New("network error")
	ErrNoRelease        = errors.New("no release available")
	ErrNoAsset          = errors.New("no matching release asset")
	ErrChecksumMismatch = errors.New("checksum verification failed")
)

ErrNetwork, ErrNoRelease, ErrNoAsset, ErrChecksumMismatch are sentinel-like errors returned by Upgrade. The CLI inspects them with errors.Is to tailor the user hint. ErrChecksumMismatch is wrapped by the checksum verification path both when the published checksum does not match the downloaded asset and when the checksums file is malformed and cannot be parsed — both cases mean verification failed and the upgrade must be aborted (fail-closed).

View Source
var (
	Version   = "(devel)"
	Commit    = "unknown"
	BuildDate = "unknown"
)

Build-time variables. Overridable via -ldflags "-X ...=value".

Functions

func CompareVersions

func CompareVersions(a, b string) (int, error)

CompareVersions compares two version strings. Returns -1, 0, 1 if a is less than, equal to, or greater than b. Returns an error wrapping errInvalidVersion if either string is not a recognizable semver.

func IsNewer

func IsNewer(candidate, current string) (bool, error)

IsNewer reports whether candidate is strictly newer than current. A development version ("(devel)") for current is treated as older than any real release, so `drift upgrade` can move from a dev build to a release.

Types

type Asset

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

Asset describes a single downloadable file attached to a release.

type Info

type Info struct {
	Version   string `json:"version"`
	Commit    string `json:"commit"`
	Built     string `json:"built"`
	GoVersion string `json:"go_version"`
	OS        string `json:"os"`
	Arch      string `json:"arch"`
}

Info describes the running binary.

func GetInfo

func GetInfo() Info

GetInfo returns the version metadata of the running binary, enriched with the toolchain and platform. When the ldflags-injected Version is the default "(devel)" placeholder, it is replaced with the module version reported by runtime/debug.ReadBuildInfo (if any) so that `go install`-built binaries still report a meaningful version.

func (Info) IsDevel

func (i Info) IsDevel() bool

IsDevel reports whether the binary is an unreleased development build. Only the literal "(devel)" placeholder or a Go pseudo-version of the form "v0.0.0-<timestamp>-<hash>" (emitted by `go install` of a non-tagged commit) is treated as development. A real release tagged "v0.0.0" is NOT a development build.

func (Info) Platform

func (i Info) Platform() string

Platform returns the "os/arch" string used to match release assets.

func (Info) String

func (i Info) String() string

String returns a single-line human-readable summary, e.g. "drift v0.1.0 (commit: a1b2c3d, built: 2026-07-08T12:00:00Z)".

type Release

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

Release describes the subset of GitHub release metadata drift consumes.

type Result

type Result struct {
	FromVersion string
	ToVersion   string
	Upgraded    bool   // true when the binary was actually replaced
	Message     string // human-readable detail
}

Result summarizes the outcome of an Upgrade call.

func Upgrade

func Upgrade(ctx context.Context, currentVersion string, opt UpgradeOptions) (Result, error)

Upgrade checks for a newer drift release on GitHub and, when available, downloads the matching binary and atomically replaces the running executable. Set Check=true to only report the latest version without modifying anything. Set Force=true to reinstall the same version.

On success Result.Upgraded is true when the binary was replaced, false when already up to date (or Check-only). A non-nil error is returned for network, release-matching, download, checksum, extraction, or replacement failures; the CLI maps these to user-facing hints via errors.Is.

type UpgradeOptions

type UpgradeOptions struct {
	Check          bool      // only check for a newer release, do not replace the binary
	Force          bool      // replace even when already up to date
	APIURL         string    // override GitHub API base (for tests); defaults to https://api.github.com
	ProgressWriter io.Writer // optional progress bar output (nil = no progress)
}

UpgradeOptions controls the behaviour of Upgrade.

Jump to

Keyboard shortcuts

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