-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
57 lines (49 loc) · 1.26 KB
/
version.go
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
53
54
55
56
57
// Copyright 2013 The GoGL2 Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.mkd file.
package main
import (
"fmt"
"strconv"
"strings"
)
type Version struct {
Major int
Minor int
}
func ParseVersion(version string) (Version, error) {
split := strings.Split(version, ".")
if len(split) != 2 {
return Version{0, 0}, fmt.Errorf("Invalid version string: '%s'.", version)
}
return ParseVersionMajMin(split[0], split[1])
}
func ParseVersionMajMin(major, minor string) (Version, error) {
majorNumber, err := strconv.Atoi(major)
if err != nil {
return Version{0, 0}, fmt.Errorf("Invalid major version number: '%s'.", major)
}
minorNumber, err := strconv.Atoi(minor)
if err != nil {
return Version{0, 0}, fmt.Errorf("Invalid minor version number: '%s'.", minor)
}
return Version{majorNumber, minorNumber}, nil
}
func (v Version) Compare(v2 Version) int {
if v.Major < v2.Major {
return -1
} else if v.Major > v2.Major {
return 1
} else if v.Minor < v2.Minor {
return -1
} else if v.Minor > v2.Minor {
return 1
}
return 0
}
func (v Version) Valid() bool {
return v.Major != 0 || v.Minor != 0
}
func (v Version) String() string {
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
}