make linters happy

fix typos, less short and more idomatic variable names, package comments, etc.
This commit is contained in:
Lilian Jónsdóttir 2024-07-30 11:43:54 -07:00
parent 50e8c992d1
commit 9b87c02744
12 changed files with 259 additions and 247 deletions

View file

@ -1,4 +1,4 @@
// Package filter filters files based on specific critera
// Package filter filters files based on specific criteria
package filter
import (
@ -61,7 +61,7 @@ func (f *Filter) Match(info fs.FileInfo) bool {
// on or before/after, not both
if !f.on.IsZero() {
if !same_day(f.on, modified) {
if !sameDay(f.on, modified) {
log.Debugf("%s: %s isn't on %s, bye!", filename, modified, f.on)
return false
}
@ -76,7 +76,7 @@ func (f *Filter) Match(info fs.FileInfo) bool {
}
}
if f.has_regex() && !f.matcher.MatchString(filename) {
if f.hasRegex() && !f.matcher.MatchString(filename) {
log.Debugf("%s doesn't match `%s`, bye!", filename, f.matcher.String())
return false
}
@ -88,7 +88,7 @@ func (f *Filter) Match(info fs.FileInfo) bool {
}
}
if f.has_unregex() && f.unmatcher.MatchString(filename) {
if f.hasUnregex() && f.unmatcher.MatchString(filename) {
log.Debugf("%s matches `%s`, bye!", filename, f.unmatcher.String())
return false
}
@ -155,14 +155,14 @@ func (f *Filter) SetUnPattern(unpattern string) error {
}
func (f *Filter) Blank() bool {
t := time.Time{}
return !f.has_regex() &&
!f.has_unregex() &&
blank := time.Time{}
return !f.hasRegex() &&
!f.hasUnregex() &&
f.glob == "" &&
f.unglob == "" &&
f.after.Equal(t) &&
f.before.Equal(t) &&
f.on.Equal(t) &&
f.after.Equal(blank) &&
f.before.Equal(blank) &&
f.on.Equal(blank) &&
len(f.filenames) == 0 &&
!f.ignorehidden &&
!f.filesonly &&
@ -173,31 +173,31 @@ func (f *Filter) Blank() bool {
}
func (f *Filter) String() string {
var m, unm string
var match, unmatch string
if f.matcher != nil {
m = f.matcher.String()
match = f.matcher.String()
}
if f.unmatcher != nil {
unm = f.unmatcher.String()
unmatch = f.unmatcher.String()
}
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' "+
"minsize:'%d' maxsize:'%d' mode:'%s'",
f.on, f.before, f.after,
f.glob, m, f.unglob, unm,
f.glob, match, f.unglob, unmatch,
f.filenames, f.filesonly, f.dirsonly,
f.ignorehidden, f.minsize, f.maxsize, f.mode,
)
}
func (f *Filter) has_regex() bool {
func (f *Filter) hasRegex() bool {
if f.matcher == nil {
return false
}
return f.matcher.String() != ""
}
func (f *Filter) has_unregex() bool {
func (f *Filter) hasUnregex() bool {
if f.unmatcher == nil {
return false
}
@ -210,7 +210,7 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
now = time.Now()
)
f := &Filter{
filter := &Filter{
glob: glob,
unglob: unglob,
filesonly: filesonly,
@ -219,14 +219,14 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
mode: mode,
}
f.AddFileNames(names...)
filter.AddFileNames(names...)
if on != "" {
o, err := anytime.Parse(on, now)
if err != nil {
return &Filter{}, err
}
f.on = o
filter.on = o
}
if after != "" {
@ -234,7 +234,7 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
if err != nil {
return &Filter{}, err
}
f.after = a
filter.after = a
}
if before != "" {
@ -242,14 +242,14 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
if err != nil {
return &Filter{}, err
}
f.before = b
filter.before = b
}
err = f.SetPattern(pattern)
err = filter.SetPattern(pattern)
if err != nil {
return nil, err
}
err = f.SetUnPattern(unpattern)
err = filter.SetUnPattern(unpattern)
if err != nil {
return nil, err
}
@ -259,7 +259,7 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
if e != nil {
log.Errorf("invalid input size '%s'", minsize)
}
f.minsize = int64(m)
filter.minsize = int64(m)
}
if maxsize != "" {
@ -267,13 +267,13 @@ func New(on, before, after, glob, pattern, unglob, unpattern string, filesonly,
if e != nil {
log.Errorf("invalid input size '%s'", maxsize)
}
f.maxsize = int64(m)
filter.maxsize = int64(m)
}
return f, nil
return filter, nil
}
func same_day(a, b time.Time) bool {
func sameDay(a, b time.Time) bool {
ay, am, ad := a.Date()
by, bm, bd := b.Date()
return ay == by && am == bm && ad == bd

View file

@ -66,13 +66,14 @@ func (s singletest) String() string {
}
func testmatch(t *testing.T, testers []testholder) {
t.Helper() // I don't think this is a helper function but w/e
const testnamefmt string = "file %s modified on %s"
var (
f *filter.Filter
err error
fltr *filter.Filter
err error
)
for _, tester := range testers {
f, err = filter.New(
fltr, err = filter.New(
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly,
tester.ignorehidden, tester.minsize, tester.maxsize, tester.mode,
@ -84,7 +85,7 @@ func testmatch(t *testing.T, testers []testholder) {
for _, tst := range tester.good {
t.Run(fmt.Sprintf(testnamefmt+"_good", tst.filename, tst.modified), func(t *testing.T) {
if !f.Match(tst) {
if !fltr.Match(tst) {
t.Fatalf("(%s) didn't match (%s) but should have", tst, tester)
}
})
@ -92,7 +93,7 @@ func testmatch(t *testing.T, testers []testholder) {
for _, tst := range tester.bad {
t.Run(fmt.Sprintf(testnamefmt+"_bad", tst.filename, tst.modified), func(t *testing.T) {
if f.Match(tst) {
if fltr.Match(tst) {
t.Fatalf("(%s) matched (%s) but shouldn't have", tst, tester)
}
})
@ -116,10 +117,10 @@ func timeonly(dir bool, times ...time.Time) []singletest {
return out
}
func sizeonly(dir bool, sizes ...int64) []singletest {
func sizeonly(sizes ...int64) []singletest {
out := make([]singletest, 0, len(sizes))
for _, size := range sizes {
out = append(out, singletest{filename: "blank", modified: time.Time{}, isdir: dir, size: size, mode: 0000})
out = append(out, singletest{filename: "blank", modified: time.Time{}, isdir: false, size: size, mode: 0000})
}
return out
}
@ -301,7 +302,7 @@ func TestFilterGlob(t *testing.T) {
},
{
glob: "t?st",
good: nameonly(false, "test", "tast", "tfst", "tnst"),
good: nameonly(false, "test", "tist", "tfst", "tnst"),
bad: nameonly(false, "best", "fast", "most", "past"),
},
})
@ -342,7 +343,7 @@ func TestFilterUnGlob(t *testing.T) {
{
unglob: "t?st",
good: nameonly(false, "best", "fast", "most", "past"),
bad: nameonly(false, "test", "tast", "tfst", "tnst"),
bad: nameonly(false, "test", "tist", "tfst", "tnst"),
},
})
}
@ -410,13 +411,13 @@ 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),
good: sizeonly(10000, 9002, 424242, math.MaxInt64),
bad: sizeonly(9000, math.MinInt64, 0, -9001),
},
{
maxsize: "9001B",
good: sizeonly(false, 9000, math.MinInt64, 0, -9001),
bad: sizeonly(false, 10000, 9002, 424242, math.MaxInt64),
good: sizeonly(9000, math.MinInt64, 0, -9001),
bad: sizeonly(10000, 9002, 424242, math.MaxInt64),
},
})
}
@ -580,25 +581,25 @@ func TestFilterMultipleParameters(t *testing.T) {
}
func TestFilterBlank(t *testing.T) {
var f *filter.Filter
var fltr *filter.Filter
t.Run("new", func(t *testing.T) {
f, _ = filter.New("", "", "", "", "", "", "", false, false, false, "0", "0", 0)
if !f.Blank() {
t.Fatalf("filter isn't blank? %s", f)
fltr, _ = filter.New("", "", "", "", "", "", "", false, false, false, "0", "0", 0)
if !fltr.Blank() {
t.Fatalf("filter isn't blank? %s", fltr)
}
})
t.Run("blank", func(t *testing.T) {
f = &filter.Filter{}
if !f.Blank() {
t.Fatalf("filter isn't blank? %s", f)
fltr = &filter.Filter{}
if !fltr.Blank() {
t.Fatalf("filter isn't blank? %s", fltr)
}
})
}
func TestFilterNotBlank(t *testing.T) {
var (
f *filter.Filter
fltr *filter.Filter
testers = []testholder{
{
pattern: "[Ttest]",
@ -642,14 +643,14 @@ func TestFilterNotBlank(t *testing.T) {
for _, tester := range testers {
t.Run("notblank"+tester.String(), func(t *testing.T) {
f, _ = filter.New(
fltr, _ = filter.New(
tester.on, tester.before, tester.after, tester.glob, tester.pattern,
tester.unglob, tester.unpattern, tester.filesonly, tester.dirsonly,
tester.ignorehidden, tester.minsize, tester.maxsize, tester.mode,
tester.filenames...,
)
if f.Blank() {
t.Fatalf("filter is blank?? %s", f)
if fltr.Blank() {
t.Fatalf("filter is blank?? %s", fltr)
}
})
}