From 50f4dbda8ad41baa6278902f655de0d84c44678f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Tue, 6 Aug 2024 09:31:45 -0700 Subject: [PATCH] some clean up --- internal/dirs/dirs.go | 12 +++++------- internal/files/disk.go | 10 +++++----- internal/files/trash.go | 10 ++++++---- internal/interactive/interactive.go | 2 +- main.go | 22 +++++----------------- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/internal/dirs/dirs.go b/internal/dirs/dirs.go index ed3d84a..e33aa0d 100644 --- a/internal/dirs/dirs.go +++ b/internal/dirs/dirs.go @@ -20,7 +20,7 @@ var ( pwd, _ = os.Getwd() ) -// UnExpand returns dir after expanding some directory shortcuts +// UnExpand returns dir after unexpanding some directory shortcuts // // $HOME -> ~ // @@ -64,13 +64,11 @@ func PercentEncode(input string) (output string) { return } -func cleanDir(dir, pwd string) (out string) { +func cleanDir(dir, pwd string) string { if strings.HasPrefix(dir, ".") { - out = filepath.Clean(dir) + return filepath.Clean(dir) } else if !strings.HasPrefix(dir, sep) { - out = filepath.Join(pwd, dir) - } else { - out = dir + return filepath.Join(pwd, dir) } - return + return dir } diff --git a/internal/files/disk.go b/internal/files/disk.go index 26fd8b8..2b5e2d3 100644 --- a/internal/files/disk.go +++ b/internal/files/disk.go @@ -67,7 +67,7 @@ func NewDisk(path string) (DiskFile, error) { }, 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 dir = filepath.Clean(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)...) } - return files, nil + return files } // isInHiddenDir checks `path` and parent directories @@ -143,7 +143,7 @@ func walkDir(dir string, fltr *filter.Filter) Files { }) if err != nil { log.Errorf("error walking directory %s: %s", dir, err) - return Files{} + return nil } return files } @@ -152,7 +152,7 @@ func readDir(dir string, fltr *filter.Filter) Files { var files Files fs, err := os.ReadDir(dir) if err != nil { - return Files{} + return nil } for _, file := range fs { name := file.Name() @@ -163,7 +163,7 @@ func readDir(dir string, fltr *filter.Filter) Files { info, err := file.Info() if err != nil { - return Files{} + return nil } path := filepath.Dir(filepath.Join(dir, name)) diff --git a/internal/files/trash.go b/internal/files/trash.go index a235a95..106822e 100644 --- a/internal/files/trash.go +++ b/internal/files/trash.go @@ -67,7 +67,7 @@ func (t TrashInfo) String() string { 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 personalTrash := filepath.Join(xdg.DataHome, "Trash") @@ -79,12 +79,13 @@ func FindInAllTrashes(ogdir string, fltr *filter.Filter) (Files, error) { for _, trash := range getAllTrashes() { fls, err := findTrash(trash, ogdir, fltr) if err != nil { + log.Errorf("error reading trash dir '%s': %s", trash, err) continue } files = append(files, fls...) } - return files, nil + return files } 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") dirs, err := os.ReadDir(infodir) if err != nil { - return Files{}, err + return nil, err } for _, dir := range dirs { @@ -425,7 +426,8 @@ func getAllTrashes() []string { }) if err != nil { - return []string{} + log.Errorf("error reading mounts: %s", err) + return nil } return trashes diff --git a/internal/interactive/interactive.go b/internal/interactive/interactive.go index 13fa0bc..1cae836 100644 --- a/internal/interactive/interactive.go +++ b/internal/interactive/interactive.go @@ -86,7 +86,7 @@ type model struct { 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{ keys: defaultKeyMap(), readonly: readonly, diff --git a/main.go b/main.go index 9dbbea1..786a53c 100644 --- a/main.go +++ b/main.go @@ -102,10 +102,7 @@ var ( err error ) - infiles, err = files.FindInAllTrashes(ogdir, fltr) - if err != nil { - return err - } + infiles = files.FindInAllTrashes(ogdir, fltr) if len(infiles) <= 0 { var msg string if fltr.Blank() { @@ -206,10 +203,7 @@ var ( // if none of the args were files, then find files based on filter if len(filesToTrash) == 0 { - fls, err := files.FindDisk(workdir, recursive, fltr) - if err != nil { - return err - } + fls := files.FindDisk(workdir, recursive, fltr) if len(fls) == 0 { fmt.Fprintln(os.Stdout, "no files to trash") return nil @@ -237,7 +231,7 @@ var ( Flags: slices.Concat(listFlags, trashedFlags, filterFlags), Before: beforeCommands, Action: func(_ *cli.Context) error { - fls, err := files.FindInAllTrashes(ogdir, fltr) + fls := files.FindInAllTrashes(ogdir, fltr) var msg string log.Debugf("filter '%s' is blank? %t in %s", fltr, fltr.Blank(), ogdir) @@ -250,8 +244,6 @@ var ( if len(fls) == 0 { fmt.Fprintln(os.Stdout, msg) return nil - } else if err != nil { - return err } return interactive.Show(fls, noInterArg, workdir) @@ -266,12 +258,10 @@ var ( Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Before: beforeCommands, Action: func(_ *cli.Context) error { - fls, err := files.FindInAllTrashes(ogdir, fltr) + fls := files.FindInAllTrashes(ogdir, fltr) if len(fls) == 0 { fmt.Fprintln(os.Stdout, "no files to restore") return nil - } else if err != nil { - return err } selected, _, err := interactive.Select(fls, all, all, workdir, modes.Restoring) @@ -295,12 +285,10 @@ var ( Flags: slices.Concat(cleanRestoreFlags, trashedFlags, filterFlags), Before: beforeCommands, Action: func(_ *cli.Context) error { - fls, err := files.FindInAllTrashes(ogdir, fltr) + fls := files.FindInAllTrashes(ogdir, fltr) if len(fls) == 0 { fmt.Fprintln(os.Stdout, "no files to clean") return nil - } else if err != nil { - return err } selected, _, err := interactive.Select(fls, all, all, workdir, modes.Cleaning)