Files
dirmd/internal/filter/filter.go
T

85 lines
1.5 KiB
Go

// internal/filter/filter.go
package filter
import (
"os"
"path/filepath"
"strings"
)
type Filter struct {
MaxSize int64
Extensions []string
Ignores []string
OmitTest bool
}
func (f *Filter) ShouldSkipDir(name string) bool {
if strings.HasPrefix(name, ".") {
return true
}
for _, pattern := range f.Ignores {
matched, _ := filepath.Match(pattern, name)
if matched {
return true
}
}
return false
}
func (f *Filter) ShouldSkipFile(path, name string, info os.FileInfo) bool {
if strings.HasPrefix(name, ".") {
return true
}
for _, pattern := range f.Ignores {
matched, _ := filepath.Match(pattern, name)
if matched {
return true
}
}
ext := filepath.Ext(path)
for _, bad := range f.Extensions {
if strings.EqualFold(ext, bad) {
return true
}
}
if info.Size() > f.MaxSize {
return true
}
if f.OmitTest && isTestFile(path, name) {
return true
}
return false
}
func isTestFile(path, name string) bool {
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".go":
return strings.HasSuffix(strings.ToLower(name), "_test.go")
case ".java":
base := strings.TrimSuffix(name, filepath.Ext(name))
if strings.HasSuffix(base, "Test") || strings.HasSuffix(base, "Tests") {
return true
}
slashPath := filepath.ToSlash(path)
return strings.Contains(slashPath, "/src/test/")
}
return false
}
func IsSymlink(path string) bool {
info, err := os.Lstat(path)
if err != nil {
return false
}
return info.Mode()&os.ModeSymlink != 0
}