Debian Codegraph @

commands/account/rmdir.go

raw
1 package account
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "slices"
8 "time"
9
10 "git.sr.ht/~rjarry/aerc/app"
11 "git.sr.ht/~rjarry/aerc/commands"
12 "git.sr.ht/~rjarry/aerc/models"
13 "git.sr.ht/~rjarry/aerc/worker/types"
14 )
15
16 type RemoveDir struct {
17 Force bool `opt:"-f" desc:"Remove the directory even if it contains messages."`
18 Folder string `opt:"folder" complete:"CompleteFolder" required:"false" desc:"Folder name."`
19 }
20
21 func init() {
22 commands.Register(RemoveDir{})
23 }
24
25 func (RemoveDir) Description() string {
26 return "Remove folder."
27 }
28
29 func (RemoveDir) Context() commands.CommandContext {
30 return commands.MESSAGE_LIST
31 }
32
33 func (RemoveDir) Aliases() []string {
34 return []string{"rmdir"}
35 }
36
37 func (RemoveDir) CompleteFolder(arg string) []string {
38 acct := app.SelectedAccount()
39 if acct == nil {
40 return nil
41 }
42 return commands.FilterList(acct.Directories().List(), arg, nil)
43 }
44
45 func (r RemoveDir) Execute(args []string) error {
46 acct := app.SelectedAccount()
47 if acct == nil {
48 return errors.New("No account selected")
49 }
50
51 current := acct.Directories().SelectedDirectory()
52 toRemove := current
53 if r.Folder != "" {
54 toRemove = acct.Directories().Directory(r.Folder)
55 if toRemove == nil {
56 return fmt.Errorf("No such directory: %s", r.Folder)
57 }
58 }
59
60 role := toRemove.Role
61
62 // Check for any messages in the directory.
63 if role != models.QueryRole && toRemove.Exists > 0 && !r.Force {
64 return errors.New("Refusing to remove non-empty directory; use -f")
65 }
66
67 if role == models.VirtualRole {
68 return errors.New("Cannot remove a virtual node")
69 }
70
71 if toRemove != current {
72 r.remove(acct, toRemove, func() {})
73 return nil
74 }
75
76 curDir := current.Name
77 var newDir string
78 dirFound := false
79
80 oldDir := acct.Directories().Previous()
81 if oldDir != "" {
82 present := slices.Contains(acct.Directories().List(), oldDir)
83 if oldDir != curDir && present {
84 newDir = oldDir
85 dirFound = true
86 }
87 }
88
89 defaultDir := acct.AccountConfig().Default
90 if !dirFound && defaultDir != curDir {
91 if slices.Contains(acct.Directories().List(), defaultDir) {
92 newDir = defaultDir
93 dirFound = true
94 }
95 }
96
97 if !dirFound {
98 for _, dir := range acct.Directories().List() {
99 if dir != curDir {
100 newDir = dir
101 dirFound = true
102 break
103 }
104 }
105 }
106
107 if !dirFound {
108 return errors.New("No directory to move to afterwards!")
109 }
110
111 reopenCurrentDir := func() { acct.Directories().Open(curDir, "", 0, nil, false) }
112
113 acct.Directories().Open(newDir, "", 0, func(msg types.WorkerMessage) {
114 switch msg.(type) {
115 case *types.Done:
116 break
117 case *types.Error:
118 app.PushError("Could not change directory")
119 reopenCurrentDir()
120 return
121 default:
122 return
123 }
124 r.remove(acct, toRemove, reopenCurrentDir)
125 }, false)
126
127 return nil
128 }
129
130 func (r RemoveDir) remove(acct *app.AccountView, dir *models.Directory, onErr func()) {
131 acct.Worker().PostAction(context.TODO(), &types.RemoveDirectory{
132 Directory: dir.Name,
133 Quiet: r.Force,
134 }, func(msg types.WorkerMessage) {
135 switch msg := msg.(type) {
136 case *types.Done:
137 app.PushStatus("Directory removed.", 10*time.Second)
138 case *types.Error:
139 app.PushError(msg.Error.Error())
140 onErr()
141 case *types.Unsupported:
142 app.PushError(":rmdir is not supported by the backend.")
143 onErr()
144 }
145 })
146 }