github.com/ks888/tgo@v0.0.0-20190130135156-80bf89407292/tracee/version.go (about)

     1  package tracee
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	develVersion  = "devel"
    10  	versionPrefix = "go"
    11  )
    12  
    13  // GoVersion represents a go version.
    14  type GoVersion struct {
    15  	Raw                                      string
    16  	Devel                                    bool
    17  	MajorVersion, MinorVersion, PatchVersion int
    18  }
    19  
    20  // ParseGoVersion parses the go version string such as 'go1.11.1'
    21  func ParseGoVersion(raw string) GoVersion {
    22  	goVersion := GoVersion{Raw: raw}
    23  
    24  	if strings.HasPrefix(raw, develVersion) {
    25  		goVersion.Devel = true
    26  		return goVersion
    27  	}
    28  
    29  	if !strings.HasPrefix(raw, versionPrefix) {
    30  		return goVersion
    31  	}
    32  
    33  	version := strings.Split(strings.TrimPrefix(raw, versionPrefix), ".")
    34  	if len(version) > 0 {
    35  		goVersion.MajorVersion, _ = strconv.Atoi(version[0])
    36  	}
    37  
    38  	if len(version) > 1 {
    39  		goVersion.MinorVersion, _ = strconv.Atoi(version[1])
    40  	}
    41  
    42  	if len(version) > 2 {
    43  		goVersion.PatchVersion, _ = strconv.Atoi(version[2])
    44  	}
    45  	return goVersion
    46  }
    47  
    48  // LaterThan returns true if the version is equal to or later than the given version.
    49  func (v GoVersion) LaterThan(target GoVersion) bool {
    50  	if v.Devel {
    51  		return true
    52  	}
    53  
    54  	if v.MajorVersion > target.MajorVersion {
    55  		return true
    56  	} else if v.MajorVersion < target.MajorVersion {
    57  		return false
    58  	}
    59  
    60  	if v.MinorVersion > target.MinorVersion {
    61  		return true
    62  	} else if v.MinorVersion < target.MinorVersion {
    63  		return false
    64  	}
    65  
    66  	return v.PatchVersion >= target.PatchVersion
    67  }