Skip to content

Commit

Permalink
feat(semver): Adding support for long prefix for linkerd notably
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 3, 2021
1 parent 62ecf2e commit d7c4fb0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
42 changes: 26 additions & 16 deletions pkg/semver/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ const (
alpha NonFinalVersion = iota + 1
beta
canary
edge
rc
test
)

var (
// According to https://semver.org/#spec-item-11
semverMatcher = regexp.MustCompile(`(?i)^[a-zA-Z]*([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(-[a-zA-Z0-9.]+)?(?:\+[a-zA-Z0-9.]+)?$`)
semverMatcher = regexp.MustCompile(`(?i)^([a-zA-Z-]*)([0-9]+)\.([0-9]+)(?:\.([0-9]+))?(-[a-zA-Z0-9.]+)?(?:\+[a-zA-Z0-9.]+)?$`)

nonFinalVersions = []string{"alpha", "beta", "canary", "rc", "test"}
nonFinalVersions = []string{"alpha", "beta", "canary", "edge", "rc", "test"}

// NoneVersion is the empty semver
NoneVersion = Version{}
Expand Down Expand Up @@ -96,40 +97,49 @@ func Parse(version string) (Version, error) {
}

semver := Version{
Name: version,
Name: version,
suffix: parseNonFinalVersion(matches),
}
var err error

semver.major, err = strconv.ParseUint(matches[1], 10, 64)
semver.major, err = strconv.ParseUint(matches[2], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version major is not numeric")
}

if len(matches[2]) != 0 {
semver.minor, err = strconv.ParseUint(matches[2], 10, 64)
if len(matches[3]) != 0 {
semver.minor, err = strconv.ParseUint(matches[3], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version minor is not numeric")
}
}

if len(matches[3]) != 0 {
semver.patch, err = strconv.ParseUint(matches[3], 10, 64)
if len(matches[4]) != 0 {
semver.patch, err = strconv.ParseUint(matches[4], 10, 64)
if err != nil {
return NoneVersion, fmt.Errorf("version patch is not numeric")
}
}

if len(matches[4]) != 0 {
for index, nonFinalVersion := range nonFinalVersions {
if strings.Contains(matches[4], nonFinalVersion) {
semver.suffix = NonFinalVersion(index + 1)
}
return semver, nil
}

func parseNonFinalVersion(matches []string) NonFinalVersion {
if len(matches[1]) <= 1 && len(matches[5]) == 0 {
return -1
}

for index, nonFinalVersion := range nonFinalVersions {
if strings.Contains(matches[1], nonFinalVersion) {
return NonFinalVersion(index + 1)
}

if len(matches[5]) != 0 && strings.Contains(matches[5], nonFinalVersion) {
return NonFinalVersion(index + 1)
}
} else {
semver.suffix = -1
}

return semver, nil
return 0
}

func safeParse(version string) Version {
Expand Down
16 changes: 16 additions & 0 deletions pkg/semver/semver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ func TestParse(t *testing.T) {
NoneVersion,
errors.New("unable to parse version"),
},
{
"prefixed version",
args{
version: "stable-2.10.1",
},
Version{"stable-2.10.1", 2, 10, 1, 0},
nil,
},
{
"edge version",
args{
version: "edge-2.10.1",
},
Version{"edge-2.10.1", 2, 10, 1, edge},
nil,
},
{
"flag rc version",
args{
Expand Down

0 comments on commit d7c4fb0

Please sign in to comment.