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>
This commit is contained in:
Moritz Poldrack 2022-08-30 22:16:30 +02:00 committed by Robin Jarry
parent e9c4e250ca
commit af9390d38b
3 changed files with 35 additions and 11 deletions

View File

@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
BSD. BSD.
- `outgoing-cred-cmd` execution is now deferred until a message needs to be sent. - `outgoing-cred-cmd` execution is now deferred until a message needs to be sent.
- `next-message-on-delete` now also applies to `:archive`. - `next-message-on-delete` now also applies to `:archive`.
- `:attach` now supports path globbing (`:attach *.log`)
### Fixed ### Fixed

View File

@ -1,11 +1,14 @@
package compose package compose
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings" "strings"
"git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/logging"
"git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/widgets"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
) )
@ -31,26 +34,45 @@ func (Attach) Execute(aerc *widgets.Aerc, args []string) error {
} }
path := strings.Join(args[1:], " ") path := strings.Join(args[1:], " ")
path, err := homedir.Expand(path) path, err := homedir.Expand(path)
if err != nil { if err != nil {
logging.Errorf("failed to expand path '%s': %v", path, err)
aerc.PushError(err.Error()) aerc.PushError(err.Error())
return err return err
} }
pathinfo, err := os.Stat(path) logging.Debugf("attaching %s", path)
if err != nil {
aerc.PushError(err.Error()) attachments, err := filepath.Glob(path)
return err if err != nil && errors.Is(err, filepath.ErrBadPattern) {
} else if pathinfo.IsDir() { logging.Warnf("failed to parse as globbing pattern: %v", err)
aerc.PushError("Attachment must be a file, not a directory") attachments = []string{path}
return nil
} }
logging.Debugf("filenames: %v", attachments)
composer, _ := aerc.SelectedTabContent().(*widgets.Composer) composer, _ := aerc.SelectedTabContent().(*widgets.Composer)
composer.AddAttachment(path) for _, attach := range attachments {
logging.Debugf("attaching '%s'", attach)
aerc.PushSuccess(fmt.Sprintf("Attached %s", pathinfo.Name())) 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
}
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 return nil
} }

View File

@ -437,7 +437,8 @@ message list, the message in the message viewer, etc).
Close the composer without sending, discarding the message in progress. Close the composer without sending, discarding the message in progress.
*attach* <path> *attach* <path>
Attaches the file at the given path to the email. Attaches the file at the given path to the email. The path can contain
globbing syntax described at https://godocs.io/path/filepath#Match.
*attach-key* *attach-key*
Attaches the public key for the configured account to the email. Attaches the public key for the configured account to the email.