version

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compare added in v1.3.0

func Compare(a, b *Version) int

Compare compares two SemVer versions according to spec §11. Returns -1 if a < b, 0 if a == b, 1 if a > b. Build metadata (§10) is ignored for precedence.

func FormatCalVer

func FormatCalVer(t time.Time, format string) string

FormatCalVer formats a time using the given format string, with support for ISO week numbers. Supports special format codes: - "YYYY" or "2006" for 4-digit year - "WW" for ISO week number (01-53) All other format codes are passed to time.Format(). Examples: - "2006.01.02" -> "2025.10.03" (year.month.day) - "2006.WW" -> "2025.40" (year.week)

func IncrementHotfixSequence added in v1.3.0

func IncrementHotfixSequence(tag string) (string, error)

IncrementHotfixSequence bumps the hotfix sequence number. "v1.0.0-hotfix.2" → "v1.0.0-hotfix.3"

func IsHotfixVersion added in v1.3.0

func IsHotfixVersion(tag string) bool

IsHotfixVersion checks if version string has hotfix suffix. Examples: "v1.0.0-hotfix.1", "2025.11.09-hotfix.2", "api/v1.0.0-patch.1"

func StripPrefix

func StripPrefix(tag, prefix string) string

StripPrefix removes a prefix (e.g., "v") from a version string.

func ValidateMetadataIdentifiers added in v1.3.0

func ValidateMetadataIdentifiers(meta string) error

ValidateMetadataIdentifiers validates a build metadata string against spec §10. Identifiers are dot-separated; each must match [0-9A-Za-z-]+.

func ValidatePrereleaseIdentifiers added in v1.3.0

func ValidatePrereleaseIdentifiers(pre string) error

ValidatePrereleaseIdentifiers validates a prerelease string against spec §9. Identifiers are dot-separated; each must match [0-9A-Za-z-]+. Numeric-only identifiers must not have leading zeros.

func ValidateSingleIdentifier added in v1.3.0

func ValidateSingleIdentifier(id string) error

ValidateSingleIdentifier validates a single (no-dot) identifier for use as a prerelease channel.

func WithPrefix

func WithPrefix(version, prefix string) string

WithPrefix adds a prefix (e.g., "v") to a version string.

Types

type BumpType

type BumpType string

BumpType indicates which part of a semver to increment.

const (
	BumpMajor BumpType = "major"
	BumpMinor BumpType = "minor"
	BumpPatch BumpType = "patch"
)

type Scheme

type Scheme string

Scheme represents the versioning scheme (semver or calver).

const (
	SchemeSemVer Scheme = "semver"
	SchemeCalVer Scheme = "calver"
)

type Version

type Version struct {
	Scheme Scheme
	Raw    string

	// SemVer fields
	Major int
	Minor int
	Patch int
	Pre   string
	Meta  string

	// CalVer fields
	CalVerDate     string // e.g., "2025.10.02"
	CalVerSequence int    // optional sequence number for same-day releases
}

Version represents a parsed version tag.

func NextCalVer

func NextCalVer(current *Version, format string, now time.Time) *Version

NextCalVer generates the next calendar version using the given format and current time. If the current version is for the same period (e.g., same day/week), it increments the sequence number. Supports special format codes via FormatCalVer (e.g., "WW" for ISO week number).

For week-based formats (containing "WW"), build numbers start at 1 for clarity. For date-based formats, build numbers start at 0 (omitted) for the first release.

func ParseCalVer

func ParseCalVer(s string) (*Version, error)

ParseCalVer parses a calendar version string (without prefix). Format: YYYY.MM.DD[.SEQUENCE][-PRERELEASE][+METADATA]

or: YYYY.WW[.SEQUENCE][-PRERELEASE][+METADATA]

Supports both date-based (3 parts: year.month.day) and week-based (2 parts: year.week) formats.

func ParseHotfixVersion added in v1.3.0

func ParseHotfixVersion(tag string) (*Version, string, int, error)

ParseHotfixVersion parses versions like "v1.0.0-hotfix.3". Returns base version, suffix, and sequence number.

func ParseSemVer

func ParseSemVer(s string) (*Version, error)

ParseSemVer parses a semantic version string (without prefix). Format: MAJOR.MINOR.PATCH[-PRERELEASE][+METADATA]

func (*Version) BumpPreRelease added in v1.3.0

func (v *Version) BumpPreRelease(channel string) (*Version, error)

BumpPreRelease returns a new version with an updated prerelease identifier. The base (major.minor.patch) is preserved.

Rules:

  • Same channel → increment counter: "rc.1" → "rc.2"
  • Different channel or no prior counter → reset to 1: "alpha.2" + channel "rc" → "rc.1"
  • channel must be a single valid spec §9 identifier (no dots allowed)

func (*Version) BumpSemVer

func (v *Version) BumpSemVer(bump BumpType) *Version

BumpSemVer increments the version according to the bump type.

func (*Version) Graduate added in v1.3.0

func (v *Version) Graduate() *Version

Graduate returns a new version with prerelease and metadata cleared, preserving major.minor.patch. Use this to promote a prerelease to a stable release.

func (*Version) IsPrerelease added in v1.3.0

func (v *Version) IsPrerelease() bool

IsPrerelease reports whether the version has a prerelease identifier (spec §9).

func (*Version) IsStable added in v1.3.0

func (v *Version) IsStable() bool

IsStable reports whether the version is a stable release (no prerelease identifier).

func (*Version) String

func (v *Version) String() string

String returns the string representation of the version (without prefix).

func (*Version) WithMetadata

func (v *Version) WithMetadata(meta string) *Version

WithMetadata returns a new version with the build metadata set.

func (*Version) WithPrerelease

func (v *Version) WithPrerelease(pre string) *Version

WithPrerelease returns a new version with the prerelease identifier set.

Jump to

Keyboard shortcuts

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