Debian Codegraph @

commands/account/mkdir.go

raw
1 package account
2
3 import (
4 "context"
5 "errors"
6 "time"
7
8 "git.sr.ht/~rjarry/aerc/app"
9 "git.sr.ht/~rjarry/aerc/commands"
10 "git.sr.ht/~rjarry/aerc/worker/types"
11 "git.sr.ht/~rjarry/go-opt/v2"
12 )
13
14 type MakeDir struct {
15 Folder string `opt:"folder" complete:"CompleteFolder" desc:"Folder name."`
16 }
17
18 func init() {
19 commands.Register(MakeDir{})
20 }
21
22 func (MakeDir) Description() string {
23 return "Create and change to a new folder."
24 }
25
26 func (MakeDir) Context() commands.CommandContext {
27 return commands.MESSAGE_LIST
28 }
29
30 func (MakeDir) Aliases() []string {
31 return []string{"mkdir"}
32 }
33
34 func (*MakeDir) CompleteFolder(arg string) []string {
35 acct := app.SelectedAccount()
36 if acct == nil {
37 return nil
38 }
39 sep := app.SelectedAccount().Worker().PathSeparator()
40 return commands.FilterList(
41 acct.Directories().List(), arg,
42 func(s string) string {
43 return opt.QuoteArg(s) + sep
44 },
45 )
46 }
47
48 func (m MakeDir) Execute(args []string) error {
49 acct := app.SelectedAccount()
50 if acct == nil {
51 return errors.New("No account selected")
52 }
53 acct.Worker().PostAction(context.TODO(), &types.CreateDirectory{
54 Directory: m.Folder,
55 }, func(msg types.WorkerMessage) {
56 switch msg := msg.(type) {
57 case *types.Done:
58 app.PushStatus("Directory created.", 10*time.Second)
59 acct.Directories().Open(m.Folder, "", 0, nil, false)
60 case *types.Error:
61 app.PushError(msg.Error.Error())
62 }
63 })
64 return nil
65 }