Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
asafgabai committed Nov 10, 2021
2 parents 6c3a3b2 + 15e312b commit a538b35
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/jfrog/gofrog

require github.com/pkg/errors v0.8.0
require (
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.7.0
)

go 1.13
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
36 changes: 36 additions & 0 deletions stringutils/wildcards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package stringutils

import (
"regexp"
"strings"
)

// MatchWildcardPattern returns whether str matches the pattern, which may contain wildcards.
func MatchWildcardPattern(pattern string, str string) (matched bool, err error) {
regexpPattern := WildcardPatternToRegExp(pattern)
r, err := regexp.Compile(regexpPattern)
if err != nil {
return false, err
}
return r.MatchString(str), nil
}

// WildcardPatternToRegExp converts a wildcard pattern to a regular expression.
func WildcardPatternToRegExp(localPath string) string {
localPath = EscapeSpecialChars(localPath)
var wildcard = ".*"
localPath = strings.Replace(localPath, "*", wildcard, -1)
if strings.HasSuffix(localPath, "/") || strings.HasSuffix(localPath, "\\") {
localPath += wildcard
}
return "^" + localPath + "$"
}

func EscapeSpecialChars(path string) string {
// We don't replace other special characters (like parenthesis) because they're used in the placeholders logic of the JFrog CLI.
var specialChars = []string{".", "^", "$", "+"}
for _, char := range specialChars {
path = strings.Replace(path, char, "\\"+char, -1)
}
return path
}
39 changes: 39 additions & 0 deletions stringutils/wildcards_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package stringutils

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestMatchWildcardPattern(t *testing.T) {
tests := []struct {
pattern string
str string
expectedMatched bool
expectError bool
}{
{"abc", "abc", true, false},
{"abc", "abcd", false, false},
{"abc", "ab", false, false},
{"abc*", "abc", true, false},
{"abc*", "abcd", true, false},
{"abc*fg", "abcdefg", true, false},
{"abc*fg", "abdefg", false, false},
{"a*c*fg", "abcdefg", true, false},
{"a*c*fg", "abdefg", false, false},
{"a*[c", "ab[c", true, false},
}

for _, tc := range tests {
t.Run(fmt.Sprintf("pattern: %s, str: %s", tc.pattern, tc.str), func(t *testing.T) {
actualMatched, err := MatchWildcardPattern(tc.pattern, tc.str)
if tc.expectError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.expectedMatched, actualMatched)
})
}
}

0 comments on commit a538b35

Please sign in to comment.