...

Source file src/edge-infra.dev/pkg/lib/build/semver/semver.go

Documentation: edge-infra.dev/pkg/lib/build/semver

     1  // Package semver provides utilities for working with semantic
     2  // versioning based on https://semver.org
     3  package semver
     4  
     5  import (
     6  	"log"
     7  	"regexp"
     8  
     9  	"errors"
    10  )
    11  
    12  const (
    13  	// SemverRegexpString is the officially recommended regular expression for
    14  	// validating SemVer: https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
    15  	SemverRegexpString = `^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`
    16  )
    17  
    18  func init() {
    19  	_, err := regexp.Compile(SemverRegexpString)
    20  	if err != nil {
    21  		log.Fatal("semver.init: failed to compile the SemVer regular expression")
    22  	}
    23  }
    24  
    25  // IsValidSemver checks the input string against the recommended SemVer
    26  // regular expression.
    27  func IsValidSemver(ver string) error {
    28  	// We can drop the error because we have a test that confirms the Regexp string
    29  	// compiles, which is the only type of error regexp.MatchString can return
    30  	ok, _ := regexp.MatchString(SemverRegexpString, ver)
    31  	// if its not valid, return our custom error
    32  	if !ok {
    33  		return ErrInvalidSemVer
    34  	}
    35  	return nil
    36  }
    37  
    38  // ErrInvalidSemVer when the SemVer string simply don't follow that SemVer.org
    39  // specification.
    40  var ErrInvalidSemVer = errors.New("invalid semver provided")
    41  

View as plain text