aboutsummaryrefslogtreecommitdiff
path: root/commands/compose/detach.go
blob: 91cf2a58c8495f9cb6cb46bdaa019d3d160aa26f (plain)
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
package compose

import (
	"fmt"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
)

type Detach struct {
	Path string `opt:"path" required:"false" complete:"CompletePath"`
}

func init() {
	register(Detach{})
}

func (Detach) Aliases() []string {
	return []string{"detach"}
}

func (*Detach) CompletePath(arg string) []string {
	composer, _ := app.SelectedTabContent().(*app.Composer)
	return commands.CompletionFromList(composer.GetAttachments(), arg)
}

func (d Detach) Execute(args []string) error {
	composer, _ := app.SelectedTabContent().(*app.Composer)

	if d.Path == "" {
		// if no attachment is specified, delete the first in the list
		atts := composer.GetAttachments()
		if len(atts) > 0 {
			d.Path = atts[0]
		} else {
			return fmt.Errorf("No attachments to delete")
		}
	}

	if err := composer.DeleteAttachment(d.Path); err != nil {
		return err
	}

	app.PushSuccess(fmt.Sprintf("Detached %s", d.Path))

	return nil
}