-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaliases.go
115 lines (90 loc) · 2.12 KB
/
aliases.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
// Package aliases creates deduplicated version aliases based on valid Semver release names.
//
// Library takes care of optional prefixed releases (`v`) as well as all version names are deduplicated and sorted in lexicographic order.
// For example this library can be used to create Semver and custom aliases for tagging Docker images.
package aliases
import (
"sort"
"strconv"
"strings"
"github.com/coreos/go-semver/semver"
)
// FromVersion creates tag aliases based on a valid Semver release.
func FromVersion(version string) []string {
version = strings.TrimPrefix(
strings.TrimSpace(version), "v",
)
if version == "" {
return nil
}
v, err := semver.NewVersion(version)
if err != nil {
return nil
}
// just return pre-release versions
if v.PreRelease != "" {
return []string{version}
}
var tags []string
var tag = ""
for _, x := range v.Slice() {
n := strconv.FormatInt(x, 10)
if tag == "" {
tag = n
} else {
tag = tag + "." + n
}
tags = append(tags, tag)
}
return tags
}
// FromVersionNames validates and returns deduplicated tag aliases.
func FromVersionNames(names []string) []string {
names = filterInputNames(names)
if len(names) == 0 {
return nil
}
sort.Strings(names)
j := 0
for i := 1; i < len(names); i++ {
if names[j] == names[i] {
continue
}
j++
names[j] = names[i]
}
return names[:j+1]
}
// GetVersionNamesSuffixed returns a version names with its items suffixed.
func GetVersionNamesSuffixed(names []string, suffix string) []string {
names = FromVersionNames(names)
if len(names) == 0 {
return nil
}
if suffix == "" {
return names
}
sort.Strings(names)
var strs []string
for _, s := range names {
strs = append(strs, s+"-"+suffix)
}
return strs
}
// filterInputNames filters input names like empty values or `v` prefixes
func filterInputNames(names []string) []string {
if len(names) == 0 {
return nil
}
var strv []string
for _, s := range names {
s = strings.TrimSpace(s)
// skip empty values
if s != "" {
// trim version prefixes append to the list
s = strings.TrimPrefix(s, "v")
strv = append(strv, s)
}
}
return strv
}