aboutsummaryrefslogtreecommitdiffstats
path: root/commands/compose/detach.go
blob: 69f8538d2c1915f9e57e6fcad5f57436b8207426 (plain) (blame)
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
83
84
85
86
87
88
89
package compose

import (
	"fmt"
	"path/filepath"

	"git.sr.ht/~rjarry/aerc/app"
	"git.sr.ht/~rjarry/aerc/commands"
	"git.sr.ht/~rjarry/aerc/lib/log"
	"github.com/pkg/errors"
)

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

func init() {
	commands.Register(Detach{})
}

func (Detach) Context() commands.CommandContext {
	return commands.COMPOSE
}

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

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

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")
		}
	}

	return d.removePath(d.Path)
}

func (d Detach) removePath(path string) error {
	composer, _ := app.SelectedTabContent().(*app.Composer)

	// If we don't get an error here, the path was not a pattern.
	if err := composer.DeleteAttachment(path); err == nil {
		log.Debugf("detaching '%s'", path)
		app.PushSuccess(fmt.Sprintf("Detached %s", path))

		return nil
	}

	currentAttachments := composer.GetAttachments()
	detached := make([]string, 0, len(currentAttachments))
	for _, a := range currentAttachments {
		// Don't use filepath.Glob like :attach does. Not all files
		// that match the glob are already attached to the message.
		matches, err := filepath.Match(path, a)
		if err != nil && errors.Is(err, filepath.ErrBadPattern) {
			log.Warnf("failed to parse as globbing pattern: %v", err)
			return err
		}

		if matches {
			log.Debugf("detaching '%s'", a)
			if err := composer.DeleteAttachment(a); err != nil {
				return err
			}

			detached = append(detached, a)
		}
	}

	if len(detached) == 1 {
		app.PushSuccess(fmt.Sprintf("Detached %s", detached[0]))
	} else {
		app.PushSuccess(fmt.Sprintf("Detached %d files", len(detached)))
	}

	return nil
}