Skip to content

Commit

Permalink
🐛 add exclusion to windows grep
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <[email protected]>
  • Loading branch information
pranavgaikwad committed Jan 14, 2025
1 parent 5528072 commit 10ee147
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pr-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jobs:
runs_on:
- ubuntu-latest
- macos-latest
- windows-latest
runs-on: ${{ matrix.runs_on }}
steps:
- uses: actions/checkout@v3
Expand Down
63 changes: 43 additions & 20 deletions provider/internal/builtin/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,18 +604,44 @@ func runOSSpecificGrepCommand(pattern string, location string, providerContext p
// This is a workaround until we can find a better solution
psScript := `
$pattern = $env:PATTERN
$location = $env:FILEPATH
Get-ChildItem -Path $location -Recurse -File | ForEach-Object {
$file = $_
# Search for the pattern in the file
Select-String -Path $file.FullName -Pattern $pattern -AllMatches | ForEach-Object {
foreach ($match in $_.Matches) {
"{0}:{1}:{2}" -f $file.FullName, $_.LineNumber, $match.Value
}
$locations = $env:FILEPATHS -split ','
%s
foreach ($location in $locations) {
Get-ChildItem -Path $location -Recurse -File |
%s
ForEach-Object {
$file = $_
# Search for the pattern in the file
Select-String -Path $file.FullName -Pattern $pattern -AllMatches | ForEach-Object {
foreach ($match in $_.Matches) {
"{0}:{1}:{2}" -f $file.FullName, $_.LineNumber, $match.Value
}
}
}
}`
exclusionScript := `
Where-Object {
-not ($excludedPaths | ForEach-Object { $_ -and ($_.FullName -like $_) })
} |
`
exclusionEnvVar := `
$excluded_paths = $env.EXCLUDEDPATHS -split ','
`
locations := []string{location}
if ok, paths := providerContext.GetScopedFilepaths(); ok {
locations = paths
psScript = fmt.Sprintf(psScript, "", "")
} else if len(excludePatterns) > 0 {
psScript = fmt.Sprintf(psScript, exclusionEnvVar, exclusionScript)
} else {
psScript = fmt.Sprintf(psScript, "", "")
}
findstr := exec.Command(utilName, "-Command", psScript)
findstr.Env = append(os.Environ(), "PATTERN="+pattern, "FILEPATH="+location)
findstr.Env = append(os.Environ(),
"PATTERN="+pattern,
"FILEPATHS="+strings.Join(locations, ","),
"EXCLUDEDPATHS="+strings.Join(excludePatterns, ","),
)
outputBytes, err = findstr.Output()

// TODO eventually replace with platform agnostic solution
Expand Down Expand Up @@ -647,11 +673,7 @@ func runOSSpecificGrepCommand(pattern string, location string, providerContext p
} else {
excludeOpts := ""
for _, pattern := range excludePatterns {
if stat, err := os.Stat(pattern); err == nil && stat.IsDir() {
excludeOpts = fmt.Sprintf("%s ! -path '%s*'", excludeOpts, pattern)
} else {
excludeOpts = fmt.Sprintf("%s ! -path '%s'", excludeOpts, pattern)
}
excludeOpts = fmt.Sprintf("%s ! -path '%s'", excludeOpts, pattern)
}
cmd = fmt.Sprintf(cmd, excludeOpts)
}
Expand All @@ -667,11 +689,7 @@ func runOSSpecificGrepCommand(pattern string, location string, providerContext p
}
findArgs := []string{}
for _, pattern := range excludePatterns {
if stat, err := os.Stat(pattern); err == nil && stat.IsDir() {
findArgs = append(findArgs, "!", "-path", fmt.Sprintf("%s*", pattern))
} else {
findArgs = append(findArgs, "!", "-path", pattern)
}
findArgs = append(findArgs, "!", "-path", pattern)
}
findArgs = append(findArgs, "-type", "f")
findArgs = append(findArgs, "-exec", "grep")
Expand Down Expand Up @@ -713,7 +731,12 @@ func getGloblikeExcludePatterns(ctx provider.ProviderContext, log logr.Logger) [
if strings.Contains(pattern, "\\") || strings.Contains(pattern, "{") {
log.V(5).Info("unsupported regex pattern for exclusion", pattern)
}
if pattern != "" {
if pattern == "" {
continue
}
if stat, err := os.Stat(pattern); err == nil && stat.IsDir() {
patterns = append(patterns, fmt.Sprintf("%s*", pattern))
} else {
patterns = append(patterns, pattern)
}
}
Expand Down
6 changes: 2 additions & 4 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,8 @@ type ProviderContext struct {
}

// GetScopedFilepaths returns a list of filepaths based on either included or excluded paths in context
// when tmpl.Filepaths is set, it is including specific files. we return the value of tmpl.Filepaths as-is
// when tmpl.ExcludedPaths is set, it will exclude the files but we don't know set of all files to exclude from
// as a result, we need an input list of paths to exclude files from. this is upto providers how to pass that list
// when both are set, exclusion happens on union of input paths and included paths.
// in a chaintemplate, Filepaths controls inclusion and ExcludedPaths controls exclusion
// when both are set, exclusion applies after inclusion
func (p *ProviderContext) GetScopedFilepaths(paths ...string) (bool, []string) {
if value, ok := p.Template[engine.TemplateContextPathScopeKey]; ok {
includedPaths := []string{}
Expand Down

0 comments on commit 10ee147

Please sign in to comment.