add min and max size filters
parsed by humanize
This commit is contained in:
parent
75370f719a
commit
a5654af0ff
|
@ -116,7 +116,7 @@ func walk_dir(dir string, f *filter.Filter) (files Files) {
|
||||||
|
|
||||||
name := d.Name()
|
name := d.Name()
|
||||||
info, _ := d.Info()
|
info, _ := d.Info()
|
||||||
if f.Match(name, info.ModTime(), info.IsDir()) {
|
if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) {
|
||||||
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
||||||
i, err := os.Stat(p)
|
i, err := os.Stat(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -161,7 +161,7 @@ func read_dir(dir string, f *filter.Filter) (files Files) {
|
||||||
|
|
||||||
path := filepath.Dir(filepath.Join(dir, name))
|
path := filepath.Dir(filepath.Join(dir, name))
|
||||||
|
|
||||||
if f.Match(name, info.ModTime(), info.IsDir()) {
|
if f.Match(name, info.ModTime(), info.Size(), info.IsDir()) {
|
||||||
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
log.Debugf("found matching file: %s %s", name, info.ModTime())
|
||||||
files = append(files, File{
|
files = append(files, File{
|
||||||
name: name,
|
name: name,
|
||||||
|
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/log"
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/ijt/go-anytime"
|
"github.com/ijt/go-anytime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,6 +23,7 @@ type Filter struct {
|
||||||
showhidden bool
|
showhidden bool
|
||||||
matcher *regexp.Regexp
|
matcher *regexp.Regexp
|
||||||
unmatcher *regexp.Regexp
|
unmatcher *regexp.Regexp
|
||||||
|
minsize, maxsize int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filter) On() time.Time { return f.on }
|
func (f *Filter) On() time.Time { return f.on }
|
||||||
|
@ -32,6 +35,8 @@ func (f *Filter) FileNames() []string { return f.filenames }
|
||||||
func (f *Filter) FilesOnly() bool { return f.filesonly }
|
func (f *Filter) FilesOnly() bool { return f.filesonly }
|
||||||
func (f *Filter) DirsOnly() bool { return f.dirsonly }
|
func (f *Filter) DirsOnly() bool { return f.dirsonly }
|
||||||
func (f *Filter) ShowHidden() bool { return f.showhidden }
|
func (f *Filter) ShowHidden() bool { return f.showhidden }
|
||||||
|
func (f *Filter) MinSize() int64 { return f.minsize }
|
||||||
|
func (f *Filter) MaxSize() int64 { return f.maxsize }
|
||||||
|
|
||||||
func (f *Filter) AddFileName(filename string) {
|
func (f *Filter) AddFileName(filename string) {
|
||||||
filename = filepath.Clean(filename)
|
filename = filepath.Clean(filename)
|
||||||
|
@ -44,7 +49,7 @@ func (f *Filter) AddFileNames(filenames ...string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filter) Match(filename string, modified time.Time, isdir bool) bool {
|
func (f *Filter) Match(filename string, modified time.Time, size int64, isdir bool) bool {
|
||||||
// this might be unnessary but w/e
|
// this might be unnessary but w/e
|
||||||
filename = filepath.Clean(filename)
|
filename = filepath.Clean(filename)
|
||||||
|
|
||||||
|
@ -98,6 +103,14 @@ func (f *Filter) Match(filename string, modified time.Time, isdir bool) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if f.maxsize != 0 && f.maxsize < size {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if f.minsize != 0 && f.minsize > size {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// okay it was good
|
// okay it was good
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -128,7 +141,9 @@ func (f *Filter) Blank() bool {
|
||||||
len(f.filenames) == 0 &&
|
len(f.filenames) == 0 &&
|
||||||
!f.showhidden &&
|
!f.showhidden &&
|
||||||
!f.filesonly &&
|
!f.filesonly &&
|
||||||
!f.dirsonly
|
!f.dirsonly &&
|
||||||
|
f.minsize == 0 &&
|
||||||
|
f.maxsize == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Filter) String() string {
|
func (f *Filter) String() string {
|
||||||
|
@ -164,7 +179,7 @@ func (f *Filter) has_unregex() bool {
|
||||||
return f.unmatcher.String() != ""
|
return f.unmatcher.String() != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly, dirsonly, showhidden bool, names ...string) (*Filter, error) {
|
func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly, dirsonly, showhidden bool, minsize, maxsize string, names ...string) (*Filter, error) {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
|
@ -213,6 +228,22 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package filter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -28,14 +29,16 @@ type testholder struct {
|
||||||
filesonly, dirsonly bool
|
filesonly, dirsonly bool
|
||||||
showhidden bool
|
showhidden bool
|
||||||
good, bad []singletest
|
good, bad []singletest
|
||||||
|
minsize, maxsize string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t testholder) String() string {
|
func (t testholder) String() string {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"pattern:'%s' glob:'%s' unpattern:'%s' unglob:'%s' filenames:'%v' "+
|
"pattern:'%s' glob:'%s' unpattern:'%s' unglob:'%s' filenames:'%v' "+
|
||||||
"before:'%s' after:'%s' on:'%s' filesonly:'%t' dirsonly:'%t' showhidden:'%t'",
|
"before:'%s' after:'%s' on:'%s' filesonly:'%t' dirsonly:'%t' "+
|
||||||
|
"showhidden:'%t' minsize:'%s' maxsize:'%s'",
|
||||||
t.pattern, t.glob, t.unpattern, t.unglob, t.filenames, t.before, t.after, t.on,
|
t.pattern, t.glob, t.unpattern, t.unglob, t.filenames, t.before, t.after, t.on,
|
||||||
t.filesonly, t.dirsonly, t.showhidden,
|
t.filesonly, t.dirsonly, t.showhidden, t.minsize, t.maxsize,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,10 +46,11 @@ type singletest struct {
|
||||||
filename string
|
filename string
|
||||||
isdir bool
|
isdir bool
|
||||||
modified time.Time
|
modified time.Time
|
||||||
|
size int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s singletest) String() string {
|
func (s singletest) String() string {
|
||||||
return fmt.Sprintf("filename:'%s' modified:'%s' isdir:'%t'", s.filename, s.modified, s.isdir)
|
return fmt.Sprintf("filename:'%s' modified:'%s' size:'%d' isdir:'%t'", s.filename, s.modified, s.size, s.isdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testmatch(t *testing.T, testers []testholder) {
|
func testmatch(t *testing.T, testers []testholder) {
|
||||||
|
@ -58,7 +62,8 @@ func testmatch(t *testing.T, testers []testholder) {
|
||||||
for _, tester := range testers {
|
for _, tester := range testers {
|
||||||
f, err = New(
|
f, err = New(
|
||||||
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
|
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
|
||||||
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.showhidden,
|
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly,
|
||||||
|
tester.showhidden, tester.minsize, tester.maxsize,
|
||||||
tester.filenames...,
|
tester.filenames...,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -67,50 +72,42 @@ func testmatch(t *testing.T, testers []testholder) {
|
||||||
|
|
||||||
for _, tst := range tester.good {
|
for _, tst := range tester.good {
|
||||||
t.Run(fmt.Sprintf(testnamefmt+"_good", tst.filename, tst.modified), func(t *testing.T) {
|
t.Run(fmt.Sprintf(testnamefmt+"_good", tst.filename, tst.modified), func(t *testing.T) {
|
||||||
if !f.Match(tst.filename, tst.modified, tst.isdir) {
|
if !f.Match(tst.filename, tst.modified, tst.size, tst.isdir) {
|
||||||
t.Fatalf("(filename:%s modified:%s) didn't match (%s) but should have", tst.filename, tst.modified, tester)
|
t.Fatalf("(%s) didn't match (%s) but should have", tst, tester)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tst := range tester.bad {
|
for _, tst := range tester.bad {
|
||||||
t.Run(fmt.Sprintf(testnamefmt+"_bad", tst.filename, tst.modified), func(t *testing.T) {
|
t.Run(fmt.Sprintf(testnamefmt+"_bad", tst.filename, tst.modified), func(t *testing.T) {
|
||||||
if f.Match(tst.filename, tst.modified, tst.isdir) {
|
if f.Match(tst.filename, tst.modified, tst.size, tst.isdir) {
|
||||||
t.Fatalf("(filename:%s modified:%s) matched (%s) but shouldn't have", tst.filename, tst.modified, tester)
|
t.Fatalf("(%s) matched (%s) but shouldn't have", tst, tester)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func blankfilename(times ...time.Time) []singletest {
|
func blanktime(dir bool, names ...string) []singletest {
|
||||||
|
out := make([]singletest, 0, len(names))
|
||||||
|
for _, name := range names {
|
||||||
|
out = append(out, singletest{filename: name, modified: time.Time{}, isdir: dir, size: 0})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func blankname(dir bool, times ...time.Time) []singletest {
|
||||||
out := make([]singletest, 0, len(times))
|
out := make([]singletest, 0, len(times))
|
||||||
for _, time := range times {
|
for _, time := range times {
|
||||||
out = append(out, singletest{filename: "blank.txt", modified: time, isdir: false})
|
out = append(out, singletest{filename: "blank.txt", modified: time, isdir: dir, size: 0})
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func blankdirname(times ...time.Time) []singletest {
|
func sizeonly(dir bool, sizes ...int64) []singletest {
|
||||||
out := make([]singletest, 0, len(times))
|
out := make([]singletest, 0, len(sizes))
|
||||||
for _, time := range times {
|
for _, size := range sizes {
|
||||||
out = append(out, singletest{filename: "blank", modified: time, isdir: true})
|
out = append(out, singletest{filename: "blank", modified: time.Time{}, isdir: dir, size: size})
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func blanktimefile(filenames ...string) []singletest {
|
|
||||||
out := make([]singletest, 0, len(filenames))
|
|
||||||
for _, filename := range filenames {
|
|
||||||
out = append(out, singletest{filename: filename, modified: time.Time{}, isdir: false})
|
|
||||||
}
|
|
||||||
return out
|
|
||||||
}
|
|
||||||
|
|
||||||
func blanktimedir(dirnames ...string) []singletest {
|
|
||||||
out := make([]singletest, 0, len(dirnames))
|
|
||||||
for _, dirname := range dirnames {
|
|
||||||
out = append(out, singletest{filename: dirname, modified: time.Time{}, isdir: true})
|
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
@ -119,43 +116,43 @@ func TestFilterOn(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
on: "2024-02-14",
|
on: "2024-02-14",
|
||||||
good: blankfilename(time.Date(2024, 2, 14, 12, 0, 0, 0, time.Local)),
|
good: blankname(false, time.Date(2024, 2, 14, 12, 0, 0, 0, time.Local)),
|
||||||
bad: blankfilename(now, now.Add(time.Hour*72), now.Add(-time.Hour*18)),
|
bad: blankname(false, now, now.Add(time.Hour*72), now.Add(-time.Hour*18)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "yesterday",
|
on: "yesterday",
|
||||||
good: blankfilename(yesterday),
|
good: blankname(false, yesterday),
|
||||||
bad: blankfilename(now, oneweekago, onemonthago, oneyearago, twoweeksago, twomonthsago, twoyearsago),
|
bad: blankname(false, now, oneweekago, onemonthago, oneyearago, twoweeksago, twomonthsago, twoyearsago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "one week ago",
|
on: "one week ago",
|
||||||
good: blankfilename(oneweekago),
|
good: blankname(false, oneweekago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "one month ago",
|
on: "one month ago",
|
||||||
good: blankfilename(onemonthago),
|
good: blankname(false, onemonthago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "two months ago",
|
on: "two months ago",
|
||||||
good: blankfilename(twomonthsago),
|
good: blankname(false, twomonthsago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "four months ago",
|
on: "four months ago",
|
||||||
good: blankfilename(fourmonthsago),
|
good: blankname(false, fourmonthsago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "one year ago",
|
on: "one year ago",
|
||||||
good: blankfilename(oneyearago),
|
good: blankname(false, oneyearago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
on: "four years ago",
|
on: "four years ago",
|
||||||
good: blankfilename(fouryearsago),
|
good: blankname(false, fouryearsago),
|
||||||
bad: blankfilename(now),
|
bad: blankname(false, now),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -164,43 +161,43 @@ func TestFilterAfter(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
after: "2020-02-14",
|
after: "2020-02-14",
|
||||||
good: blankfilename(time.Date(2024, 3, 14, 12, 0, 0, 0, time.Local), now, yesterday),
|
good: blankname(false, time.Date(2024, 3, 14, 12, 0, 0, 0, time.Local), now, yesterday),
|
||||||
bad: blankfilename(time.Date(2018, 2, 14, 12, 0, 0, 0, time.Local)),
|
bad: blankname(false, time.Date(2018, 2, 14, 12, 0, 0, 0, time.Local)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "yesterday",
|
after: "yesterday",
|
||||||
good: blankfilename(yesterday, yesterday.AddDate(1, 0, 0), now, now.AddDate(0, 3, 0)),
|
good: blankname(false, yesterday, yesterday.AddDate(1, 0, 0), now, now.AddDate(0, 3, 0)),
|
||||||
bad: blankfilename(yesterday.AddDate(-1, 0, 0), yesterday.AddDate(0, 0, -1), ereyesterday),
|
bad: blankname(false, yesterday.AddDate(-1, 0, 0), yesterday.AddDate(0, 0, -1), ereyesterday),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "one week ago",
|
after: "one week ago",
|
||||||
good: blankfilename(now),
|
good: blankname(false, now),
|
||||||
bad: blankfilename(oneweekago.AddDate(0, 0, -1)),
|
bad: blankname(false, oneweekago.AddDate(0, 0, -1)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "one month ago",
|
after: "one month ago",
|
||||||
good: blankfilename(now, oneweekago, twoweeksago),
|
good: blankname(false, now, oneweekago, twoweeksago),
|
||||||
bad: blankfilename(onemonthago, twomonthsago, fourmonthsago, oneyearago),
|
bad: blankname(false, onemonthago, twomonthsago, fourmonthsago, oneyearago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "two months ago",
|
after: "two months ago",
|
||||||
good: blankfilename(now, onemonthago, oneweekago),
|
good: blankname(false, now, onemonthago, oneweekago),
|
||||||
bad: blankfilename(twomonthsago, oneyearago, fourmonthsago),
|
bad: blankname(false, twomonthsago, oneyearago, fourmonthsago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "four months ago",
|
after: "four months ago",
|
||||||
good: blankfilename(now, oneweekago, onemonthago, twoweeksago, twomonthsago, onemonthago),
|
good: blankname(false, now, oneweekago, onemonthago, twoweeksago, twomonthsago, onemonthago),
|
||||||
bad: blankfilename(fourmonthsago, oneyearago),
|
bad: blankname(false, fourmonthsago, oneyearago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "one year ago",
|
after: "one year ago",
|
||||||
good: blankfilename(now, onemonthago, twomonthsago, fourmonthsago),
|
good: blankname(false, now, onemonthago, twomonthsago, fourmonthsago),
|
||||||
bad: blankfilename(oneyearago, fouryearsago, twoyearsago),
|
bad: blankname(false, oneyearago, fouryearsago, twoyearsago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
after: "four years ago",
|
after: "four years ago",
|
||||||
good: blankfilename(now, twoyearsago, onemonthago, fourmonthsago),
|
good: blankname(false, now, twoyearsago, onemonthago, fourmonthsago),
|
||||||
bad: blankfilename(fouryearsago, fouryearsago.AddDate(-1, 0, 0)),
|
bad: blankname(false, fouryearsago, fouryearsago.AddDate(-1, 0, 0)),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -209,43 +206,43 @@ func TestFilterBefore(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
before: "2024-02-14",
|
before: "2024-02-14",
|
||||||
good: blankfilename(time.Date(2020, 2, 14, 12, 0, 0, 0, time.Local), time.Date(1989, 8, 13, 18, 53, 0, 0, time.Local)),
|
good: blankname(false, time.Date(2020, 2, 14, 12, 0, 0, 0, time.Local), time.Date(1989, 8, 13, 18, 53, 0, 0, time.Local)),
|
||||||
bad: blankfilename(now, now.AddDate(0, 0, 10), now.AddDate(0, -2, 0)),
|
bad: blankname(false, now, now.AddDate(0, 0, 10), now.AddDate(0, -2, 0)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "yesterday",
|
before: "yesterday",
|
||||||
good: blankfilename(onemonthago, oneweekago, oneyearago),
|
good: blankname(false, onemonthago, oneweekago, oneyearago),
|
||||||
bad: blankfilename(now, now.AddDate(0, 0, 1)),
|
bad: blankname(false, now, now.AddDate(0, 0, 1)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "one week ago",
|
before: "one week ago",
|
||||||
good: blankfilename(onemonthago, oneyearago, twoweeksago),
|
good: blankname(false, onemonthago, oneyearago, twoweeksago),
|
||||||
bad: blankfilename(yesterday, now),
|
bad: blankname(false, yesterday, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "one month ago",
|
before: "one month ago",
|
||||||
good: blankfilename(oneyearago, twomonthsago),
|
good: blankname(false, oneyearago, twomonthsago),
|
||||||
bad: blankfilename(oneweekago, yesterday, now),
|
bad: blankname(false, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "two months ago",
|
before: "two months ago",
|
||||||
good: blankfilename(fourmonthsago, oneyearago),
|
good: blankname(false, fourmonthsago, oneyearago),
|
||||||
bad: blankfilename(onemonthago, oneweekago, yesterday, now),
|
bad: blankname(false, onemonthago, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "four months ago",
|
before: "four months ago",
|
||||||
good: blankfilename(oneyearago, twoyearsago, fouryearsago),
|
good: blankname(false, oneyearago, twoyearsago, fouryearsago),
|
||||||
bad: blankfilename(twomonthsago, onemonthago, oneweekago, yesterday, now),
|
bad: blankname(false, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "one year ago",
|
before: "one year ago",
|
||||||
good: blankfilename(twoyearsago, fouryearsago),
|
good: blankname(false, twoyearsago, fouryearsago),
|
||||||
bad: blankfilename(fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
bad: blankname(false, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
before: "four years ago",
|
before: "four years ago",
|
||||||
good: blankfilename(fouryearsago.AddDate(-1, 0, 0), fouryearsago.AddDate(-4, 0, 0)),
|
good: blankname(false, fouryearsago.AddDate(-1, 0, 0), fouryearsago.AddDate(-4, 0, 0)),
|
||||||
bad: blankfilename(oneyearago, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
bad: blankname(false, oneyearago, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -254,13 +251,13 @@ func TestFilterMatch(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
pattern: "[Tt]est",
|
pattern: "[Tt]est",
|
||||||
good: blanktimefile("test", "Test"),
|
good: blanktime(false, "test", "Test"),
|
||||||
bad: blanktimefile("TEST", "tEst", "tEST", "TEst"),
|
bad: blanktime(false, "TEST", "tEst", "tEST", "TEst"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
pattern: "^h.*o$",
|
pattern: "^h.*o$",
|
||||||
good: blanktimefile("hello", "hippo", "how about some pasta with alfredo"),
|
good: blanktime(false, "hello", "hippo", "how about some pasta with alfredo"),
|
||||||
bad: blanktimefile("hi", "test", "hellO", "Hello", "oh hello there"),
|
bad: blanktime(false, "hi", "test", "hellO", "Hello", "oh hello there"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -269,23 +266,23 @@ func TestFilterGlob(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
glob: "*.txt",
|
glob: "*.txt",
|
||||||
good: blanktimefile("test.txt", "alsotest.txt"),
|
good: blanktime(false, "test.txt", "alsotest.txt"),
|
||||||
bad: blanktimefile("test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
bad: blanktime(false, "test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
glob: "*.tar.*",
|
glob: "*.tar.*",
|
||||||
good: blanktimefile("test.tar.gz", "test.tar.xz", "test.tar.zst", "test.tar.bz2"),
|
good: blanktime(false, "test.tar.gz", "test.tar.xz", "test.tar.zst", "test.tar.bz2"),
|
||||||
bad: blanktimefile("test.tar", "test.txt", "test.targz", "test.tgz"),
|
bad: blanktime(false, "test.tar", "test.txt", "test.targz", "test.tgz"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
glob: "pot*o",
|
glob: "pot*o",
|
||||||
good: blanktimefile("potato", "potdonkeyo", "potesto"),
|
good: blanktime(false, "potato", "potdonkeyo", "potesto"),
|
||||||
bad: blanktimefile("salad", "test", "alsotest"),
|
bad: blanktime(false, "salad", "test", "alsotest"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
glob: "t?st",
|
glob: "t?st",
|
||||||
good: blanktimefile("test", "tast", "tfst", "tnst"),
|
good: blanktime(false, "test", "tast", "tfst", "tnst"),
|
||||||
bad: blanktimefile("best", "fast", "most", "past"),
|
bad: blanktime(false, "best", "fast", "most", "past"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -294,13 +291,13 @@ func TestFilterUnMatch(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
unpattern: "^ss_.*\\.zip",
|
unpattern: "^ss_.*\\.zip",
|
||||||
good: blanktimefile("hello.zip", "ss_potato.png", "sss.zip"),
|
good: blanktime(false, "hello.zip", "ss_potato.png", "sss.zip"),
|
||||||
bad: blanktimefile("ss_ost_flac.zip", "ss_guide.zip", "ss_controls.zip"),
|
bad: blanktime(false, "ss_ost_flac.zip", "ss_guide.zip", "ss_controls.zip"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
unpattern: "^h.*o$",
|
unpattern: "^h.*o$",
|
||||||
good: blanktimefile("hi", "test", "hellO", "Hello", "oh hello there"),
|
good: blanktime(false, "hi", "test", "hellO", "Hello", "oh hello there"),
|
||||||
bad: blanktimefile("hello", "hippo", "how about some pasta with alfredo"),
|
bad: blanktime(false, "hello", "hippo", "how about some pasta with alfredo"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -309,23 +306,23 @@ func TestFilterUnGlob(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
unglob: "*.txt",
|
unglob: "*.txt",
|
||||||
good: blanktimefile("test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
good: blanktime(false, "test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||||
bad: blanktimefile("test.txt", "alsotest.txt"),
|
bad: blanktime(false, "test.txt", "alsotest.txt"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
unglob: "*.tar.*",
|
unglob: "*.tar.*",
|
||||||
good: blanktimefile("test.tar", "test.txt", "test.targz", "test.tgz"),
|
good: blanktime(false, "test.tar", "test.txt", "test.targz", "test.tgz"),
|
||||||
bad: blanktimefile("test.tar.gz", "test.tar.xz", "test.tar.zst", "test.tar.bz2"),
|
bad: blanktime(false, "test.tar.gz", "test.tar.xz", "test.tar.zst", "test.tar.bz2"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
unglob: "pot*o",
|
unglob: "pot*o",
|
||||||
good: blanktimefile("salad", "test", "alsotest"),
|
good: blanktime(false, "salad", "test", "alsotest"),
|
||||||
bad: blanktimefile("potato", "potdonkeyo", "potesto"),
|
bad: blanktime(false, "potato", "potdonkeyo", "potesto"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
unglob: "t?st",
|
unglob: "t?st",
|
||||||
good: blanktimefile("best", "fast", "most", "past"),
|
good: blanktime(false, "best", "fast", "most", "past"),
|
||||||
bad: blanktimefile("test", "tast", "tfst", "tnst"),
|
bad: blanktime(false, "test", "tast", "tfst", "tnst"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -334,18 +331,18 @@ func TestFilterFilenames(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
filenames: []string{"test.txt", "alsotest.txt"},
|
filenames: []string{"test.txt", "alsotest.txt"},
|
||||||
good: blanktimefile("test.txt", "alsotest.txt"),
|
good: blanktime(false, "test.txt", "alsotest.txt"),
|
||||||
bad: blanktimefile("test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
bad: blanktime(false, "test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filenames: []string{"test.md", "test.txt"},
|
filenames: []string{"test.md", "test.txt"},
|
||||||
good: blanktimefile("test.txt", "test.md"),
|
good: blanktime(false, "test.txt", "test.md"),
|
||||||
bad: blanktimefile("alsotest.txt", "test.go", "test.tar.gz", "testxt", "test.text"),
|
bad: blanktime(false, "alsotest.txt", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
filenames: []string{"hello.world"},
|
filenames: []string{"hello.world"},
|
||||||
good: blanktimefile("hello.world"),
|
good: blanktime(false, "hello.world"),
|
||||||
bad: blanktimefile("test.md", "test.go", "test.tar.gz", "testxt", "test.text", "helloworld", "Hello.world"),
|
bad: blanktime(false, "test.md", "test.go", "test.tar.gz", "testxt", "test.text", "helloworld", "Hello.world"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -354,8 +351,8 @@ func TestFilterFilesOnly(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
filesonly: true,
|
filesonly: true,
|
||||||
good: blanktimefile("test", "hellowold.txt", "test.md", "test.jpg"),
|
good: blanktime(false, "test", "hellowold.txt", "test.md", "test.jpg"),
|
||||||
bad: blanktimedir("test", "alsotest", "helloworld"),
|
bad: blanktime(true, "test", "alsotest", "helloworld"),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -364,13 +361,13 @@ func TestFilterDirsOnly(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
dirsonly: true,
|
dirsonly: true,
|
||||||
good: blanktimedir("test", "alsotest", "helloworld"),
|
good: blanktime(true, "test", "alsotest", "helloworld"),
|
||||||
bad: blanktimefile("test", "hellowold.txt", "test.md", "test.jpg"),
|
bad: blanktime(false, "test", "hellowold.txt", "test.md", "test.jpg"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
dirsonly: true,
|
dirsonly: true,
|
||||||
good: blankdirname(fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
good: blankname(true, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||||
bad: blankfilename(fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
bad: blankname(false, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -379,12 +376,27 @@ func TestFilterShowHidden(t *testing.T) {
|
||||||
testmatch(t, []testholder{
|
testmatch(t, []testholder{
|
||||||
{
|
{
|
||||||
showhidden: false,
|
showhidden: false,
|
||||||
good: append(blanktimedir("test", "alsotest", "helloworld"), blanktimefile("test", "alsotest", "helloworld")...),
|
good: append(blanktime(true, "test", "alsotest", "helloworld"), blanktime(false, "test", "alsotest", "helloworld")...),
|
||||||
bad: append(blanktimedir(".test", ".alsotest", ".helloworld"), blanktimefile(".test", ".alsotest", ".helloworld")...),
|
bad: append(blanktime(true, ".test", ".alsotest", ".helloworld"), blanktime(false, ".test", ".alsotest", ".helloworld")...),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
showhidden: true,
|
showhidden: true,
|
||||||
good: append(blanktimedir("test", "alsotest", ".helloworld"), blanktimefile("test", "alsotest", ".helloworld")...),
|
good: append(blanktime(true, "test", "alsotest", ".helloworld"), blanktime(false, "test", "alsotest", ".helloworld")...),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilesize(t *testing.T) {
|
||||||
|
testmatch(t, []testholder{
|
||||||
|
{
|
||||||
|
minsize: "9001B",
|
||||||
|
good: sizeonly(false, 10000, 9002, 424242, math.MaxInt64),
|
||||||
|
bad: sizeonly(false, 9000, math.MinInt64, 0, -9001),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
maxsize: "9001B",
|
||||||
|
good: sizeonly(false, 9000, math.MinInt64, 0, -9001),
|
||||||
|
bad: sizeonly(false, 10000, 9002, 424242, math.MaxInt64),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -432,8 +444,8 @@ func TestFilterMultipleParameters(t *testing.T) {
|
||||||
on: "today",
|
on: "today",
|
||||||
after: "two weeks ago",
|
after: "two weeks ago",
|
||||||
before: "one week ago",
|
before: "one week ago",
|
||||||
good: blankfilename(now, twoam, sevenam, threepm, tenpm),
|
good: blankname(false, now, twoam, sevenam, threepm, tenpm),
|
||||||
bad: blankfilename(yesterday, oneweekago, onemonthago, oneyearago),
|
bad: blankname(false, yesterday, oneweekago, onemonthago, oneyearago),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
unpattern: ".*\\.(jpg|png)",
|
unpattern: ".*\\.(jpg|png)",
|
||||||
|
@ -451,7 +463,7 @@ func TestFilterMultipleParameters(t *testing.T) {
|
||||||
{
|
{
|
||||||
filesonly: true,
|
filesonly: true,
|
||||||
unglob: "*.txt",
|
unglob: "*.txt",
|
||||||
good: blanktimefile("test.md", "test.jpg", "test.png"),
|
good: blanktime(false, "test.md", "test.jpg", "test.png"),
|
||||||
bad: []singletest{
|
bad: []singletest{
|
||||||
{
|
{
|
||||||
filename: "test",
|
filename: "test",
|
||||||
|
@ -470,7 +482,7 @@ func TestFilterMultipleParameters(t *testing.T) {
|
||||||
{
|
{
|
||||||
dirsonly: true,
|
dirsonly: true,
|
||||||
pattern: "w(or|ea)ld",
|
pattern: "w(or|ea)ld",
|
||||||
good: blanktimedir("hello world", "high weald"),
|
good: blanktime(true, "hello world", "high weald"),
|
||||||
bad: []singletest{
|
bad: []singletest{
|
||||||
{
|
{
|
||||||
filename: "hello_world.txt",
|
filename: "hello_world.txt",
|
||||||
|
@ -482,13 +494,37 @@ func TestFilterMultipleParameters(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
glob: "*.txt",
|
||||||
|
minsize: "4096B",
|
||||||
|
good: []singletest{
|
||||||
|
{
|
||||||
|
filename: "test.txt",
|
||||||
|
size: 9001,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
filename: "hello.txt",
|
||||||
|
size: 4097,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bad: []singletest{
|
||||||
|
{
|
||||||
|
filename: "test.md",
|
||||||
|
size: 9001,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
filename: "test.txt",
|
||||||
|
size: 1024,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFilterBlank(t *testing.T) {
|
func TestFilterBlank(t *testing.T) {
|
||||||
var f *Filter
|
var f *Filter
|
||||||
t.Run("new", func(t *testing.T) {
|
t.Run("new", func(t *testing.T) {
|
||||||
f, _ = New("", "", "", "", "", "", "", false, false, false)
|
f, _ = New("", "", "", "", "", "", "", false, false, false, "0", "0")
|
||||||
if !f.Blank() {
|
if !f.Blank() {
|
||||||
t.Fatalf("filter isn't blank? %s", f)
|
t.Fatalf("filter isn't blank? %s", f)
|
||||||
}
|
}
|
||||||
|
@ -547,7 +583,8 @@ func TestFilterNotBlank(t *testing.T) {
|
||||||
t.Run("notblank"+tester.String(), func(t *testing.T) {
|
t.Run("notblank"+tester.String(), func(t *testing.T) {
|
||||||
f, _ = New(
|
f, _ = New(
|
||||||
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
|
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
|
||||||
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly, tester.showhidden,
|
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly,
|
||||||
|
tester.showhidden, tester.minsize, tester.maxsize,
|
||||||
tester.filenames...,
|
tester.filenames...,
|
||||||
)
|
)
|
||||||
if f.Blank() {
|
if f.Blank() {
|
||||||
|
|
|
@ -89,7 +89,7 @@ func FindFiles(trashdir, ogdir string, f *filter.Filter) (files Infos, outerr er
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.Match(filename, date, info.IsDir()) {
|
if f.Match(filename, date, info.Size(), info.IsDir()) {
|
||||||
log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
||||||
files = append(files, Info{
|
files = append(files, Info{
|
||||||
name: filename,
|
name: filename,
|
||||||
|
|
19
main.go
19
main.go
|
@ -33,6 +33,7 @@ var (
|
||||||
loglvl string
|
loglvl string
|
||||||
f *filter.Filter
|
f *filter.Filter
|
||||||
o, b, a, g, p string
|
o, b, a, g, p string
|
||||||
|
sm, lg string
|
||||||
ung, unp string
|
ung, unp string
|
||||||
fo, do, sh bool
|
fo, do, sh bool
|
||||||
askconfirm bool
|
askconfirm bool
|
||||||
|
@ -91,7 +92,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
if f == nil {
|
if f == nil {
|
||||||
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh)
|
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh, sm, lg)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -115,7 +116,7 @@ var (
|
||||||
beforeCommands = func(ctx *cli.Context) (err error) {
|
beforeCommands = func(ctx *cli.Context) (err error) {
|
||||||
// setup filter
|
// setup filter
|
||||||
if f == nil {
|
if f == nil {
|
||||||
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, true, ctx.Args().Slice()...)
|
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, true, sm, lg, ctx.Args().Slice()...)
|
||||||
}
|
}
|
||||||
log.Debugf("filter: %s", f.String())
|
log.Debugf("filter: %s", f.String())
|
||||||
return
|
return
|
||||||
|
@ -123,7 +124,7 @@ var (
|
||||||
|
|
||||||
beforeTrash = func(_ *cli.Context) (err error) {
|
beforeTrash = func(_ *cli.Context) (err error) {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh)
|
f, err = filter.New(o, b, a, g, p, ung, unp, fo, do, sh, sm, lg)
|
||||||
}
|
}
|
||||||
log.Debugf("filter: %s", f.String())
|
log.Debugf("filter: %s", f.String())
|
||||||
return
|
return
|
||||||
|
@ -363,6 +364,18 @@ var (
|
||||||
DisableDefaultText: true,
|
DisableDefaultText: true,
|
||||||
Destination: &do,
|
Destination: &do,
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "min-size",
|
||||||
|
Usage: "operate on files larger than `SIZE`",
|
||||||
|
Aliases: []string{"N"},
|
||||||
|
Destination: &sm,
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "max-size",
|
||||||
|
Usage: "operate on files smaller than `SIZE`",
|
||||||
|
Aliases: []string{"X"},
|
||||||
|
Destination: &lg,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
trashFlags = []cli.Flag{
|
trashFlags = []cli.Flag{
|
||||||
|
|
Loading…
Reference in a new issue