From 7b4c586939e85ec019fe9306b123b965629a2fbc Mon Sep 17 00:00:00 2001 From: "konveyor-ci-bot[bot]" <159171263+konveyor-ci-bot[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:11:17 -0500 Subject: [PATCH] :bug: Fix perl command for MacOS (#741) (#743) Signed-off-by: Cherry Picker Signed-off-by: Cherry Picker Co-authored-by: Emily McMullan --- provider/internal/builtin/service_client.go | 24 +++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/provider/internal/builtin/service_client.go b/provider/internal/builtin/service_client.go index a39a4b25..2dbd1ecb 100644 --- a/provider/internal/builtin/service_client.go +++ b/provider/internal/builtin/service_client.go @@ -535,14 +535,25 @@ func runOSSpecificGrepCommand(pattern string, location string, providerContext p findstr.Env = append(os.Environ(), "PATTERN="+pattern, "FILEPATH="+location) outputBytes, err = findstr.Output() + // TODO eventually replace with platform agnostic solution } else if runtime.GOOS == "darwin" { + isEscaped := isSlashEscaped(pattern) + escapedPattern := pattern + // some rules already escape '/' while others do not + if !isEscaped { + escapedPattern = strings.ReplaceAll(escapedPattern, "/", "\\/") + } + // escape other chars used in perl pattern + escapedPattern = strings.ReplaceAll(escapedPattern, "'", "'\\''") + escapedPattern = strings.ReplaceAll(escapedPattern, "$", "\\$") cmd := fmt.Sprintf( `find %v -type f | \ - while read file; do perl -ne '/(%v)/ && print "$ARGV:$.:$1\n";' "$file"; done`, - location, pattern, + while read file; do perl -ne '/%v/ && print "$ARGV:$.:$1\n";' "$file"; done`, + location, escapedPattern, ) findstr := exec.Command("/bin/sh", "-c", cmd) outputBytes, err = findstr.Output() + } else { grep := exec.Command("grep", "-o", "-n", "-R", "-P", pattern, location) outputBytes, err = grep.Output() @@ -556,3 +567,12 @@ func runOSSpecificGrepCommand(pattern string, location string, providerContext p return outputBytes, nil } + +func isSlashEscaped(str string) bool { + for i := 0; i < len(str); i++ { + if str[i] == '/' && i > 0 && str[i-1] == '\\' { + return true + } + } + return false +}