2023-09-11 17:09:08 +00:00
|
|
|
/*
|
2024-01-14 18:39:14 +00:00
|
|
|
Copyright © 2024 Seednode <seednode@seedno.de>
|
2023-09-11 17:09:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
2023-09-15 06:06:52 +00:00
|
|
|
import (
|
2023-09-26 21:37:28 +00:00
|
|
|
"path/filepath"
|
2023-09-15 06:06:52 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
)
|
2023-09-11 17:09:08 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
type filters struct {
|
|
|
|
included []string
|
|
|
|
excluded []string
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) isEmpty() bool {
|
|
|
|
return !(filters.hasIncludes() || filters.hasExcludes())
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) hasIncludes() bool {
|
2023-11-05 20:03:34 +00:00
|
|
|
return len(filters.included) != 0 && Filtering
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) hasExcludes() bool {
|
2023-11-05 20:03:34 +00:00
|
|
|
return len(filters.excluded) != 0 && Filtering
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 06:06:52 +00:00
|
|
|
func (filters *filters) apply(fileList []string) []string {
|
|
|
|
result := make([]string, len(fileList))
|
|
|
|
|
|
|
|
copy(result, fileList)
|
|
|
|
|
|
|
|
if filters.hasExcludes() {
|
2023-11-05 20:03:34 +00:00
|
|
|
result = slices.DeleteFunc(result, func(s string) bool {
|
|
|
|
p := filepath.Base(s)
|
|
|
|
|
|
|
|
for _, exclude := range filters.excluded {
|
2024-01-19 20:39:53 +00:00
|
|
|
if (!CaseInsensitive && strings.Contains(p, exclude)) || (CaseInsensitive && strings.Contains(strings.ToLower(p), strings.ToLower(exclude))) {
|
2023-11-05 20:03:34 +00:00
|
|
|
return true
|
2023-09-25 22:13:31 +00:00
|
|
|
}
|
2023-11-05 20:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
2023-09-15 06:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filters.hasIncludes() {
|
2023-09-26 21:37:28 +00:00
|
|
|
result = slices.DeleteFunc(result, func(s string) bool {
|
|
|
|
p := filepath.Base(s)
|
|
|
|
|
|
|
|
for _, include := range filters.included {
|
2024-01-19 20:39:53 +00:00
|
|
|
if (!CaseInsensitive && strings.Contains(p, include)) || (CaseInsensitive && strings.Contains(strings.ToLower(p), strings.ToLower(include))) {
|
2023-11-05 20:03:34 +00:00
|
|
|
return false
|
2023-09-25 22:13:31 +00:00
|
|
|
}
|
2023-09-26 21:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 20:03:34 +00:00
|
|
|
return true
|
2023-09-26 21:37:28 +00:00
|
|
|
})
|
2023-09-15 06:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|