some clean up

This commit is contained in:
Lilian Jónsdóttir 2024-08-06 09:31:45 -07:00
parent d8ec25b36d
commit 50f4dbda8a
5 changed files with 22 additions and 34 deletions

View file

@ -20,7 +20,7 @@ var (
pwd, _ = os.Getwd() pwd, _ = os.Getwd()
) )
// UnExpand returns dir after expanding some directory shortcuts // UnExpand returns dir after unexpanding some directory shortcuts
// //
// $HOME -> ~ // $HOME -> ~
// //
@ -64,13 +64,11 @@ func PercentEncode(input string) (output string) {
return return
} }
func cleanDir(dir, pwd string) (out string) { func cleanDir(dir, pwd string) string {
if strings.HasPrefix(dir, ".") { if strings.HasPrefix(dir, ".") {
out = filepath.Clean(dir) return filepath.Clean(dir)
} else if !strings.HasPrefix(dir, sep) { } else if !strings.HasPrefix(dir, sep) {
out = filepath.Join(pwd, dir) return filepath.Join(pwd, dir)
} else {
out = dir
} }
return return dir
} }

View file

@ -67,7 +67,7 @@ func NewDisk(path string) (DiskFile, error) {
}, nil }, nil
} }
func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) { func FindDisk(dir string, recursive bool, fltr *filter.Filter) Files {
var files Files var files Files
dir = filepath.Clean(dir) dir = filepath.Clean(dir)
if dir == "." || dir == "" { if dir == "." || dir == "" {
@ -91,7 +91,7 @@ func FindDisk(dir string, recursive bool, fltr *filter.Filter) (Files, error) {
files = append(files, readDir(dir, fltr)...) files = append(files, readDir(dir, fltr)...)
} }
return files, nil return files
} }
// isInHiddenDir checks `path` and parent directories // isInHiddenDir checks `path` and parent directories
@ -143,7 +143,7 @@ func walkDir(dir string, fltr *filter.Filter) Files {
}) })
if err != nil { if err != nil {
log.Errorf("error walking directory %s: %s", dir, err) log.Errorf("error walking directory %s: %s", dir, err)
return Files{} return nil
} }
return files return files
} }
@ -152,7 +152,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
var files Files var files Files
fs, err := os.ReadDir(dir) fs, err := os.ReadDir(dir)
if err != nil { if err != nil {
return Files{} return nil
} }
for _, file := range fs { for _, file := range fs {
name := file.Name() name := file.Name()
@ -163,7 +163,7 @@ func readDir(dir string, fltr *filter.Filter) Files {
info, err := file.Info() info, err := file.Info()
if err != nil { if err != nil {
return Files{} return nil
} }
path := filepath.Dir(filepath.Join(dir, name)) path := filepath.Dir(filepath.Join(dir, name))

View file

@ -67,7 +67,7 @@ func (t TrashInfo) String() string {
return t.name + t.path + t.ogpath + t.trashinfo return t.name + t.path + t.ogpath + t.trashinfo
} }
func FindInAllTrashes(ogdir string, fltr *filter.Filter) (Files, error) { func FindInAllTrashes(ogdir string, fltr *filter.Filter) Files {
var files Files var files Files
personalTrash := filepath.Join(xdg.DataHome, "Trash") personalTrash := filepath.Join(xdg.DataHome, "Trash")
@ -79,12 +79,13 @@ func FindInAllTrashes(ogdir string, fltr *filter.Filter) (Files, error) {
for _, trash := range getAllTrashes() { for _, trash := range getAllTrashes() {
fls, err := findTrash(trash, ogdir, fltr) fls, err := findTrash(trash, ogdir, fltr)
if err != nil { if err != nil {
log.Errorf("error reading trash dir '%s': %s", trash, err)
continue continue
} }
files = append(files, fls...) files = append(files, fls...)
} }
return files, nil return files
} }
func ConfirmRestore(confirm bool, fs Files) error { func ConfirmRestore(confirm bool, fs Files) error {
@ -143,7 +144,7 @@ func findTrash(trashdir, ogdir string, fltr *filter.Filter) (Files, error) {
infodir := filepath.Join(trashdir, "info") infodir := filepath.Join(trashdir, "info")
dirs, err := os.ReadDir(infodir) dirs, err := os.ReadDir(infodir)
if err != nil { if err != nil {
return Files{}, err return nil, err
} }
for _, dir := range dirs { for _, dir := range dirs {
@ -425,7 +426,8 @@ func getAllTrashes() []string {
}) })
if err != nil { if err != nil {
return []string{} log.Errorf("error reading mounts: %s", err)
return nil
} }
return trashes return trashes

View file

@ -86,7 +86,7 @@ type model struct {
fltrfiles files.Files fltrfiles files.Files
} }
func newModel(fls []files.File, selectall, readonly, once bool, workdir string, mode modes.Mode) model { func newModel(fls files.Files, selectall, readonly, once bool, workdir string, mode modes.Mode) model {
m := model{ m := model{
keys: defaultKeyMap(), keys: defaultKeyMap(),
readonly: readonly, readonly: readonly,

22
main.go
View file

@ -102,10 +102,7 @@ var (
err error err error
) )
infiles, err = files.FindInAllTrashes(ogdir, fltr) infiles = files.FindInAllTrashes(ogdir, fltr)
if err != nil {
return err
}
if len(infiles) <= 0 { if len(infiles) <= 0 {
var msg string var msg string
if fltr.Blank() { if fltr.Blank() {
@ -206,10 +203,7 @@ var (
// if none of the args were files, then find files based on filter // if none of the args were files, then find files based on filter
if len(filesToTrash) == 0 { if len(filesToTrash) == 0 {
fls, err := files.FindDisk(workdir, recursive, fltr) fls := files.FindDisk(workdir, recursive, fltr)
if err != nil {
return err
}
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to trash") fmt.Fprintln(os.Stdout, "no files to trash")
return nil return nil
@ -237,7 +231,7 @@ var (
Flags: slices.Concat(listFlags, trashedFlags, filterFlags), Flags: slices.Concat(listFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr) fls := files.FindInAllTrashes(ogdir, fltr)
var msg string var msg string
log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir) log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir)
@ -250,8 +244,6 @@ var (
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, msg) fmt.Fprintln(os.Stdout, msg)
return nil return nil
} else if err != nil {
return err
} }
return interactive.Show(fls, noInterArg, workdir) return interactive.Show(fls, noInterArg, workdir)
@ -266,12 +258,10 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr) fls := files.FindInAllTrashes(ogdir, fltr)
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to restore") fmt.Fprintln(os.Stdout, "no files to restore")
return nil return nil
} else if err != nil {
return err
} }
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring) selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring)
@ -295,12 +285,10 @@ var (
Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags),
Before: beforeCommands, Before: beforeCommands,
Action: func(_ *cli.Context) error { Action: func(_ *cli.Context) error {
fls, err := files.FindInAllTrashes(ogdir, fltr) fls := files.FindInAllTrashes(ogdir, fltr)
if len(fls) == 0 { if len(fls) == 0 {
fmt.Fprintln(os.Stdout, "no files to clean") fmt.Fprintln(os.Stdout, "no files to clean")
return nil return nil
} else if err != nil {
return err
} }
selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning) selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)