Skip to content

Commit

Permalink
Implement FindLatest
Browse files Browse the repository at this point in the history
  • Loading branch information
haya14busa committed Mar 26, 2019
0 parents commit 24bbd08
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/haya14busa/xtag

go 1.12

require github.com/haya14busa/go-versionsort v0.0.0-20190326014014-5b3a40d6070a
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/haya14busa/go-versionsort v0.0.0-20190326014014-5b3a40d6070a h1:C75TMjl9nVoJt0B2KcbO6qESAF3lG4vVYgDXoxotWrg=
github.com/haya14busa/go-versionsort v0.0.0-20190326014014-5b3a40d6070a/go.mod h1:LUfyxWIo5I3AzeZxF5+/wAUM5sz5ZibEh/Ck3dZrN50=
35 changes: 35 additions & 0 deletions xtag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xtag

import (
"fmt"
"regexp"
"strings"

"github.com/haya14busa/go-versionsort"
)

// FindLatest finds latest tag matched with give xtag (e.g. v1.2.x).
func FindLatest(xtag string, tags []string) (string, error) {
if !strings.HasSuffix(xtag, "x") {
return "", fmt.Errorf("xtag must ends with 'x': %q", xtag)
}
target := "x"
if strings.HasSuffix(xtag, ".x") {
target = `\.x`
}
pattern := strings.Replace(regexp.QuoteMeta(xtag), target, `[0-9.]*`, -1)
re, err := regexp.Compile(pattern)
if err != nil {
return "", err
}
latest := ""
for _, tag := range tags {
if re.MatchString(tag) && versionsort.Less(latest, tag) {
latest = tag
}
}
if latest != "" {
return latest, nil
}
return "", fmt.Errorf("latest tag not found: %q", xtag)
}
28 changes: 28 additions & 0 deletions xtag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package xtag

import "testing"

func TestFindLatest(t *testing.T) {
tests := []struct {
xtag string
tags []string
want string
}{
{"v1.x", []string{"v1.0", "v1.5", "v1.4", "v2.0"}, "v1.5"},
{"1.x", []string{"1.0", "1.5", "1.4", "2.0"}, "1.5"},
{"v2.x", []string{"v1.0", "v2.0.0", "v3.0"}, "v2.0.0"},
{"v1.2.x", []string{"v1.0", "v2.0.0", "v1.2.1", "v1.2.3"}, "v1.2.3"},
{"vx", []string{"v1.0", "v2.0.0", "v1.2.1"}, "v2.0.0"},
{"x", []string{"1.0", "2.0.0", "1.2.1"}, "2.0.0"},
}
for _, tt := range tests {
got, err := FindLatest(tt.xtag, tt.tags)
if err != nil {
t.Errorf("%s: got error: %v", tt.xtag, err)
continue
}
if got != tt.want {
t.Errorf("FindLatest(%q, %s) = %q, want %q", tt.xtag, tt.tags, got, tt.want)
}
}
}

0 comments on commit 24bbd08

Please sign in to comment.