Skip to content

Commit

Permalink
🐛 Fix perl command for MacOS (konveyor#741) (konveyor#743)
Browse files Browse the repository at this point in the history
Signed-off-by: Cherry Picker <[email protected]>

Signed-off-by: Cherry Picker <[email protected]>
Co-authored-by: Emily McMullan <[email protected]>
  • Loading branch information
konveyor-ci-bot[bot] and eemcmullan authored Dec 3, 2024
1 parent 6ec6ba8 commit 7b4c586
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}

0 comments on commit 7b4c586

Please sign in to comment.