summaryrefslogtreecommitdiff
path: root/pkg/version/version.go
blob: 5ff9d2ed2a0c34ac7f498fcfa5231420e2bff103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
package version

import (
	"strconv"
	"strings"
)

type Version string

func (me Version) compareTo(other Version) int {
	var (
		meTab    = strings.Split(string(me), ".")
		otherTab = strings.Split(string(other), ".")
	)
	for i, s := range meTab {
		var meInt, otherInt int
		meInt, _ = strconv.Atoi(s)
		if len(otherTab) > i {
			otherInt, _ = strconv.Atoi(otherTab[i])
		}
		if meInt > otherInt {
			return 1
		}
		if otherInt > meInt {
			return -1
		}
	}
	if len(otherTab) > len(meTab) {
		return -1
	}
	return 0
}

func (me Version) LessThan(other Version) bool {
	return me.compareTo(other) == -1
}

func (me Version) LessThanOrEqualTo(other Version) bool {
	return me.compareTo(other) <= 0
}

func (me Version) GreaterThan(other Version) bool {
	return me.compareTo(other) == 1
}

func (me Version) GreaterThanOrEqualTo(other Version) bool {
	return me.compareTo(other) >= 0
}

func (me Version) Equal(other Version) bool {
	return me.compareTo(other) == 0
}