From d234410f6d8b56bff6e4ab8c2c093c6bfbfde982 Mon Sep 17 00:00:00 2001 From: umlx5h Date: Fri, 12 Jan 2024 12:04:01 +0900 Subject: [PATCH] refactor: error handling --- internal/cmd/find.go | 12 ++++++++---- internal/cmd/metafix.go | 9 +++++++-- internal/cmd/summary.go | 2 +- itest/trash_test.go | 2 +- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/internal/cmd/find.go b/internal/cmd/find.go index 9d50a13..5428ce7 100644 --- a/internal/cmd/find.go +++ b/internal/cmd/find.go @@ -191,7 +191,13 @@ func findCmdRun(args []string, opts findOptions) error { trash.WithTrashDir(opts.trashDir), ) if err := box.Open(); err != nil { - return err + // no error only remove mode (consider executing via batch) + if opts.doRemove && errors.Is(err, trash.ErrNotFound) { + fmt.Printf("do nothing: %s\n", err) + return nil + } else { + return err + } } listFiles(box.Files, box.GetSize, opts.showTrashPath) @@ -212,9 +218,7 @@ func findCmdRun(args []string, opts findOptions) error { if !opts.force && isTerminal && !tui.BoolPrompt("Are you sure you want to remove PERMENANTLY? ") { return errors.New("do nothing") } - if err := doRemove(box.Files); err != nil { - return err - } + doRemove(box.Files) } else if opts.doRestore { if opts.restoreTo != "" { diff --git a/internal/cmd/metafix.go b/internal/cmd/metafix.go index 613512e..bd88b07 100644 --- a/internal/cmd/metafix.go +++ b/internal/cmd/metafix.go @@ -57,11 +57,16 @@ func metafixCmdRun(opts metafixOptions) error { trash.WithSortBy(trash.SortByName), ) if err := box.Open(); err != nil { - return err + if errors.Is(err, trash.ErrNotFound) { + fmt.Printf("do nothing: %s\n", err) + return nil + } else { + return err + } } if len(box.OrphanMeta) == 0 { - fmt.Println("Not found invalid metadata") + fmt.Println("not found invalid metadata") return nil } diff --git a/internal/cmd/summary.go b/internal/cmd/summary.go index a5f50d1..a99206f 100644 --- a/internal/cmd/summary.go +++ b/internal/cmd/summary.go @@ -83,7 +83,7 @@ func summaryCmdRun(_ summaryOptions) error { totalItem += item } - if len(box.FilesByTrashDir) > 1 { + if len(box.TrashDirs) > 1 { fmt.Printf("\n[total]\n") fmt.Printf("item: %d\n", totalItem) fmt.Printf("size: %s\n", humanize.Bytes(uint64(totalSize))) diff --git a/itest/trash_test.go b/itest/trash_test.go index 6387e05..f7b3f31 100644 --- a/itest/trash_test.go +++ b/itest/trash_test.go @@ -86,7 +86,7 @@ func TestTrashAllType(t *testing.T) { cmd = exec.Command(execBinary, "find") out, err = cmd.CombinedOutput() mustError(t, err, string(out)) - assertContains(t, string(out), "not found trashed files", "should not list deleted file") + assertContains(t, string(out), "not found: trashed files", "should not list deleted file") }) } }