version.go 1.62 KB
Newer Older
1 2 3 4 5 6 7
package build

import (
	"strconv"
	"strings"
)

8 9
const (
	// Version is the current version of siad.
Luke Champine's avatar
Luke Champine committed
10
	Version = "1.1.1"
11 12 13 14 15 16 17 18

	// MaxEncodedVersionLength is the maximum length of a version string encoded
	// with the encode package. 100 is much larger than any version number we send
	// now, but it allows us to send additional information in the version string
	// later if we choose. For example appending the version string with the HEAD
	// commit hash.
	MaxEncodedVersionLength = 100
)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

// IsVersion returns whether str is a valid version number.
func IsVersion(str string) bool {
	for _, n := range strings.Split(str, ".") {
		if _, err := strconv.Atoi(n); err != nil {
			return false
		}
	}
	return true
}

// min returns the smaller of two integers.
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// VersionCmp returns an int indicating the difference between a and b. It
// follows the convention of bytes.Compare and big.Cmp:
//
//   -1 if a <  b
//    0 if a == b
//   +1 if a >  b
//
// One important quirk is that "1.1.0" is considered newer than "1.1", despite
// being numerically equal.
func VersionCmp(a, b string) int {
	aNums := strings.Split(a, ".")
	bNums := strings.Split(b, ".")
	for i := 0; i < min(len(aNums), len(bNums)); i++ {
		// assume that both version strings are valid
		aInt, _ := strconv.Atoi(aNums[i])
		bInt, _ := strconv.Atoi(bNums[i])
		if aInt < bInt {
			return -1
		} else if aInt > bInt {
			return 1
		}
	}
	// all shared digits are equal, but lengths may not be equal
	if len(aNums) < len(bNums) {
		return -1
	} else if len(aNums) > len(bNums) {
		return 1
	}
	// strings are identical
	return 0
}