-
Notifications
You must be signed in to change notification settings - Fork 3
/
semver.go
57 lines (49 loc) · 1.47 KB
/
semver.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
package main
import (
"fmt"
"strings"
"github.com/aquasecurity/go-version/pkg/semver"
)
const (
commentDelimiter = "#"
tagSlashDelimiter = "/"
tagAtmarkDelimiter = "@"
versionPrefix = "v"
)
func newSemver(currentTag string, incrLevel string) (string, error) {
separatedTagWithComment := strings.Split(currentTag, commentDelimiter)
tagWithoutComment := strings.TrimSpace(separatedTagWithComment[0])
separatedTag := strings.Split(tagWithoutComment, tagSlashDelimiter)
var isTagAtmarkDelimiter bool
isTagAtmarkDelimiter = false
if len(separatedTag) == 1 {
separatedTag = strings.Split(tagWithoutComment, tagAtmarkDelimiter)
if len(separatedTag) > 1 {
isTagAtmarkDelimiter = true
}
}
currentSemver := separatedTag[len(separatedTag)-1]
sv, err := semver.Parse(strings.TrimPrefix(currentSemver, versionPrefix))
if err != nil {
return "", err
}
var nextSemver string
switch incrLevel {
case "":
return "", fmt.Errorf("next_semver_level is empty")
case "patch":
nextSemver = sv.IncPatch().String()
case "minor":
nextSemver = sv.IncMinor().String()
case "major":
nextSemver = sv.IncMajor().String()
default:
return "", fmt.Errorf("%s not supported", incrLevel)
}
if isTagAtmarkDelimiter {
separatedTag[len(separatedTag)-1] = fmt.Sprintf("%s", nextSemver)
return strings.Join(separatedTag, tagAtmarkDelimiter), nil
}
separatedTag[len(separatedTag)-1] = fmt.Sprintf("v%s", nextSemver)
return strings.Join(separatedTag, tagSlashDelimiter), nil
}