github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/versioning.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package p2pcommon
     7  
     8  import (
     9  	"errors"
    10  	"github.com/coreos/go-semver/semver"
    11  	"regexp"
    12  )
    13  
    14  // AergoVersion follows sementic versioning https://semver.org/
    15  type AergoVersion = semver.Version
    16  
    17  // const verPattern = `v([0-9]+)\.([0-9]+)\..([0-9]+)(-(.+))?`
    18  const verPattern = `v[0-9].+`
    19  var checker, _ = regexp.Compile(verPattern)
    20  
    21  // ParseAergoVersion parse version string to sementic version with slightly different manner. This function allows not only standard sementic version but also version strings containing v in prefixes.
    22  func ParseAergoVersion(verStr string) (AergoVersion, error) {
    23  	if checker.MatchString(verStr) {
    24  		verStr = verStr[1:]
    25  	}
    26  	ver, err := semver.NewVersion(verStr)
    27  	if err != nil {
    28  		return AergoVersion{}, errors.New("invalid version "+verStr)
    29  	}
    30  	return AergoVersion(*ver), nil
    31  }
    32  
    33  // Supported Aergo version. polaris will register aergosvr within the version range. This version range should be modified when new release is born.
    34  const (
    35  	MinimumAergoVersion = "v1.2.3"
    36  	MaximumAergoVersion = "v2.0.0"
    37  )
    38  var (
    39  	minAergoVersion AergoVersion // inclusive
    40  	maxAergoVersion AergoVersion // exclusive
    41  )
    42  
    43  func init() {
    44  	var err error
    45  	minAergoVersion, err = ParseAergoVersion(MinimumAergoVersion)
    46  	if err != nil {
    47  		panic("Invalid minimum version "+MinimumAergoVersion)
    48  	}
    49  	maxAergoVersion, err = ParseAergoVersion(MaximumAergoVersion)
    50  	if err != nil {
    51  		panic("Invalid maximum version "+MaximumAergoVersion)
    52  	}
    53  }
    54  
    55  
    56  func CheckVersion(version string) bool {
    57  	ver, err := ParseAergoVersion(version)
    58  	if err != nil {
    59  		return false
    60  	}
    61  
    62  	return ver.Compare(minAergoVersion) >= 0 && ver.Compare(maxAergoVersion) < 0
    63  }