initial commit, it does the thing
This commit is contained in:
commit
cc1523034b
9 changed files with 1986 additions and 0 deletions
176
internal/files/files.go
Normal file
176
internal/files/files.go
Normal file
|
@ -0,0 +1,176 @@
|
|||
// Package files finds and displays files on disk
|
||||
package files
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"git.burning.moe/celediel/gt/internal/filter"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/lipgloss/table"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
name, path string
|
||||
filesize int64
|
||||
modified time.Time
|
||||
}
|
||||
|
||||
type Files []File
|
||||
|
||||
func (f File) Name() string { return f.name }
|
||||
func (f File) Path() string { return f.path }
|
||||
func (f File) Modified() time.Time { return f.modified }
|
||||
func (f File) Filesize() int64 { return f.filesize }
|
||||
|
||||
func (fls Files) Table(width int) string {
|
||||
// sort newest on top
|
||||
slices.SortStableFunc(fls, SortByModifiedReverse)
|
||||
|
||||
data := [][]string{}
|
||||
for _, file := range fls {
|
||||
t := humanize.Time(file.modified)
|
||||
b := humanize.Bytes(uint64(file.filesize))
|
||||
data = append(data, []string{
|
||||
file.name,
|
||||
file.path,
|
||||
t,
|
||||
b,
|
||||
})
|
||||
}
|
||||
t := table.New().
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
|
||||
Width(width).
|
||||
Headers("filename", "path", "modified", "size").
|
||||
Rows(data...)
|
||||
|
||||
return fmt.Sprint(t)
|
||||
}
|
||||
|
||||
func (fls Files) Show(width int) {
|
||||
fmt.Println(fls.Table(width))
|
||||
}
|
||||
|
||||
func Find(dir string, recursive bool, f *filter.Filter) (files Files, err error) {
|
||||
if dir == "." || dir == "" {
|
||||
var d string
|
||||
if d, err = os.Getwd(); err != nil {
|
||||
return
|
||||
} else {
|
||||
dir = d
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("gonna find files in %s matching %s", dir, f)
|
||||
|
||||
if recursive {
|
||||
files = append(files, walk_dir(dir, f)...)
|
||||
} else {
|
||||
files = append(files, read_dir(dir, f)...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func walk_dir(dir string, f *filter.Filter) (files Files) {
|
||||
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
||||
p, e := filepath.Abs(path)
|
||||
if e != nil {
|
||||
return err
|
||||
}
|
||||
info, _ := d.Info()
|
||||
if f.Match(d.Name(), info.ModTime()) {
|
||||
log.Debugf("found matching file: %s %s", p, info.ModTime())
|
||||
i, _ := os.Stat(p)
|
||||
files = append(files, File{path: filepath.Dir(p), name: d.Name(), filesize: i.Size(), modified: i.ModTime()})
|
||||
} else {
|
||||
log.Debugf("ignoring file %s (%s)", p, info.ModTime())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return []File{}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func read_dir(dir string, f *filter.Filter) (files Files) {
|
||||
fs, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return []File{}
|
||||
}
|
||||
for _, file := range fs {
|
||||
name := file.Name()
|
||||
|
||||
if name == dir {
|
||||
continue
|
||||
}
|
||||
|
||||
info, err := file.Info()
|
||||
if err != nil {
|
||||
return []File{}
|
||||
}
|
||||
|
||||
path := filepath.Dir(filepath.Join(dir, name))
|
||||
|
||||
if f.Match(name, info.ModTime()) {
|
||||
log.Debugf("found matching file: %s %s", path, info.ModTime())
|
||||
files = append(files, File{
|
||||
name: name,
|
||||
path: path,
|
||||
modified: info.ModTime(),
|
||||
filesize: info.Size(),
|
||||
})
|
||||
} else {
|
||||
log.Debugf("ignoring file %s (%s)", path, info.ModTime())
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SortByModified(a, b File) int {
|
||||
if a.modified.After(b.modified) {
|
||||
return 1
|
||||
} else if a.modified.Before(b.modified) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortByModifiedReverse(a, b File) int {
|
||||
if a.modified.Before(b.modified) {
|
||||
return 1
|
||||
} else if a.modified.After(b.modified) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortBySize(a, b File) int {
|
||||
if a.filesize > b.filesize {
|
||||
return 1
|
||||
} else if a.filesize < b.filesize {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortBySizeReverse(a, b File) int {
|
||||
if a.filesize < b.filesize {
|
||||
return 1
|
||||
} else if a.filesize > b.filesize {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
137
internal/filter/filter.go
Normal file
137
internal/filter/filter.go
Normal file
|
@ -0,0 +1,137 @@
|
|||
// Package filter filters files based on specific critera
|
||||
package filter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/ijt/go-anytime"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
on, before, after time.Time
|
||||
glob, pattern string
|
||||
filenames []string
|
||||
matcher *regexp.Regexp
|
||||
}
|
||||
|
||||
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 }
|
||||
|
||||
func (f *Filter) Match(filename string, modified time.Time) bool {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
if f.has_regex() && !f.matcher.MatchString(filename) {
|
||||
return false
|
||||
}
|
||||
if f.glob != "" {
|
||||
if match, err := filepath.Match(f.glob, filename); err != nil || !match {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if len(f.filenames) > 0 && !slices.Contains(f.filenames, filename) {
|
||||
return false
|
||||
}
|
||||
// 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
|
||||
}
|
||||
|
||||
func (f *Filter) Blank() bool {
|
||||
t := time.Time{}
|
||||
return !f.has_regex() &&
|
||||
f.glob == "" &&
|
||||
f.after.Equal(t) &&
|
||||
f.before.Equal(t) &&
|
||||
f.on.Equal(t) &&
|
||||
len(f.filenames) == 0
|
||||
}
|
||||
|
||||
func (f *Filter) String() string {
|
||||
var m string
|
||||
if f.matcher != nil {
|
||||
m = f.matcher.String()
|
||||
}
|
||||
return fmt.Sprintf("on:'%s' before:'%s' after:'%s' glob:'%s' regex:'%s' filenames:'%v'",
|
||||
f.on, f.before, f.after,
|
||||
f.glob, m, f.filenames,
|
||||
)
|
||||
}
|
||||
|
||||
func (f *Filter) has_regex() bool {
|
||||
if f.matcher == nil {
|
||||
return false
|
||||
}
|
||||
return f.matcher.String() != ""
|
||||
}
|
||||
|
||||
func New(o, b, a, g, p string, names ...string) (*Filter, error) {
|
||||
// o b a g p
|
||||
var (
|
||||
err error
|
||||
now = time.Now()
|
||||
)
|
||||
|
||||
f := &Filter{
|
||||
glob: g,
|
||||
filenames: append([]string{}, names...),
|
||||
}
|
||||
|
||||
if o != "" {
|
||||
on, err := anytime.Parse(o, now)
|
||||
if err != nil {
|
||||
return &Filter{}, err
|
||||
}
|
||||
f.on = on
|
||||
}
|
||||
|
||||
if a != "" {
|
||||
after, err := anytime.Parse(a, now)
|
||||
if err != nil {
|
||||
return &Filter{}, err
|
||||
}
|
||||
f.after = after
|
||||
}
|
||||
|
||||
if b != "" {
|
||||
before, err := anytime.Parse(b, now)
|
||||
if err != nil {
|
||||
return &Filter{}, err
|
||||
}
|
||||
f.before = before
|
||||
}
|
||||
|
||||
err = f.SetPattern(p)
|
||||
|
||||
return f, err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
378
internal/filter/filter_test.go
Normal file
378
internal/filter/filter_test.go
Normal file
|
@ -0,0 +1,378 @@
|
|||
package filter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
now = time.Now()
|
||||
yesterday = now.AddDate(0, 0, -1)
|
||||
ereyesterday = now.AddDate(0, 0, -2)
|
||||
oneweekago = now.AddDate(0, 0, -7)
|
||||
twoweeksago = now.AddDate(0, 0, -14)
|
||||
onemonthago = now.AddDate(0, -1, 0)
|
||||
twomonthsago = now.AddDate(0, -2, 0)
|
||||
fourmonthsago = now.AddDate(0, -4, 0)
|
||||
oneyearago = now.AddDate(-1, 0, 0)
|
||||
twoyearsago = now.AddDate(-2, 0, 0)
|
||||
fouryearsago = now.AddDate(-4, 0, 0)
|
||||
)
|
||||
|
||||
type testholder struct {
|
||||
pattern, glob string
|
||||
before, after, on string
|
||||
filenames []string
|
||||
good, bad []singletest
|
||||
}
|
||||
|
||||
func (t testholder) String() string {
|
||||
return fmt.Sprintf("pattern:'%s' glob:'%s' filenames:'%v' before:'%s' after:'%s' on:'%s'", t.pattern, t.glob, t.filenames, t.before, t.after, t.on)
|
||||
}
|
||||
|
||||
type singletest struct {
|
||||
filename string
|
||||
modified time.Time
|
||||
}
|
||||
|
||||
func (s singletest) String() string {
|
||||
return fmt.Sprintf("filename:'%s' modified:'%s'", s.filename, s.modified)
|
||||
}
|
||||
|
||||
func testmatch(t *testing.T, testers []testholder) {
|
||||
const testnamefmt string = "file %s modified on %s"
|
||||
var (
|
||||
f *Filter
|
||||
err error
|
||||
)
|
||||
for _, tester := range testers {
|
||||
f, err = New(tester.on, tester.before, tester.after, tester.glob, tester.pattern, tester.filenames...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tst := range tester.good {
|
||||
t.Run(fmt.Sprintf(testnamefmt+"_good", tst.filename, tst.modified), func(t *testing.T) {
|
||||
if !f.Match(tst.filename, tst.modified) {
|
||||
t.Fatalf("(filename:%s modified:%s) didn't match (%s) but should have", tst.filename, tst.modified, tester)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for _, tst := range tester.bad {
|
||||
t.Run(fmt.Sprintf(testnamefmt+"_bad", tst.filename, tst.modified), func(t *testing.T) {
|
||||
if f.Match(tst.filename, tst.modified) {
|
||||
t.Fatalf("(filename:%s modified:%s) matched (%s) but shouldn't have", tst.filename, tst.modified, tester)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func blankfilename(times ...time.Time) []singletest {
|
||||
out := make([]singletest, 0, len(times))
|
||||
for _, time := range times {
|
||||
out = append(out, singletest{filename: "blank.txt", modified: time})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func blanktime(filenames ...string) []singletest {
|
||||
out := make([]singletest, 0, len(filenames))
|
||||
for _, filename := range filenames {
|
||||
out = append(out, singletest{filename: filename, modified: time.Time{}})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestFilterOn(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
on: "2024-02-14",
|
||||
good: blankfilename(time.Date(2024, 2, 14, 12, 0, 0, 0, time.Local)),
|
||||
bad: blankfilename(now, now.Add(time.Hour*72), now.Add(-time.Hour*18)),
|
||||
},
|
||||
{
|
||||
on: "yesterday",
|
||||
good: blankfilename(yesterday),
|
||||
bad: blankfilename(now, oneweekago, onemonthago, oneyearago, twoweeksago, twomonthsago, twoyearsago),
|
||||
},
|
||||
{
|
||||
on: "one week ago",
|
||||
good: blankfilename(oneweekago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
{
|
||||
on: "one month ago",
|
||||
good: blankfilename(onemonthago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
{
|
||||
on: "two months ago",
|
||||
good: blankfilename(twomonthsago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
{
|
||||
on: "four months ago",
|
||||
good: blankfilename(fourmonthsago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
{
|
||||
on: "one year ago",
|
||||
good: blankfilename(oneyearago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
{
|
||||
on: "four years ago",
|
||||
good: blankfilename(fouryearsago),
|
||||
bad: blankfilename(now),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterAfter(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
after: "2020-02-14",
|
||||
good: blankfilename(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)),
|
||||
},
|
||||
{
|
||||
after: "yesterday",
|
||||
good: blankfilename(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),
|
||||
},
|
||||
{
|
||||
after: "one week ago",
|
||||
good: blankfilename(now),
|
||||
bad: blankfilename(oneweekago.AddDate(0, 0, -1)),
|
||||
},
|
||||
{
|
||||
after: "one month ago",
|
||||
good: blankfilename(now, oneweekago, twoweeksago),
|
||||
bad: blankfilename(onemonthago, twomonthsago, fourmonthsago, oneyearago),
|
||||
},
|
||||
{
|
||||
after: "two months ago",
|
||||
good: blankfilename(now, onemonthago, oneweekago),
|
||||
bad: blankfilename(twomonthsago, oneyearago, fourmonthsago),
|
||||
},
|
||||
{
|
||||
after: "four months ago",
|
||||
good: blankfilename(now, oneweekago, onemonthago, twoweeksago, twomonthsago, onemonthago),
|
||||
bad: blankfilename(fourmonthsago, oneyearago),
|
||||
},
|
||||
{
|
||||
after: "one year ago",
|
||||
good: blankfilename(now, onemonthago, twomonthsago, fourmonthsago),
|
||||
bad: blankfilename(oneyearago, fouryearsago, twoyearsago),
|
||||
},
|
||||
{
|
||||
after: "four years ago",
|
||||
good: blankfilename(now, twoyearsago, onemonthago, fourmonthsago),
|
||||
bad: blankfilename(fouryearsago, fouryearsago.AddDate(-1, 0, 0)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterBefore(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
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)),
|
||||
bad: blankfilename(now, now.AddDate(0, 0, 10), now.AddDate(0, -2, 0)),
|
||||
},
|
||||
{
|
||||
before: "yesterday",
|
||||
good: blankfilename(onemonthago, oneweekago, oneyearago),
|
||||
bad: blankfilename(now, now.AddDate(0, 0, 1)),
|
||||
},
|
||||
{
|
||||
before: "one week ago",
|
||||
good: blankfilename(onemonthago, oneyearago, twoweeksago),
|
||||
bad: blankfilename(yesterday, now),
|
||||
},
|
||||
{
|
||||
before: "one month ago",
|
||||
good: blankfilename(oneyearago, twomonthsago),
|
||||
bad: blankfilename(oneweekago, yesterday, now),
|
||||
},
|
||||
{
|
||||
before: "two months ago",
|
||||
good: blankfilename(fourmonthsago, oneyearago),
|
||||
bad: blankfilename(onemonthago, oneweekago, yesterday, now),
|
||||
},
|
||||
{
|
||||
before: "four months ago",
|
||||
good: blankfilename(oneyearago, twoyearsago, fouryearsago),
|
||||
bad: blankfilename(twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||
},
|
||||
{
|
||||
before: "one year ago",
|
||||
good: blankfilename(twoyearsago, fouryearsago),
|
||||
bad: blankfilename(fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||
},
|
||||
{
|
||||
before: "four years ago",
|
||||
good: blankfilename(fouryearsago.AddDate(-1, 0, 0), fouryearsago.AddDate(-4, 0, 0)),
|
||||
bad: blankfilename(oneyearago, fourmonthsago, twomonthsago, onemonthago, oneweekago, yesterday, now),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterMatch(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
pattern: "[Tt]est",
|
||||
good: blanktime("test", "Test"),
|
||||
bad: blanktime("TEST", "tEst", "tEST", "TEst"),
|
||||
},
|
||||
{
|
||||
pattern: "^h.*o$",
|
||||
good: blanktime("hello", "hippo", "how about some pasta with alfredo"),
|
||||
bad: blanktime("hi", "test", "hellO", "Hello", "oh hello there"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterGlob(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
glob: "*.txt",
|
||||
good: blanktime("test.txt", "alsotest.txt"),
|
||||
bad: blanktime("test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||
},
|
||||
{
|
||||
glob: "*.tar.*",
|
||||
good: blanktime("test.tar.gz", "test.tar.xz", "test.tar.zst", "test.tar.bz2"),
|
||||
bad: blanktime("test.tar", "test.txt", "test.targz", "test.tgz"),
|
||||
},
|
||||
{
|
||||
glob: "pot*o",
|
||||
good: blanktime("potato", "potdonkeyo", "potesto"),
|
||||
bad: blanktime("salad", "test", "alsotest"),
|
||||
},
|
||||
{
|
||||
glob: "t?st",
|
||||
good: blanktime("test", "tast", "tfst", "tnst"),
|
||||
bad: blanktime("best", "fast", "most", "past"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterFilenames(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
filenames: []string{"test.txt", "alsotest.txt"},
|
||||
good: blanktime("test.txt", "alsotest.txt"),
|
||||
bad: blanktime("test.md", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||
},
|
||||
{
|
||||
filenames: []string{"test.md", "test.txt"},
|
||||
good: blanktime("test.txt", "test.md"),
|
||||
bad: blanktime("alsotest.txt", "test.go", "test.tar.gz", "testxt", "test.text"),
|
||||
},
|
||||
{
|
||||
filenames: []string{"hello.world"},
|
||||
good: blanktime("hello.world"),
|
||||
bad: blanktime("test.md", "test.go", "test.tar.gz", "testxt", "test.text", "helloworld", "Hello.world"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterMultipleParameters(t *testing.T) {
|
||||
testmatch(t, []testholder{
|
||||
{
|
||||
pattern: "[Tt]est",
|
||||
before: "yesterday",
|
||||
good: []singletest{
|
||||
{filename: "test", modified: oneweekago},
|
||||
{filename: "test", modified: twoweeksago},
|
||||
{filename: "Test", modified: onemonthago},
|
||||
{filename: "Test", modified: fourmonthsago},
|
||||
},
|
||||
bad: []singletest{
|
||||
{filename: "test", modified: now},
|
||||
{filename: "salad", modified: oneweekago},
|
||||
{filename: "holyshit", modified: onemonthago},
|
||||
},
|
||||
},
|
||||
{
|
||||
glob: "*.tar.*",
|
||||
before: "yesterday",
|
||||
after: "one month ago",
|
||||
good: []singletest{
|
||||
{filename: "test.tar.xz", modified: oneweekago},
|
||||
{filename: "test.tar.gz", modified: twoweeksago},
|
||||
{filename: "test.tar.zst", modified: twoweeksago.AddDate(0, 0, 2)},
|
||||
{filename: "test.tar.bz2", modified: twoweeksago.AddDate(0, 0, -4)},
|
||||
},
|
||||
bad: []singletest{
|
||||
{filename: "test.tar.gz", modified: oneyearago},
|
||||
{filename: "test.targz", modified: oneweekago},
|
||||
{filename: "test.jpg", modified: ereyesterday},
|
||||
},
|
||||
},
|
||||
{
|
||||
on: "today",
|
||||
after: "two weeks ago",
|
||||
before: "one week ago",
|
||||
good: blankfilename(now, time.Date(now.Year(), now.Month(), now.Day(), 18, 42, 0, 0, time.Local), time.Date(now.Year(), now.Month(), now.Day(), 8, 17, 33, 0, time.Local)),
|
||||
bad: blankfilename(yesterday, oneweekago, onemonthago, oneyearago),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterBlank(t *testing.T) {
|
||||
var f *Filter
|
||||
t.Run("new", func(t *testing.T) {
|
||||
f, _ = New("", "", "", "", "")
|
||||
if !f.Blank() {
|
||||
t.Fatalf("filter isn't blank? %s", f)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("blank", func(t *testing.T) {
|
||||
f = &Filter{}
|
||||
if !f.Blank() {
|
||||
t.Fatalf("filter isn't blank? %s", f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestFilterNotBlank(t *testing.T) {
|
||||
var (
|
||||
f *Filter
|
||||
testers = []testholder{
|
||||
{
|
||||
pattern: "[Ttest]",
|
||||
},
|
||||
{
|
||||
glob: "*test*",
|
||||
},
|
||||
{
|
||||
before: "yesterday",
|
||||
after: "one week ago",
|
||||
},
|
||||
{
|
||||
on: "2024-06-06",
|
||||
},
|
||||
{
|
||||
filenames: []string{"hello"},
|
||||
},
|
||||
{
|
||||
filenames: []string{""},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
for _, tester := range testers {
|
||||
t.Run("notblank"+tester.String(), func(t *testing.T) {
|
||||
f, _ = New(tester.on, tester.before, tester.after, tester.glob, tester.pattern, tester.filenames...)
|
||||
if f.Blank() {
|
||||
t.Fatalf("filter is blank?? %s", f)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
236
internal/trash/trash.go
Normal file
236
internal/trash/trash.go
Normal file
|
@ -0,0 +1,236 @@
|
|||
// Package trash finds and displays files located in the trash, and moves
|
||||
// files into the trash, creating cooresponding .trashinfo files
|
||||
package trash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.burning.moe/celediel/gt/internal/filter"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/lipgloss/table"
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/dustin/go-humanize"
|
||||
"gitlab.com/tymonx/go-formatter/formatter"
|
||||
"gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
const (
|
||||
trash_info_ext string = ".trashinfo"
|
||||
trash_info_sec string = "Trash Info"
|
||||
trash_info_path string = "Path"
|
||||
trash_info_date string = "DeletionDate"
|
||||
trash_info_date_fmt string = "2006-01-02T15:04:05"
|
||||
trash_info_template string = `[Trash Info]
|
||||
Path={path}
|
||||
DeletionDate={date}`
|
||||
)
|
||||
|
||||
type Info struct {
|
||||
name, ogpath string
|
||||
path, trashinfo string
|
||||
trashed time.Time
|
||||
filesize int64
|
||||
}
|
||||
|
||||
type Infos []Info
|
||||
|
||||
func (i Info) Name() string { return i.name }
|
||||
func (i Info) Path() string { return i.path }
|
||||
func (i Info) OGPath() string { return i.ogpath }
|
||||
func (i Info) TrashInfo() string { return i.trashinfo }
|
||||
func (i Info) Trashed() time.Time { return i.trashed }
|
||||
func (i Info) Filesize() int64 { return i.filesize }
|
||||
|
||||
func (is Infos) Table(width int) string {
|
||||
|
||||
// sort newest on top
|
||||
slices.SortStableFunc(is, SortByTrashedReverse)
|
||||
out := [][]string{}
|
||||
for _, file := range is {
|
||||
t := humanize.Time(file.trashed)
|
||||
b := humanize.Bytes(uint64(file.filesize))
|
||||
out = append(out, []string{
|
||||
file.name,
|
||||
filepath.Dir(file.ogpath),
|
||||
t,
|
||||
b,
|
||||
})
|
||||
}
|
||||
|
||||
t := table.New().
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("99"))).
|
||||
Width(width).
|
||||
Headers("filename", "original path", "deleted", "size").
|
||||
Rows(out...)
|
||||
|
||||
return fmt.Sprint(t)
|
||||
}
|
||||
|
||||
func (is Infos) Show(width int) {
|
||||
fmt.Println(is.Table(width))
|
||||
}
|
||||
|
||||
func FindFiles(trashdir string, f *filter.Filter) (files Infos, outerr error) {
|
||||
outerr = filepath.WalkDir(trashdir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
log.Debugf("what happened?? what is %s?", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// ignore self, directories, and non trashinfo files
|
||||
if path == trashdir || d.IsDir() || filepath.Ext(path) != trash_info_ext {
|
||||
return nil
|
||||
}
|
||||
|
||||
// trashinfo is just an ini file, so
|
||||
c, err := ini.Load(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s := c.Section(trash_info_sec); s != nil {
|
||||
basepath := s.Key(trash_info_path).String()
|
||||
filename := filepath.Base(basepath)
|
||||
// maybe this is kind of a HACK
|
||||
trashedpath := strings.Replace(strings.Replace(path, "info", "files", 1), trash_info_ext, "", 1)
|
||||
info, _ := os.Stat(trashedpath)
|
||||
|
||||
s := s.Key(trash_info_date).Value()
|
||||
date, err := time.ParseInLocation(trash_info_date_fmt, s, time.Local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if f.Match(filename, date) {
|
||||
log.Debugf("%s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
||||
files = append(files, Info{
|
||||
name: filename,
|
||||
path: trashedpath,
|
||||
ogpath: basepath,
|
||||
trashinfo: path,
|
||||
trashed: date,
|
||||
filesize: info.Size(),
|
||||
})
|
||||
} else {
|
||||
log.Debugf("(ignored) %s: deleted on %s", filename, date.Format(trash_info_date_fmt))
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if outerr != nil {
|
||||
return []Info{}, outerr
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Restore(files []Info) (restored int, err error) {
|
||||
for _, file := range files {
|
||||
log.Infof("restoring %s back to %s\n", file.name, file.ogpath)
|
||||
if err = os.Rename(file.path, file.ogpath); err != nil {
|
||||
return restored, err
|
||||
}
|
||||
if err = os.Remove(file.trashinfo); err != nil {
|
||||
return restored, err
|
||||
}
|
||||
restored++
|
||||
}
|
||||
fmt.Printf("restored %d files\n", restored)
|
||||
return restored, err
|
||||
}
|
||||
|
||||
func Remove(files []Info) (removed int, err error) {
|
||||
for _, file := range files {
|
||||
log.Infof("removing %s permanently forever!!!", file.name)
|
||||
if err = os.Remove(file.path); err != nil {
|
||||
return removed, err
|
||||
}
|
||||
if err = os.Remove(file.trashinfo); err != nil {
|
||||
return removed, err
|
||||
}
|
||||
removed++
|
||||
}
|
||||
return removed, err
|
||||
}
|
||||
|
||||
func TrashFile(trashDir, name string) error {
|
||||
outdir := filepath.Join(trashDir, "files")
|
||||
trashout := filepath.Join(trashDir, "info")
|
||||
|
||||
filename := filepath.Base(name)
|
||||
trashinfo_filename := filepath.Join(trashout, filename+trash_info_ext)
|
||||
|
||||
out_path := filepath.Join(outdir, filename)
|
||||
if err := os.Rename(name, out_path); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
trash_info, err := formatter.Format(trash_info_template, formatter.Named{
|
||||
"path": name,
|
||||
"date": time.Now().Format(trash_info_date_fmt),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.WriteFile(trashinfo_filename, []byte(trash_info), fs.FileMode(0600)); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TrashFiles(trashDir string, files ...string) (trashed int, err error) {
|
||||
for _, file := range files {
|
||||
if err = TrashFile(trashDir, file); err != nil {
|
||||
return trashed, err
|
||||
}
|
||||
trashed++
|
||||
}
|
||||
return trashed, err
|
||||
}
|
||||
|
||||
func SortByTrashed(a, b Info) int {
|
||||
if a.trashed.After(b.trashed) {
|
||||
return 1
|
||||
} else if a.trashed.Before(b.trashed) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortByTrashedReverse(a, b Info) int {
|
||||
if a.trashed.Before(b.trashed) {
|
||||
return 1
|
||||
} else if a.trashed.After(b.trashed) {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortBySize(a, b Info) int {
|
||||
if a.filesize > b.filesize {
|
||||
return 1
|
||||
} else if a.filesize < b.filesize {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func SortBySizeReverse(a, b Info) int {
|
||||
if a.filesize < b.filesize {
|
||||
return 1
|
||||
} else if a.filesize > b.filesize {
|
||||
return -1
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue