2023-02-28 04:36:04 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Package semver provides functionality to parse and process semantic versions, as they are used in multiple components of Constellation.
|
2023-07-25 08:20:25 -04:00
|
|
|
|
|
|
|
The official [semantic versioning specification] disallows leading "v" prefixes.
|
|
|
|
However, the Constellation config uses the "v" prefix for versions to make version strings more recognizable.
|
|
|
|
This package bridges the gap between Go's semver pkg (doesn't allow "v" prefix) and the Constellation config (requires "v" prefix).
|
|
|
|
|
|
|
|
[semantic versioning specification]: https://semver.org/
|
2023-02-28 04:36:04 -05:00
|
|
|
*/
|
|
|
|
package semver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-07-25 08:20:25 -04:00
|
|
|
"errors"
|
2023-02-28 04:36:04 -05:00
|
|
|
"fmt"
|
2023-07-25 08:20:25 -04:00
|
|
|
"sort"
|
2023-02-28 04:36:04 -05:00
|
|
|
"strings"
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/compatibility"
|
2023-02-28 04:36:04 -05:00
|
|
|
"golang.org/x/mod/semver"
|
|
|
|
)
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
// Sort sorts a list of semantic version strings using [ByVersion].
|
|
|
|
func Sort(list []Semver) {
|
|
|
|
sort.Sort(byVersion(list))
|
|
|
|
}
|
|
|
|
|
2023-02-28 04:36:04 -05:00
|
|
|
// Semver represents a semantic version.
|
|
|
|
type Semver struct {
|
2023-07-25 08:20:25 -04:00
|
|
|
major int
|
|
|
|
minor int
|
|
|
|
patch int
|
|
|
|
prerelease string
|
2023-02-28 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
2023-02-03 05:05:42 -05:00
|
|
|
// New returns a Version from a string.
|
|
|
|
func New(version string) (Semver, error) {
|
2023-02-28 04:36:04 -05:00
|
|
|
// ensure that semver has "v" prefix
|
|
|
|
if !strings.HasPrefix(version, "v") {
|
|
|
|
version = "v" + version
|
|
|
|
}
|
|
|
|
|
|
|
|
if !semver.IsValid(version) {
|
|
|
|
return Semver{}, fmt.Errorf("invalid semver: %s", version)
|
|
|
|
}
|
|
|
|
|
|
|
|
version = semver.Canonical(version)
|
|
|
|
|
|
|
|
var major, minor, patch int
|
2023-04-03 04:48:28 -04:00
|
|
|
_, pre, _ := strings.Cut(version, "-")
|
2023-02-28 04:36:04 -05:00
|
|
|
_, err := fmt.Sscanf(version, "v%d.%d.%d", &major, &minor, &patch)
|
|
|
|
if err != nil {
|
|
|
|
return Semver{}, fmt.Errorf("parsing semver parts: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Semver{
|
2023-07-25 08:20:25 -04:00
|
|
|
major: major,
|
|
|
|
minor: minor,
|
|
|
|
patch: patch,
|
|
|
|
prerelease: pre,
|
2023-02-28 04:36:04 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
// NewFromInt constructs a new Semver from three integers and prerelease string: MAJOR.MINOR.PATCH-PRERELEASE.
|
|
|
|
func NewFromInt(major, minor, patch int, prerelease string) Semver {
|
|
|
|
return Semver{
|
|
|
|
major: major,
|
|
|
|
minor: minor,
|
|
|
|
patch: patch,
|
|
|
|
prerelease: prerelease,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSlice returns a slice of Semver from a slice of strings.
|
|
|
|
func NewSlice(in []string) ([]Semver, error) {
|
|
|
|
var out []Semver
|
|
|
|
for _, version := range in {
|
|
|
|
semVersion, err := New(version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing version %s: %w", version, err)
|
|
|
|
}
|
|
|
|
out = append(out, semVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToStrings converts a slice of Semver to a slice of strings.
|
|
|
|
func ToStrings(in []Semver) []string {
|
|
|
|
var out []string
|
|
|
|
for _, v := range in {
|
|
|
|
out = append(out, v.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Major returns the major version of the object.
|
|
|
|
func (v Semver) Major() int {
|
|
|
|
return v.major
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minor returns the minor version of the object.
|
|
|
|
func (v Semver) Minor() int {
|
|
|
|
return v.minor
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch returns the patch version of the object.
|
|
|
|
func (v Semver) Patch() int {
|
|
|
|
return v.patch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prerelease returns the prerelease section of the object.
|
|
|
|
func (v Semver) Prerelease() string {
|
|
|
|
return v.prerelease
|
|
|
|
}
|
|
|
|
|
2023-02-28 04:36:04 -05:00
|
|
|
// String returns the string representation of the version.
|
|
|
|
func (v Semver) String() string {
|
2023-07-25 08:20:25 -04:00
|
|
|
version := fmt.Sprintf("v%d.%d.%d", v.major, v.minor, v.patch)
|
|
|
|
if v.prerelease != "" {
|
|
|
|
return fmt.Sprintf("%s-%s", version, v.prerelease)
|
2023-04-03 04:48:28 -04:00
|
|
|
}
|
|
|
|
return version
|
2023-02-28 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compare compares two versions. It relies on the semver.Compare function internally.
|
2023-08-23 08:37:53 -04:00
|
|
|
// The result will be 0 if v == w, -1 if v < w, or +1 if v > w.
|
2023-02-28 04:36:04 -05:00
|
|
|
func (v Semver) Compare(other Semver) int {
|
|
|
|
return semver.Compare(v.String(), other.String())
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:24:35 -04:00
|
|
|
// MajorMinorEqual returns if the major and minor version of two versions are equal.
|
|
|
|
func (v Semver) MajorMinorEqual(other Semver) bool {
|
2023-07-25 08:20:25 -04:00
|
|
|
return v.major == other.major && v.minor == other.minor
|
2023-06-27 12:24:35 -04:00
|
|
|
}
|
|
|
|
|
2023-02-28 04:36:04 -05:00
|
|
|
// IsUpgradeTo returns if a version is an upgrade to another version.
|
|
|
|
// It checks if the version of v is greater than the version of other and allows a drift of at most one minor version.
|
2023-07-25 08:20:25 -04:00
|
|
|
func (v Semver) IsUpgradeTo(other Semver) error {
|
|
|
|
if v.Compare(other) <= 0 {
|
2023-08-11 04:45:56 -04:00
|
|
|
return compatibility.NewInvalidUpgradeError(other.String(), v.String(), errors.New("current version newer than or equal to new version"))
|
2023-07-25 08:20:25 -04:00
|
|
|
}
|
|
|
|
if v.major != other.major {
|
2023-08-11 04:45:56 -04:00
|
|
|
return compatibility.NewInvalidUpgradeError(other.String(), v.String(), compatibility.ErrMajorMismatch)
|
2023-07-25 08:20:25 -04:00
|
|
|
}
|
2023-02-28 04:36:04 -05:00
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
if v.minor-other.minor > 1 {
|
2023-08-11 04:45:56 -04:00
|
|
|
return compatibility.NewInvalidUpgradeError(other.String(), v.String(), compatibility.ErrMinorDrift)
|
2023-02-28 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
return nil
|
2023-02-28 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
2023-07-25 08:20:25 -04:00
|
|
|
// NextMinor returns the next minor version in the format "vMAJOR.MINOR+1".
|
2023-02-28 04:36:04 -05:00
|
|
|
func (v Semver) NextMinor() string {
|
2023-07-25 08:20:25 -04:00
|
|
|
return fmt.Sprintf("v%d.%d", v.major, v.minor+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML implements the yaml.Marshaller interface.
|
|
|
|
func (v Semver) MarshalYAML() (any, error) {
|
|
|
|
return v.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (v *Semver) UnmarshalYAML(unmarshal func(any) error) error {
|
|
|
|
var raw string
|
|
|
|
if err := unmarshal(&raw); err != nil {
|
|
|
|
return fmt.Errorf("unmarshalling to string: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
version, err := New(raw)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing semantic version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = version
|
|
|
|
|
|
|
|
return nil
|
2023-02-28 04:36:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
|
|
|
func (v Semver) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(fmt.Sprintf(`"%s"`, v.String())), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
|
|
|
func (v *Semver) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-03 05:05:42 -05:00
|
|
|
version, err := New(s)
|
2023-02-28 04:36:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = version
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-25 08:20:25 -04:00
|
|
|
|
|
|
|
// byVersion implements [sort.Interface] for sorting semantic version strings.
|
|
|
|
// Copied from Go's semver pkg with minimal modification.
|
|
|
|
// https://cs.opensource.google/go/x/mod/+/master:semver/semver.go
|
|
|
|
type byVersion []Semver
|
|
|
|
|
|
|
|
func (vs byVersion) Len() int { return len(vs) }
|
|
|
|
func (vs byVersion) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
|
|
|
|
func (vs byVersion) Less(i, j int) bool {
|
|
|
|
cmp := vs[i].Compare(vs[j])
|
|
|
|
if cmp != 0 {
|
|
|
|
return cmp < 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// if versions are equal, sort by lexicographic order
|
|
|
|
return vs[i].String() < vs[j].String()
|
|
|
|
}
|