/
restoreGroup.go
82 lines (65 loc) · 1.84 KB
/
restoreGroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/umlx5h/gtrash/internal/glog"
"github.com/umlx5h/gtrash/internal/trash"
"github.com/umlx5h/gtrash/internal/tui"
)
type restoreGroupCmd struct {
cmd *cobra.Command
opts restoreGroupOptions
}
type restoreGroupOptions struct{}
func newRestoreGroupCmd() *restoreGroupCmd {
root := &restoreGroupCmd{}
cmd := &cobra.Command{
Use: "restore-group",
Aliases: []string{"rg"},
Short: "Restore trashed files as a group interactively (rg)",
Long: `Description:
Use the TUI interface for file restoration.
Unlike the 'restore' command, files deleted simultaneously are grouped together.
Multiple selections of groups are not allowed.
Actually, files deleted using 'gtrash put' may not be grouped accurately.
Files with deletion times matching in seconds are grouped together.
Refer below for detailed information.
ref: https://github.com/umlx5h/gtrash#how-does-the-restore-group-subcommand-work
`,
SilenceUsage: true,
Args: cobra.NoArgs,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, _ []string) error {
if err := restoreGroupCmdRun(root.opts); err != nil {
return err
}
if glog.ExitCode() > 0 {
return errContinue
}
return nil
},
}
root.cmd = cmd
return root
}
func restoreGroupCmdRun(_ restoreGroupOptions) error {
box := trash.NewBox()
if err := box.Open(); err != nil {
return err
}
groups := box.ToGroups()
group, err := tui.GroupSelect(groups)
if err != nil {
return err
}
listFiles(group.Files, false, false)
fmt.Printf("\nSelected %d trashed files\n", len(group.Files))
if isTerminal && !tui.BoolPrompt("Are you sure you want to restore? ") {
return errors.New("do nothing")
}
if err := doRestore(group.Files, "", true); err != nil {
return err
}
return nil
}