-
Notifications
You must be signed in to change notification settings - Fork 0
/
versions.go
84 lines (70 loc) · 1.7 KB
/
versions.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
package pulp
import (
"strings"
"github.com/NordSecurity/nordvpn-linux/internal"
"golang.org/x/mod/semver"
)
func addPrefix(str string) string {
if strings.HasPrefix(str, "v") {
return str
}
return "v" + str
}
func removePrefix(str string) string {
return strings.TrimPrefix(str, "v")
}
// transform the input slice and apply a given
// function to each element in the copy.
func transform(slice []string, fn func(string) string) []string {
ret := []string{}
for _, val := range slice {
ret = append(ret, fn(val))
}
return ret
}
// unique returns a slice with only unique elements.
func unique(slice []string) []string {
set := map[string]bool{}
for _, val := range slice {
set[val] = true
}
ret := []string{}
for elem := range set {
ret = append(ret, elem)
}
return ret
}
// last elements from the slice up to a given count.
func last(slice []string, count uint) []string {
length := len(slice)
if length == 0 || count == 0 {
return []string{}
}
if int(count) > length {
dst := make([]string, length)
copy(dst, slice)
return dst
}
lastIndex := length - 1
ret := []string{}
for i := 0; i < int(count); i++ {
ret = append(ret, slice[lastIndex-i])
}
return ret
}
func deleteFrom(versions []string, count uint) []string {
versions = transform(versions, addPrefix)
semver.Sort(versions) // requires v prefix to work correctly
uniqueVersions := unique(transform(versions, semver.MajorMinor))
semver.Sort(uniqueVersions)
toKeep := last(uniqueVersions, count)
toDelete := internal.Filter(versions, func(s string) bool {
for _, prefix := range toKeep {
if strings.HasPrefix(s, prefix) {
return false
}
}
return true
})
return transform(toDelete, removePrefix)
}