Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ func Analyze(a *Analyzer) (model.AnalyzedPaths, error) {
projectConfigFiles := make([]string, 0)
done := make(chan bool)
hasGitIgnoreFile, gitIgnore := shouldConsiderGitIgnoreFile(a.Paths[0], a.GitIgnoreFileName, a.ExcludeGitIgnore)
excludedFiles := resolveExcludedFiles(a.Exc)
// get all the files inside the given paths
for _, path := range a.Paths {
if _, err := os.Stat(path); err != nil {
Expand All @@ -351,14 +352,19 @@ func Analyze(a *Analyzer) (model.AnalyzedPaths, error) {
ext, errExt := utils.GetExtension(path)
if errExt == nil {
trimmedPath := strings.ReplaceAll(path, a.Paths[0], filepath.Base(a.Paths[0]))
ignoreFilesLen := len(ignoreFiles)
ignoreFiles = a.checkIgnore(info.Size(), hasGitIgnoreFile, gitIgnore, path, trimmedPath, ignoreFiles)
if len(ignoreFiles) > ignoreFilesLen {
excludedFiles[path] = struct{}{}
}

if isConfigFile(path, defaultConfigFiles) {
projectConfigFiles = append(projectConfigFiles, path)
a.Exc = append(a.Exc, path)
excludedFiles[path] = struct{}{}
}

if _, ok := possibleFileTypes[ext]; ok && !isExcludedFile(path, a.Exc) {
if _, ok := possibleFileTypes[ext]; ok && !isExcludedFile(path, excludedFiles) {
files = append(files, path)
}
}
Expand Down Expand Up @@ -782,20 +788,26 @@ func getKeysFromExcludeTypesFlag(excludeTypesFlag []string) []string {
return ks
}

// isExcludedFile verifies if the path is pointed in the --exclude-paths flag
func isExcludedFile(path string, exc []string) bool {
func resolveExcludedFiles(exc []string) map[string]struct{} {
excludedFiles := make(map[string]struct{})
for i := range exc {
exclude, err := provider.GetExcludePaths(exc[i])
if err != nil {
log.Err(err).Msg("failed to get exclude paths")
}
for j := range exclude {
if exclude[j] == path {
log.Info().Msgf("Excluded file %s from analyzer", path)
return true
}
excludedFiles[exclude[j]] = struct{}{}
}
}
return excludedFiles
}

// isExcludedFile verifies if the path is pointed in the --exclude-paths flag
func isExcludedFile(path string, excludedFiles map[string]struct{}) bool {
if _, ok := excludedFiles[path]; ok {
log.Info().Msgf("Excluded file %s from analyzer", path)
return true
}
return false
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package analyzer

import (
"os"
"path/filepath"
"sort"
"testing"
Expand Down Expand Up @@ -729,6 +730,33 @@ func TestAnalyzer_FileStats(t *testing.T) {
}
}

func TestAnalyzer_AnalyzeWithGlobExclude(t *testing.T) {
tempDir := t.TempDir()

err := os.MkdirAll(filepath.Join(tempDir, "nested", "excluded"), 0755)
require.NoError(t, err)
err = os.MkdirAll(filepath.Join(tempDir, "nested", "included"), 0755)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, "nested", "excluded", "main.tf"), []byte("resource \"aws_s3_bucket\" \"excluded\" {}\n"), 0644)
require.NoError(t, err)
err = os.WriteFile(filepath.Join(tempDir, "nested", "included", "main.tf"), []byte("resource \"aws_s3_bucket\" \"included\" {}\n"), 0644)
require.NoError(t, err)

analyzer := &Analyzer{
Paths: []string{tempDir},
Types: []string{""},
ExcludeTypes: []string{""},
Exc: []string{filepath.Join(tempDir, "**", "excluded", "*.tf")},
MaxFileSize: -1,
}

got, err := Analyze(analyzer)
require.NoError(t, err)

require.Contains(t, got.Types, "terraform")
require.Equal(t, 1, got.FileStats["terraform"].FileCount)
}

type platformFileStats struct {
fileCount int
dirCount int
Expand Down
Loading