version

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package version provides comprehensive version comparison for multiple ecosystems. It supports semver (npm, Go, Cargo), PEP 440 (Python), Debian, RPM, Alpine, and Maven.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare

func Compare(v1, v2 string, format Format) (int, error)

Compare compares two version strings using the specified format. Returns -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2. Returns error if versions cannot be compared.

func CompareAlpine

func CompareAlpine(v1, v2 string) (int, error)

CompareAlpine compares two Alpine versions. Format: version-rN where N is the revision number.

func CompareDebian

func CompareDebian(v1, v2 string) (int, error)

CompareDebian compares two Debian versions. Format: [epoch:]upstream_version[-debian_revision] Comparison order: epoch (numeric) > upstream > revision Uses Debian string comparison algorithm where ~ sorts before empty.

func CompareGeneric

func CompareGeneric(v1, v2 string) (int, error)

CompareGeneric performs a loose version comparison that works for most formats. It splits versions on common delimiters and compares segments numerically when possible.

func CompareMaven

func CompareMaven(v1, v2 string) (int, error)

CompareMaven compares two Maven versions. Qualifier ordering: SNAPSHOT < alpha < beta < milestone < rc < RELEASE/FINAL/GA < (none) < sp

func ComparePEP440

func ComparePEP440(v1, v2 string) (int, error)

ComparePEP440 compares two PEP 440 Python versions. Ordering: dev < alpha < beta < rc < release < post Handles epochs (1!1.0.0) and strips local versions for comparison.

func CompareRPM

func CompareRPM(v1, v2 string) (int, error)

CompareRPM compares two RPM versions. Format: [epoch:]version[-release] Comparison order: epoch (numeric) > version > release

func CompareSemver

func CompareSemver(v1, v2 string) (int, error)

CompareSemver compares two semantic versions. Handles v prefix, prerelease ordering (alpha < beta < rc < release), and ignores build metadata in comparison.

func InRange

func InRange(version, introduced, fixed string, format Format) (bool, error)

InRange checks if a version is within an affected range. The range is [introduced, fixed) - introduced is inclusive, fixed is exclusive.

func IsDistroVersion

func IsDistroVersion(v string) bool

IsDistroVersion returns true if the version appears to be from a distribution package.

func IsFixed

func IsFixed(version, fixVersion string, format Format) (bool, error)

IsFixed checks if a version is at or above the fix version.

func MustCompare

func MustCompare(v1, v2 string, format Format) int

MustCompare is like Compare but returns 0 on error.

func ParseSemverConstraint

func ParseSemverConstraint(constraint string) (*semver.Constraints, error)

ParseSemverConstraint parses a semver constraint string. Supports: =, !=, >, <, >=, <=, ~, ^, and ranges.

func SafeCompare

func SafeCompare(v1, v2 string, format Format) (int, error)

SafeCompare compares versions with format validation. Returns an error if versions are not comparable (different ecosystems).

func SemverSatisfies

func SemverSatisfies(version, constraint string) (bool, error)

SemverSatisfies checks if a version satisfies a constraint.

func VersionsComparable

func VersionsComparable(v1, v2, ecosystem string) bool

VersionsComparable checks if two versions can be meaningfully compared. Returns false if one is a distro-packaged version and the other is upstream.

Types

type AlpineVersion

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

AlpineVersion represents a parsed Alpine version.

func NewAlpineVersion

func NewAlpineVersion(v string) *AlpineVersion

NewAlpineVersion creates a new AlpineVersion from a version string.

func (*AlpineVersion) Compare

func (a *AlpineVersion) Compare(other Version) int

Compare implements Version.Compare.

func (*AlpineVersion) Format

func (a *AlpineVersion) Format() Format

Format implements Version.Format.

func (*AlpineVersion) String

func (a *AlpineVersion) String() string

String implements Version.String.

type CompareResult

type CompareResult int

CompareResult represents the result of a version comparison.

const (
	CompareLess    CompareResult = -1
	CompareEqual   CompareResult = 0
	CompareGreater CompareResult = 1
	CompareError   CompareResult = -2 // Versions not comparable
)

type DebianVersion

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

DebianVersion represents a parsed Debian version.

func NewDebianVersion

func NewDebianVersion(v string) *DebianVersion

NewDebianVersion creates a new DebianVersion from a version string.

func (*DebianVersion) Compare

func (d *DebianVersion) Compare(other Version) int

Compare implements Version.Compare.

func (*DebianVersion) Format

func (d *DebianVersion) Format() Format

Format implements Version.Format.

func (*DebianVersion) String

func (d *DebianVersion) String() string

String implements Version.String.

type Format

type Format int

Format represents a version format/ecosystem.

const (
	UnknownFormat Format = iota
	SemverFormat         // npm, Go, Cargo, Rust
	PEP440Format         // Python/PyPI
	DebFormat            // Debian/Ubuntu
	RpmFormat            // RHEL, CentOS, Fedora, Amazon Linux
	ApkFormat            // Alpine Linux
	MavenFormat          // Java/Maven
	GenericFormat        // Fallback loose comparison
)

func DetectFormat

func DetectFormat(version string, hint string) Format

DetectFormat attempts to detect the version format from a version string. The hint parameter can provide additional context (e.g., ecosystem name).

func FormatForEcosystem

func FormatForEcosystem(ecosystem string) Format

FormatForEcosystem returns the expected version format for an ecosystem name.

func ParseFormat

func ParseFormat(s string) Format

ParseFormat parses a format string into a Format.

func (Format) String

func (f Format) String() string

String returns the string representation of the format.

type GenericVersion

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

GenericVersion represents a generic version string.

func NewGenericVersion

func NewGenericVersion(v string) *GenericVersion

NewGenericVersion creates a new GenericVersion from a version string.

func (*GenericVersion) Compare

func (g *GenericVersion) Compare(other Version) int

Compare implements Version.Compare.

func (*GenericVersion) Format

func (g *GenericVersion) Format() Format

Format implements Version.Format.

func (*GenericVersion) String

func (g *GenericVersion) String() string

String implements Version.String.

type MavenVersion

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

MavenVersion represents a parsed Maven version.

func NewMavenVersion

func NewMavenVersion(v string) *MavenVersion

NewMavenVersion creates a new MavenVersion from a version string.

func (*MavenVersion) Compare

func (m *MavenVersion) Compare(other Version) int

Compare implements Version.Compare.

func (*MavenVersion) Format

func (m *MavenVersion) Format() Format

Format implements Version.Format.

func (*MavenVersion) String

func (m *MavenVersion) String() string

String implements Version.String.

type PEP440Version

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

PEP440Version wraps a pep440.Version for the Version interface.

func NewPEP440Version

func NewPEP440Version(v string) (*PEP440Version, error)

NewPEP440Version creates a new PEP440Version from a version string.

func (*PEP440Version) Compare

func (p *PEP440Version) Compare(other Version) int

Compare implements Version.Compare.

func (*PEP440Version) Format

func (p *PEP440Version) Format() Format

Format implements Version.Format.

func (*PEP440Version) String

func (p *PEP440Version) String() string

String implements Version.String.

type RPMVersion

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

RPMVersion represents a parsed RPM version.

func NewRPMVersion

func NewRPMVersion(v string) *RPMVersion

NewRPMVersion creates a new RPMVersion from a version string.

func (*RPMVersion) Compare

func (r *RPMVersion) Compare(other Version) int

Compare implements Version.Compare.

func (*RPMVersion) Format

func (r *RPMVersion) Format() Format

Format implements Version.Format.

func (*RPMVersion) String

func (r *RPMVersion) String() string

String implements Version.String.

type SemVer

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

SemVer wraps a semver.Version for the Version interface.

func NewSemVer

func NewSemVer(v string) (*SemVer, error)

NewSemVer creates a new SemVer from a version string.

func (*SemVer) Compare

func (s *SemVer) Compare(other Version) int

Compare implements Version.Compare.

func (*SemVer) Format

func (s *SemVer) Format() Format

Format implements Version.Format.

func (*SemVer) Major

func (s *SemVer) Major() uint64

Major returns the major version number.

func (*SemVer) Minor

func (s *SemVer) Minor() uint64

Minor returns the minor version number.

func (*SemVer) Patch

func (s *SemVer) Patch() uint64

Patch returns the patch version number.

func (*SemVer) Prerelease

func (s *SemVer) Prerelease() string

Prerelease returns the prerelease string.

func (*SemVer) String

func (s *SemVer) String() string

String implements Version.String.

type Version

type Version interface {
	// Compare returns -1 if v < other, 0 if v == other, 1 if v > other.
	Compare(other Version) int
	// String returns the original version string.
	String() string
	// Format returns the version format.
	Format() Format
}

Version represents a parsed version that can be compared.

func ParseVersion

func ParseVersion(v string, format Format) (Version, error)

ParseVersion parses a version string into a Version interface. If format is UnknownFormat, auto-detection is attempted.

type VulnerabilityStatus

type VulnerabilityStatus int

VulnerabilityStatus represents the vulnerability status of a version.

const (
	StatusUnknown     VulnerabilityStatus = iota
	StatusNotAffected                     // Version predates vulnerability or is past last_affected
	StatusVulnerable                      // Version is in affected range
	StatusFixed                           // Version >= fix version
)

func CheckVulnerabilityStatus

func CheckVulnerabilityStatus(current, introduced, fixed, lastAffected string, format Format) (VulnerabilityStatus, string)

CheckVulnerabilityStatus determines if a version is affected by a vulnerability.

Parameters:

  • current: The version to check
  • introduced: Version where vulnerability was introduced (empty = from beginning)
  • fixed: Version where vulnerability was fixed (empty = no fix available)
  • lastAffected: Last version affected (empty = use fixed for upper bound)
  • format: The version format to use for comparison

Returns the vulnerability status and a human-readable explanation.

func (VulnerabilityStatus) String

func (s VulnerabilityStatus) String() string

String returns the string representation of the status.

Jump to

Keyboard shortcuts

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