-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.go
116 lines (101 loc) · 2.15 KB
/
list.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package semv
import (
"bytes"
"strings"
"github.com/blang/semver"
)
// TagCmd for tag list
var TagCmd = []string{"tag", "--list", "--sort=v:refname"}
var git = "git"
var tagCmder Cmder
var defaultVersion = "0.0.0"
// List struct
type List struct {
data semver.Versions
}
// GetList returns List
func GetList() (*List, error) {
list, err := getVersions()
if err != nil {
return nil, err
}
return &List{data: list}, nil
}
// FindSimilar finds similar one
func (l *List) FindSimilar(v semver.Version) *Semv {
newSemv := &Semv{}
for _, vv := range l.data {
if vv.Major == v.Major && vv.Minor == v.Minor && vv.Patch == v.Patch {
newSemv = MustNew(vv.String())
}
}
return newSemv
}
// String to string
func (l *List) String() string {
var ll []string
for _, v := range l.data {
ll = append(ll, defaultTagPrefix+v.String())
}
return strings.Join(ll, "\n")
}
// Latest gets latest version from List
func (l *List) Latest() *Semv {
if l.data.Len() > 0 {
return &Semv{data: l.data[len(l.data)-1]}
}
return &Semv{data: semver.MustParse(defaultVersion)}
}
// WithoutPreRelease excludes pre-release
func (l *List) WithoutPreRelease() *List {
var list semver.Versions
for _, v := range l.data {
if len(v.Pre) > 0 {
continue
}
list = append(list, v)
}
return &List{data: list}
}
// OnlyPreRelease includes only pre-release
func (l *List) OnlyPreRelease() *List {
var list semver.Versions
for _, v := range l.data {
if len(v.Pre) == 0 {
continue
}
list = append(list, v)
}
return &List{data: list}
}
// getVersions executes git tag as command
func getVersions() (semver.Versions, error) {
if tagCmder == nil {
tagCmder = Cmd{}
}
b, err := tagCmder.Do(git, TagCmd...)
if err != nil {
return nil, err
}
b = bytes.TrimSpace(b)
vv := []string{defaultVersion}
if len(b) > 0 {
vv = strings.Split(string(b), "\n")
}
var list semver.Versions
for _, v := range vv {
if defaultTagPrefix != "" {
trimmed := strings.TrimPrefix(v, defaultTagPrefix)
if trimmed == v {
continue
}
}
sv, err := semver.ParseTolerant(v)
if err != nil {
continue
}
list = append(list, sv)
}
semver.Sort(list)
return list, nil
}