diff options
author | Moritz Poldrack <git@moritz.sh> | 2022-08-30 22:16:30 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-08-30 22:23:24 +0200 |
commit | af9390d38befb9c227bfb90bfb7a49abb40ba411 (patch) | |
tree | e3b97799f61c66838cb20ba5deaf631394f0991a /commands/compose/attach.go | |
parent | e9c4e250cace13b6eb35a472d7b4110fbec31ed5 (diff) | |
download | aerc-af9390d38befb9c227bfb90bfb7a49abb40ba411.tar.gz |
attach: enable path globbing
Enable path globbing using Go's standard library globbing capabilities,
which allows for attaching multiple files at once.
Suggested-by: Anderson John Njahi <johnjahi55@gmail.com>
Signed-off-by: Moritz Poldrack <git@moritz.sh>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands/compose/attach.go')
-rw-r--r-- | commands/compose/attach.go | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/commands/compose/attach.go b/commands/compose/attach.go index 60809736..99898d5d 100644 --- a/commands/compose/attach.go +++ b/commands/compose/attach.go @@ -1,11 +1,14 @@ package compose import ( + "errors" "fmt" "os" + "path/filepath" "strings" "git.sr.ht/~rjarry/aerc/commands" + "git.sr.ht/~rjarry/aerc/logging" "git.sr.ht/~rjarry/aerc/widgets" "github.com/mitchellh/go-homedir" ) @@ -31,26 +34,45 @@ func (Attach) Execute(aerc *widgets.Aerc, args []string) error { } path := strings.Join(args[1:], " ") - path, err := homedir.Expand(path) if err != nil { + logging.Errorf("failed to expand path '%s': %v", path, err) aerc.PushError(err.Error()) return err } - pathinfo, err := os.Stat(path) - if err != nil { - aerc.PushError(err.Error()) - return err - } else if pathinfo.IsDir() { - aerc.PushError("Attachment must be a file, not a directory") - return nil + logging.Debugf("attaching %s", path) + + attachments, err := filepath.Glob(path) + if err != nil && errors.Is(err, filepath.ErrBadPattern) { + logging.Warnf("failed to parse as globbing pattern: %v", err) + attachments = []string{path} } + logging.Debugf("filenames: %v", attachments) + composer, _ := aerc.SelectedTabContent().(*widgets.Composer) - composer.AddAttachment(path) + for _, attach := range attachments { + logging.Debugf("attaching '%s'", attach) + + pathinfo, err := os.Stat(attach) + if err != nil { + logging.Errorf("failed to stat file: %v", err) + aerc.PushError(err.Error()) + return err + } else if pathinfo.IsDir() && len(attachments) == 1 { + aerc.PushError("Attachment must be a file, not a directory") + return nil + } - aerc.PushSuccess(fmt.Sprintf("Attached %s", pathinfo.Name())) + composer.AddAttachment(attach) + } + + if len(attachments) == 1 { + aerc.PushSuccess(fmt.Sprintf("Attached %s", path)) + } else { + aerc.PushSuccess(fmt.Sprintf("Attached %d files", len(attachments))) + } return nil } |