diff --git a/internal/cmd/find.go b/internal/cmd/find.go index 4e81c44..b1d8c82 100644 --- a/internal/cmd/find.go +++ b/internal/cmd/find.go @@ -210,7 +210,7 @@ func findCmdRun(args []string, opts findOptions) error { if !opts.force && isTerminal && !tui.BoolPrompt("Are you sure you want to restore? ") { return errors.New("do nothing") } - if err := doRestore(box.Files, opts.restoreTo); err != nil { + if err := doRestore(box.Files, opts.restoreTo, isTerminal && !opts.force); err != nil { return err } } diff --git a/internal/cmd/metafix.go b/internal/cmd/metafix.go index 91fddc8..613512e 100644 --- a/internal/cmd/metafix.go +++ b/internal/cmd/metafix.go @@ -16,7 +16,9 @@ type metafixCmd struct { opts metafixOptions } -type metafixOptions struct{} +type metafixOptions struct { + force bool +} func newMetafixCmd() *metafixCmd { root := &metafixCmd{} @@ -43,6 +45,9 @@ func newMetafixCmd() *metafixCmd { }, } + cmd.Flags().BoolVarP(&root.opts.force, "force", "f", false, `Always execute without confirmation prompt +This is not necessary if running outside of a terminal`) + root.cmd = cmd return root } @@ -66,7 +71,7 @@ func metafixCmdRun(opts metafixOptions) error { // (those for which trashinfo exists but the file does not). fmt.Printf("\nFound invalid metadata: %d\n", len(box.OrphanMeta)) - if isTerminal && !tui.BoolPrompt("Are you sure you want to remove invalid metadata? ") { + if !opts.force && isTerminal && !tui.BoolPrompt("Are you sure you want to remove invalid metadata? ") { return errors.New("do nothing") } diff --git a/internal/cmd/restore.go b/internal/cmd/restore.go index 8b3f452..8da1865 100644 --- a/internal/cmd/restore.go +++ b/internal/cmd/restore.go @@ -24,6 +24,7 @@ type restoreOptions struct { directory string cwd bool restoreTo string + force bool } func newRestoreCmd() *restoreCmd { @@ -64,6 +65,8 @@ func newRestoreCmd() *restoreCmd { cmd.Flags().StringVarP(&root.opts.directory, "directory", "d", "", "Filter by directory") cmd.Flags().BoolVarP(&root.opts.cwd, "cwd", "c", false, "Filter by current working directory") cmd.Flags().StringVar(&root.opts.restoreTo, "restore-to", "", "Restore to this path instead of original path") + cmd.Flags().BoolVarP(&root.opts.force, "force", "f", false, `Always execute without confirmation prompt +This is not necessary if running outside of a terminal`) root.cmd = cmd return root @@ -112,11 +115,11 @@ func restoreCmdRun(args []string, opts restoreOptions) (err error) { fmt.Printf("Will restore to %q instead of original path\n", opts.restoreTo) } - if isTerminal && !tui.BoolPrompt("Are you sure you want to restore? ") { + if !opts.force && isTerminal && !tui.BoolPrompt("Are you sure you want to restore? ") { return errors.New("do nothing") } - if err := doRestore(box.Files, opts.restoreTo); err != nil { + if err := doRestore(box.Files, opts.restoreTo, isTerminal && !opts.force); err != nil { return err } @@ -172,8 +175,8 @@ func checkRestoreDup(files []trash.File) error { return nil } -func doRestore(files []trash.File, restoreTo string) error { - if !isTerminal { +func doRestore(files []trash.File, restoreTo string, prompt bool) error { + if !prompt { if err := checkRestoreDup(files); err != nil { return err } @@ -214,7 +217,7 @@ func doRestore(files []trash.File, restoreTo string) error { // Check to see if the file already exists in the destination path. // This is necessary because rename(2) overwrites the file. if _, err := os.Lstat(restorePath); err == nil { - if !isTerminal { + if !prompt { glog.Errorf("cannot restore %q: restore path alread exists\n", file.OriginalPath) failed = append(failed, file) continue diff --git a/internal/cmd/restoreGroup.go b/internal/cmd/restoreGroup.go index 1fb0204..2550ff6 100644 --- a/internal/cmd/restoreGroup.go +++ b/internal/cmd/restoreGroup.go @@ -72,7 +72,7 @@ func restoreGroupCmdRun(args []string, opts restoreGroupOptions) error { return errors.New("do nothing") } - if err := doRestore(group.Files, ""); err != nil { + if err := doRestore(group.Files, "", true); err != nil { return err } diff --git a/internal/cmd/rm.go b/internal/cmd/rm.go index 733dc38..000fb98 100644 --- a/internal/cmd/rm.go +++ b/internal/cmd/rm.go @@ -17,7 +17,9 @@ type removeCmd struct { opts removeOptions } -type removeOptions struct{} +type removeOptions struct { + force bool +} func newRemoveCmd() *removeCmd { root := &removeCmd{} @@ -49,6 +51,9 @@ func newRemoveCmd() *removeCmd { }, } + cmd.Flags().BoolVarP(&root.opts.force, "force", "f", false, `Always execute without confirmation prompt +This is not necessary if running outside of a terminal`) + root.cmd = cmd return root } @@ -72,7 +77,7 @@ func removeCmdRun(args []string, opts removeOptions) error { } fmt.Printf("\nFound %d trashed files\n", len(box.Files)) - if isTerminal && !tui.BoolPrompt("Are you sure you want to remove PERMENANTLY? ") { + if !opts.force && isTerminal && !tui.BoolPrompt("Are you sure you want to remove PERMENANTLY? ") { return errors.New("do nothing") }