2024-06-18 18:21:03 -04:00
|
|
|
// Package filter filters files based on specific critera
|
|
|
|
package filter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"slices"
|
2024-06-19 22:13:10 -04:00
|
|
|
"strings"
|
2024-06-18 18:21:03 -04:00
|
|
|
"time"
|
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
"github.com/dustin/go-humanize"
|
2024-06-18 18:21:03 -04:00
|
|
|
"github.com/ijt/go-anytime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Filter struct {
|
2024-06-19 22:13:10 -04:00
|
|
|
on, before, after time.Time
|
|
|
|
glob, pattern string
|
|
|
|
unglob, unpattern string
|
|
|
|
filenames []string
|
|
|
|
dirsonly, filesonly bool
|
2024-06-30 18:59:14 -04:00
|
|
|
showhidden bool
|
2024-06-19 22:13:10 -04:00
|
|
|
matcher *regexp.Regexp
|
|
|
|
unmatcher *regexp.Regexp
|
2024-06-30 21:39:23 -04:00
|
|
|
minsize, maxsize int64
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) On() time.Time { return f.on }
|
|
|
|
func (f *Filter) After() time.Time { return f.after }
|
|
|
|
func (f *Filter) Before() time.Time { return f.before }
|
|
|
|
func (f *Filter) Glob() string { return f.glob }
|
|
|
|
func (f *Filter) Pattern() string { return f.pattern }
|
|
|
|
func (f *Filter) FileNames() []string { return f.filenames }
|
2024-06-19 22:13:10 -04:00
|
|
|
func (f *Filter) FilesOnly() bool { return f.filesonly }
|
|
|
|
func (f *Filter) DirsOnly() bool { return f.dirsonly }
|
2024-06-30 18:59:14 -04:00
|
|
|
func (f *Filter) ShowHidden() bool { return f.showhidden }
|
2024-06-30 21:39:23 -04:00
|
|
|
func (f *Filter) MinSize() int64 { return f.minsize }
|
|
|
|
func (f *Filter) MaxSize() int64 { return f.maxsize }
|
2024-06-18 18:21:03 -04:00
|
|
|
|
2024-06-19 20:14:50 -04:00
|
|
|
func (f *Filter) AddFileName(filename string) {
|
|
|
|
filename = filepath.Clean(filename)
|
|
|
|
f.filenames = append(f.filenames, filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) AddFileNames(filenames ...string) {
|
|
|
|
for _, filename := range filenames {
|
|
|
|
f.AddFileName(filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
func (f *Filter) Match(filename string, modified time.Time, size int64, isdir bool) bool {
|
2024-06-19 20:14:50 -04:00
|
|
|
// this might be unnessary but w/e
|
|
|
|
filename = filepath.Clean(filename)
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
// on or before/after, not both
|
|
|
|
if !f.on.IsZero() {
|
|
|
|
if !same_day(f.on, modified) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !f.after.IsZero() && f.after.After(modified) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !f.before.IsZero() && f.before.Before(modified) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
if f.has_regex() && !f.matcher.MatchString(filename) {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
if f.glob != "" {
|
|
|
|
if match, err := filepath.Match(f.glob, filename); err != nil || !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
if f.has_unregex() && f.unmatcher.MatchString(filename) {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
if f.unglob != "" {
|
|
|
|
if match, err := filepath.Match(f.unglob, filename); err != nil || match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
if len(f.filenames) > 0 && !slices.Contains(f.filenames, filename) {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-19 22:13:10 -04:00
|
|
|
if f.filesonly && isdir {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-19 22:13:10 -04:00
|
|
|
if f.dirsonly && !isdir {
|
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-30 18:59:14 -04:00
|
|
|
if !f.showhidden && strings.HasPrefix(filename, ".") {
|
2024-06-19 22:13:10 -04:00
|
|
|
return false
|
|
|
|
}
|
2024-06-30 21:17:42 -04:00
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
if f.maxsize != 0 && f.maxsize < size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.minsize != 0 && f.minsize > size {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
// okay it was good
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) SetPattern(pattern string) error {
|
|
|
|
var err error
|
|
|
|
f.pattern = pattern
|
|
|
|
f.matcher, err = regexp.Compile(f.pattern)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
func (f *Filter) SetUnPattern(unpattern string) error {
|
|
|
|
var err error
|
|
|
|
f.unpattern = unpattern
|
|
|
|
f.unmatcher, err = regexp.Compile(f.unpattern)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:21:03 -04:00
|
|
|
func (f *Filter) Blank() bool {
|
|
|
|
t := time.Time{}
|
|
|
|
return !f.has_regex() &&
|
2024-06-18 18:56:55 -04:00
|
|
|
!f.has_unregex() &&
|
2024-06-18 18:21:03 -04:00
|
|
|
f.glob == "" &&
|
2024-06-18 18:56:55 -04:00
|
|
|
f.unglob == "" &&
|
2024-06-18 18:21:03 -04:00
|
|
|
f.after.Equal(t) &&
|
|
|
|
f.before.Equal(t) &&
|
|
|
|
f.on.Equal(t) &&
|
2024-06-19 22:13:10 -04:00
|
|
|
len(f.filenames) == 0 &&
|
2024-06-30 18:59:14 -04:00
|
|
|
!f.showhidden &&
|
2024-06-19 22:13:10 -04:00
|
|
|
!f.filesonly &&
|
2024-06-30 21:39:23 -04:00
|
|
|
!f.dirsonly &&
|
|
|
|
f.minsize == 0 &&
|
|
|
|
f.maxsize == 0
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) String() string {
|
2024-06-18 18:56:55 -04:00
|
|
|
var m, unm string
|
2024-06-18 18:21:03 -04:00
|
|
|
if f.matcher != nil {
|
|
|
|
m = f.matcher.String()
|
|
|
|
}
|
2024-06-18 18:56:55 -04:00
|
|
|
if f.unmatcher != nil {
|
|
|
|
unm = f.unmatcher.String()
|
|
|
|
}
|
2024-06-19 22:13:10 -04:00
|
|
|
return fmt.Sprintf("on:'%s' before:'%s' after:'%s' glob:'%s' regex:'%s' unglob:'%s' "+
|
|
|
|
"unregex:'%s' filenames:'%v' filesonly:'%t' dirsonly:'%t' ignorehidden:'%t'",
|
2024-06-18 18:21:03 -04:00
|
|
|
f.on, f.before, f.after,
|
2024-06-18 18:56:55 -04:00
|
|
|
f.glob, m,
|
|
|
|
f.unglob, unm,
|
|
|
|
f.filenames,
|
2024-06-19 22:13:10 -04:00
|
|
|
f.filesonly, f.dirsonly,
|
2024-06-30 18:59:14 -04:00
|
|
|
f.showhidden,
|
2024-06-18 18:21:03 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filter) has_regex() bool {
|
|
|
|
if f.matcher == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return f.matcher.String() != ""
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
func (f *Filter) has_unregex() bool {
|
|
|
|
if f.unmatcher == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return f.unmatcher.String() != ""
|
|
|
|
}
|
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly, dirsonly, showhidden bool, minsize, maxsize string, names ...string) (*Filter, error) {
|
2024-06-18 18:21:03 -04:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
now = time.Now()
|
|
|
|
)
|
|
|
|
|
|
|
|
f := &Filter{
|
2024-06-30 18:59:14 -04:00
|
|
|
glob: glob,
|
|
|
|
unglob: unglob,
|
|
|
|
filesonly: filesonly,
|
|
|
|
dirsonly: dirsonly,
|
|
|
|
showhidden: showhidden,
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-06-19 20:14:50 -04:00
|
|
|
f.AddFileNames(names...)
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
if on != "" {
|
|
|
|
o, err := anytime.Parse(on, now)
|
2024-06-18 18:21:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return &Filter{}, err
|
|
|
|
}
|
2024-06-18 18:56:55 -04:00
|
|
|
f.on = o
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
if after != "" {
|
|
|
|
a, err := anytime.Parse(after, now)
|
2024-06-18 18:21:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return &Filter{}, err
|
|
|
|
}
|
2024-06-18 18:56:55 -04:00
|
|
|
f.after = a
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
if before != "" {
|
|
|
|
b, err := anytime.Parse(before, now)
|
2024-06-18 18:21:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return &Filter{}, err
|
|
|
|
}
|
2024-06-18 18:56:55 -04:00
|
|
|
f.before = b
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
err = f.SetPattern(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = f.SetUnPattern(unpattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-18 18:21:03 -04:00
|
|
|
|
2024-06-30 21:39:23 -04:00
|
|
|
if minsize != "" {
|
|
|
|
m, e := humanize.ParseBytes(minsize)
|
|
|
|
if e != nil {
|
|
|
|
log.Errorf("invalid input size '%s'", minsize)
|
|
|
|
}
|
|
|
|
f.minsize = int64(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
if maxsize != "" {
|
|
|
|
m, e := humanize.ParseBytes(maxsize)
|
|
|
|
if e != nil {
|
|
|
|
log.Errorf("invalid input size '%s'", maxsize)
|
|
|
|
}
|
|
|
|
f.maxsize = int64(m)
|
|
|
|
}
|
|
|
|
|
2024-06-18 18:56:55 -04:00
|
|
|
return f, nil
|
2024-06-18 18:21:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func same_day(a, b time.Time) bool {
|
|
|
|
ay, am, ad := a.Date()
|
|
|
|
by, bm, bd := b.Date()
|
|
|
|
return ay == by && am == bm && ad == bd
|
|
|
|
}
|