semver

package module
v1.0.1 Latest Latest
Warning

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

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

README

Go Reference

semver

Yet another Go library and CLI for parsing and modifying semver- compatible strings.

Dear God, why?

WHY NOT? The libraries out there didn't meet my precise use case and I wanted to learn more about the intricacies of the standard, so I did this in an afternoon.

Using

go install github.com/jghiloni/semver/semver-cli@latest

Contributing

You can if you want to. Make sure to follow the code of conduct.

License

This is licensed under the Apache 2 License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotNumeric is returned when a given field is expected to be a number.
	// Numbers cannot be zero-padded in semver.
	ErrNotNumeric = errors.New("field must be numeric and not zero-padded")

	// ErrUnparseable is returned when parsing a semver that is not valid
	ErrUnparseable = errors.New("could not parse string as semantic version")
)

Functions

This section is empty.

Types

type Version

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

Version represents a semantic version compliant with the v2.0.0 standard specified at https://semver.org

func ParseStrict

func ParseStrict(versionString string) (*Version, error)

ParseStrict parses a string into a semantic version that strictly follows the standard, and validates it

func ParseTolerant

func ParseTolerant(versionString string) (*Version, error)

ParseTolerant allows more leeway in parsing -- it allows an optional "v" at the beginning of the string, and it allows minor or patch to be missing, to be replaced with zeroes. HOWEVER, if patch is present, then minor must be present as well. It validates the version before returning

func (*Version) BuildMetadata

func (v *Version) BuildMetadata() []string

func (*Version) BumpMajor

func (v *Version) BumpMajor() error

BumpMajor attempts to increment the major field of the version by 1. If it's successful, it resets the minor and patch fields to 0 and unsets the prerelease and build metadata fields. It only errors if major is not parsable by (*math/big.Int).SetString, which should not occur.

func (*Version) BumpMinor

func (v *Version) BumpMinor() error

BumpMinor attempts to increment the minor field of the version by 1. If it's successful, it resets the patch field to 0 and unsets the prerelease and build metadata fields. It only errors if minor is not parsable by (*math/big.Int).SetString, which should not occur.

func (*Version) BumpPatch

func (v *Version) BumpPatch() error

BumpPatch attempts to increment the patch field of the version by 1. If it's successful, it unsets the prerelease and build metadata fields. It only errors if major is not parsable by (*math/big.Int).SetString, which should not occur.

func (*Version) BumpPrerelease

func (v *Version) BumpPrerelease() error

BumpPrerelease inspects the version's prerelease information, and if the last field is a number, increment it. If it is not, append ".1" to the end of the prerelease information. It returns an error if prerelease is empty.

func (*Version) Clone

func (v *Version) Clone() *Version

func (*Version) Compare

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

Compare compares two semantic versions based on the rules set out in the standard. If v is smaller than v2, Compare returns a value < 0. If v is larger than v2, Compare returns a value > 0. In both cases, the actual value returned is variable, but will conform to that signature. If the two versions are equivalent according to the standard, Compare returns 0.

func (*Version) Prerelease

func (v *Version) Prerelease() []string

func (*Version) SetBuildMetadata

func (v *Version) SetBuildMetadata(meta string) error

SetBuildMetadata sets the build metadata information for the version. If the passed string is empty or contains only whitespace, it unsets the prerelease info. If it has data, it must be a dot-separated string, where each substring contains only letters, numbers, and dashes.

func (*Version) SetPrelease

func (v *Version) SetPrelease(pre string) error

SetPrerelease sets the prerelease information for the version. If the passed string is empty or contains only whitespace, it unsets the prerelease info. If it has data, it must be a dot-separated string, where each substring contains only letters, numbers, and dashes. If a substring is fully numeric, it must not be zero padded (if it starts with 0, it must be exactly 0).

func (*Version) String

func (v *Version) String() string

type Versions

type Versions []*Version

func (Versions) Len

func (v Versions) Len() int

func (Versions) Less

func (v Versions) Less(i, j int) bool

func (Versions) Swap

func (v Versions) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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