-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
65 lines (52 loc) · 1.1 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
58
59
60
61
62
63
64
65
package semvergo
import (
"fmt"
"strconv"
"strings"
)
// Version is a type
type Version struct {
Major uint
Minor uint
Patch uint
Branch string
Meta string
}
// NewFromString returns a new Version
func NewFromString(s string) (*Version, error) {
return fromString(s)
}
// String implements Stringer
func (v Version) String() string {
s := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
if strings.TrimSpace(v.Branch) != "" {
s += fmt.Sprintf("-%s", v.Branch)
}
return s
}
func fromString(s string) (*Version, error) {
var v Version
a := strings.Split(s, "-")
if len(a) == 2 {
v.Branch = a[1]
}
parts := strings.Split(a[0], ".")
if len(parts) == 3 {
major, err := strconv.Atoi(parts[0])
if err != nil {
return nil, fmt.Errorf("invalid major: %v", err)
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return nil, fmt.Errorf("invalid minor: %v", err)
}
patch, err := strconv.Atoi(parts[2])
if err != nil {
return nil, fmt.Errorf("invalid patch: %v", err)
}
v.Major = uint(major)
v.Minor = uint(minor)
v.Patch = uint(patch)
}
return &v, nil
}