135 lines
2.6 KiB
Go
135 lines
2.6 KiB
Go
package filter
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"git.flo.fo/FLO/dirmd/internal/config"
|
|
)
|
|
|
|
type Filter struct {
|
|
MaxSize int64
|
|
Extensions []string
|
|
Ignores []string
|
|
OmitTest bool
|
|
FrontendOnly bool
|
|
OmitMd 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
|
|
}
|
|
}
|
|
|
|
if info.Size() > f.MaxSize {
|
|
return true
|
|
}
|
|
|
|
if f.OmitMd && IsNonReadmeMarkdown(path, name) {
|
|
return true
|
|
}
|
|
|
|
if f.FrontendOnly && !IsFrontendOrReadme(path, name) {
|
|
return true
|
|
}
|
|
|
|
if f.OmitTest && IsTestFile(path, name) {
|
|
return true
|
|
}
|
|
|
|
ext := filepath.Ext(path)
|
|
for _, bad := range f.Extensions {
|
|
if strings.EqualFold(ext, bad) {
|
|
return true
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func IsNonReadmeMarkdown(path, name string) bool {
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
if ext != ".md" && ext != ".markdown" {
|
|
return false
|
|
}
|
|
return !strings.EqualFold(name, "README.md") && !strings.EqualFold(name, "README.markdown")
|
|
}
|
|
|
|
func IsFrontendOrReadme(path, name string) bool {
|
|
if IsFrontendExt(path) {
|
|
return true
|
|
}
|
|
return strings.EqualFold(name, "README.md")
|
|
}
|
|
|
|
func IsFrontendExt(path string) bool {
|
|
ext := strings.ToLower(filepath.Ext(path))
|
|
for _, fe := range config.FrontendExts {
|
|
if strings.EqualFold(ext, fe) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsTestDir(path string) bool {
|
|
slashPath := filepath.ToSlash(path)
|
|
return strings.Contains(slashPath, "/tests/") || strings.HasPrefix(slashPath, "tests/")
|
|
}
|
|
|
|
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/")
|
|
case ".py":
|
|
lower := strings.ToLower(name)
|
|
if strings.HasPrefix(lower, "test_") || strings.HasSuffix(lower, "_test.py") {
|
|
return true
|
|
}
|
|
if strings.EqualFold(name, "conftest.py") {
|
|
return true
|
|
}
|
|
return IsTestDir(path)
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func IsSymlink(path string) bool {
|
|
info, err := os.Lstat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return info.Mode()&os.ModeSymlink != 0
|
|
}
|