github.com/tompao/docker@v1.9.1/pkg/version/version.go (about) 1 package version 2 3 import ( 4 "strconv" 5 "strings" 6 ) 7 8 // Version provides utility methods for comparing versions. 9 type Version string 10 11 func (v Version) compareTo(other Version) int { 12 var ( 13 currTab = strings.Split(string(v), ".") 14 otherTab = strings.Split(string(other), ".") 15 ) 16 17 max := len(currTab) 18 if len(otherTab) > max { 19 max = len(otherTab) 20 } 21 for i := 0; i < max; i++ { 22 var currInt, otherInt int 23 24 if len(currTab) > i { 25 currInt, _ = strconv.Atoi(currTab[i]) 26 } 27 if len(otherTab) > i { 28 otherInt, _ = strconv.Atoi(otherTab[i]) 29 } 30 if currInt > otherInt { 31 return 1 32 } 33 if otherInt > currInt { 34 return -1 35 } 36 } 37 return 0 38 } 39 40 // LessThan checks if a version is less than another 41 func (v Version) LessThan(other Version) bool { 42 return v.compareTo(other) == -1 43 } 44 45 // LessThanOrEqualTo checks if a version is less than or equal to another 46 func (v Version) LessThanOrEqualTo(other Version) bool { 47 return v.compareTo(other) <= 0 48 } 49 50 // GreaterThan checks if a version is greater than another 51 func (v Version) GreaterThan(other Version) bool { 52 return v.compareTo(other) == 1 53 } 54 55 // GreaterThanOrEqualTo checks if a version is greater than or equal to another 56 func (v Version) GreaterThanOrEqualTo(other Version) bool { 57 return v.compareTo(other) >= 0 58 } 59 60 // Equal checks if a version is equal to another 61 func (v Version) Equal(other Version) bool { 62 return v.compareTo(other) == 0 63 }