Skip to content

Commit 59e1740

Browse files
iapershinalexey-igrychev
authored andcommitted
fix(build): add proper error message if path matcher has special chars
Signed-off-by: Yaroslav Pershin <62902094+iapershin@users.noreply.github.com>
1 parent a06839b commit 59e1740

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

pkg/path_matcher/common.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package path_matcher
22

33
import (
4-
"fmt"
4+
"os"
55
"path/filepath"
66
"strings"
77

88
"github.com/bmatcuk/doublestar/v4"
99

1010
"github.com/werf/common-go/pkg/util"
11+
"github.com/werf/logboek"
12+
)
13+
14+
const (
15+
specialCharError = "The specified glob pattern contains special characters '?[{' that need to be properly used or escaped with '\\\\'."
1116
)
1217

1318
func matchGlobs(pathPart string, globs []string) (inProgressGlobs, matchedGlobs []string) {
@@ -74,11 +79,14 @@ func isPathMatched(filePath, glob string) bool {
7479
util.SafeTrimGlobsAndSlashesFromFilepath(glob),
7580
filepath.Join(util.SafeTrimGlobsAndSlashesFromFilepath(glob), "**", "*"),
7681
} {
77-
matched, err := doublestar.PathMatch(escapeGlobSpecials(g), filePath)
82+
matched, err := doublestar.PathMatch(g, filePath)
7883
if err != nil {
79-
panic(fmt.Sprintf("failed to match path %q with glob %q: %v", filePath, g, err))
84+
logboek.Error().LogF("Failed to match path %q with glob %q: %v\n", filePath, g, err)
85+
if strings.ContainsAny(g, "?[{") {
86+
logboek.Error().LogF("%s\n", specialCharError)
87+
}
88+
os.Exit(1)
8089
}
81-
8290
if matched {
8391
return true
8492
}
@@ -87,18 +95,6 @@ func isPathMatched(filePath, glob string) bool {
8795
return false
8896
}
8997

90-
func escapeGlobSpecials(path string) string {
91-
specials := `?[]{}!\\`
92-
var b strings.Builder
93-
for _, r := range path {
94-
if strings.ContainsRune(specials, r) {
95-
b.WriteRune('\\')
96-
}
97-
b.WriteRune(r)
98-
}
99-
return b.String()
100-
}
101-
10298
func formatPaths(paths []string) []string {
10399
var result []string
104100
for _, path := range paths {

pkg/path_matcher/common_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestIsPathMatched(t *testing.T) {
1414
{
1515
name: "exact match with special character",
1616
filePath: "usr/bin/[",
17-
glob: "usr/bin/[",
17+
glob: "usr/bin/\\[",
1818
want: true,
1919
},
2020
{
@@ -47,6 +47,12 @@ func TestIsPathMatched(t *testing.T) {
4747
glob: "usr/bin",
4848
want: true,
4949
},
50+
{
51+
name: "glob with multiple patterns",
52+
filePath: "images/img/Dockerfile",
53+
glob: "images/*/{Dockerfile,werf.inc.yaml}",
54+
want: true,
55+
},
5056
}
5157

5258
for _, tt := range tests {

0 commit comments

Comments
 (0)