semver

package
v0.22.2 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package semver contains methods of working with semantic versions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedVersionFormat is returned is a version is of an unsupported
	// format (i.e. likely not semantic).
	ErrUnsupportedVersionFormat = errors.New("unsupported version format")
)
View Source
var PackedSingleDigitPatchDiff = PackInt64(&Version{Release: []int{1, 0, 1}}) - PackInt64(&Version{Release: []int{1, 0, 0}})

Functions

func CompareVersions

func CompareVersions(a *Version, b *Version) int

CompareVersions compares two compatible [Version]s. Returns a negative number when a < b, a positive number when a > b and zero when a == b. Useful to use as a sort func.

func LatestOpinionatedVersionString

func LatestOpinionatedVersionString(current string, versions []string, stayOnCurrentMajor bool) (string, bool)

LatestOpinionatedVersionString returns the latest compatible version and whether or not a compatible version was found. If there is a new version on the same major as the current, it is preferred. If the current version is the newest version of the major, the latest version is returned. This allows more graceful handling of version tracks for things like databases where multiple majors are supported concurrently.

func PackInt64

func PackInt64(version *Version) uint64

PackInt64 packs a Version into a lossy format which fits into a 64-bit int. The resulting int is sortable when compared to compatible versions. That is, a version of a higher major, higher minor, higher patch and so on will be a higher value than a lower version. Useful for creating a value which is then used to diff two versions of the same image. The resulting diff is sortable among with calculated values. Might fall apart if there are many parts, but most of the time, there are only three or four version parts. Note that it is valid to pass a nil Version, which will be treated as a unknown version.

Types

type Version

type Version struct {
	// Release holds the digits (likely major, minor, patch), as many as
	// available.
	Release []int
	// Suffix is the suffix of the version, if any, such as -alpine.
	Suffix string
	// Prerelease is the prerelease of the version, such as rc1.
	Prerelease string
	// contains filtered or unexported fields
}

Version is a semantic version.

func ParseVersion

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

ParseVersion parses a Version. Returns ErrUnsupportedVersionFormat if the version is not parsable.

func (*Version) Compare

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

Compare compares two versions. Returns a negative number when v < other, a positive number when v > other and zero when v == other.

func (*Version) Diff

func (v *Version) Diff(other *Version) string

Diff returns the type of bump that differs v to other. Returns an empty string in cases where a bump could not be found.

func (*Version) Equals

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

Equals returns whether or not the two versions are equal.

func (*Version) IsCompatible

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

IsCompatible returns true if v can be compared to other.

func (*Version) IsStable

func (v *Version) IsStable() bool

IsStable returns whether or not the version is a "stable" version without a pre-release.

Jump to

Keyboard shortcuts

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