2014-02-21 18:15:28 -05:00
|
|
|
package version
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
// Version provides utility methods for comparing versions.
|
2014-02-21 18:15:28 -05:00
|
|
|
type Version string
|
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
func (v Version) compareTo(other Version) int {
|
2014-02-21 18:15:28 -05:00
|
|
|
var (
|
2014-10-06 11:41:53 -04:00
|
|
|
currTab = strings.Split(string(v), ".")
|
2014-04-01 18:46:52 -04:00
|
|
|
otherTab = strings.Split(string(other), ".")
|
2014-02-21 18:15:28 -05:00
|
|
|
)
|
2014-08-11 17:40:35 -04:00
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
max := len(currTab)
|
2014-08-11 17:40:35 -04:00
|
|
|
if len(otherTab) > max {
|
|
|
|
max = len(otherTab)
|
|
|
|
}
|
|
|
|
for i := 0; i < max; i++ {
|
2014-10-06 11:41:53 -04:00
|
|
|
var currInt, otherInt int
|
2014-08-11 17:40:35 -04:00
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
if len(currTab) > i {
|
|
|
|
currInt, _ = strconv.Atoi(currTab[i])
|
2014-08-11 17:40:35 -04:00
|
|
|
}
|
2014-02-21 18:15:28 -05:00
|
|
|
if len(otherTab) > i {
|
|
|
|
otherInt, _ = strconv.Atoi(otherTab[i])
|
|
|
|
}
|
2014-10-06 11:41:53 -04:00
|
|
|
if currInt > otherInt {
|
2014-02-21 18:15:28 -05:00
|
|
|
return 1
|
|
|
|
}
|
2014-10-06 11:41:53 -04:00
|
|
|
if otherInt > currInt {
|
2014-02-21 18:15:28 -05:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-12-17 05:52:23 -05:00
|
|
|
// String returns the version string
|
|
|
|
func (v Version) String() string {
|
|
|
|
return string(v)
|
|
|
|
}
|
|
|
|
|
2015-03-24 12:46:22 -04:00
|
|
|
// LessThan checks if a version is less than another
|
2014-10-06 11:41:53 -04:00
|
|
|
func (v Version) LessThan(other Version) bool {
|
|
|
|
return v.compareTo(other) == -1
|
2014-02-21 18:15:28 -05:00
|
|
|
}
|
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
// LessThanOrEqualTo checks if a version is less than or equal to another
|
|
|
|
func (v Version) LessThanOrEqualTo(other Version) bool {
|
|
|
|
return v.compareTo(other) <= 0
|
2014-02-21 18:15:28 -05:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:46:22 -04:00
|
|
|
// GreaterThan checks if a version is greater than another
|
2014-10-06 11:41:53 -04:00
|
|
|
func (v Version) GreaterThan(other Version) bool {
|
|
|
|
return v.compareTo(other) == 1
|
2014-02-21 18:15:28 -05:00
|
|
|
}
|
|
|
|
|
2015-03-24 12:46:22 -04:00
|
|
|
// GreaterThanOrEqualTo checks if a version is greater than or equal to another
|
2014-10-06 11:41:53 -04:00
|
|
|
func (v Version) GreaterThanOrEqualTo(other Version) bool {
|
|
|
|
return v.compareTo(other) >= 0
|
2014-02-21 18:15:28 -05:00
|
|
|
}
|
|
|
|
|
2014-10-06 11:41:53 -04:00
|
|
|
// Equal checks if a version is equal to another
|
|
|
|
func (v Version) Equal(other Version) bool {
|
|
|
|
return v.compareTo(other) == 0
|
2014-02-21 18:15:28 -05:00
|
|
|
}
|