2023-09-11 17:09:08 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return len(filters.included) != 0
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) includes() string {
|
|
|
|
return strings.Join(filters.included, ",")
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) hasExcludes() bool {
|
|
|
|
return len(filters.excluded) != 0
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func (filters *filters) excludes() string {
|
|
|
|
return strings.Join(filters.excluded, ",")
|
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() {
|
|
|
|
for _, exclude := range filters.excluded {
|
2023-09-26 21:37:28 +00:00
|
|
|
result = slices.DeleteFunc(result, func(s string) bool {
|
2023-09-25 22:13:31 +00:00
|
|
|
if CaseSensitive {
|
2023-09-26 21:37:28 +00:00
|
|
|
return strings.Contains(s, filepath.Base(exclude))
|
2023-09-25 22:13:31 +00:00
|
|
|
} else {
|
2023-09-26 21:37:28 +00:00
|
|
|
return strings.Contains(strings.ToLower(s), strings.ToLower(filepath.Base(exclude)))
|
2023-09-25 22:13:31 +00:00
|
|
|
}
|
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 {
|
|
|
|
var delete bool = true
|
|
|
|
|
|
|
|
p := filepath.Base(s)
|
|
|
|
|
|
|
|
for _, include := range filters.included {
|
|
|
|
if (CaseSensitive && strings.Contains(p, include)) || (!CaseSensitive && strings.Contains(strings.ToLower(p), strings.ToLower(include))) {
|
|
|
|
delete = false
|
2023-09-25 22:13:31 +00:00
|
|
|
}
|
2023-09-26 21:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return delete
|
|
|
|
})
|
2023-09-15 06:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|