Skip to content

Commit

Permalink
refactor: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
umlx5h committed Jan 12, 2024
1 parent b1e05e0 commit d234410
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
12 changes: 8 additions & 4 deletions internal/cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 != "" {
Expand Down
9 changes: 7 additions & 2 deletions internal/cmd/metafix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
2 changes: 1 addition & 1 deletion itest/trash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
}

0 comments on commit d234410

Please sign in to comment.