Skip to content

Commit

Permalink
🐛 Fix perl command for MacOS (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemcmullan authored Dec 3, 2024
1 parent 80d7fb0 commit bea5c15
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 @@ -616,14 +616,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)
if ok, paths := providerContext.GetScopedFilepaths(); ok {
Expand All @@ -642,3 +653,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 bea5c15

Please sign in to comment.