2019-07-16 22:48:25 +02:00
|
|
|
package compose
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-09-20 18:16:29 +02:00
|
|
|
"strings"
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/commands"
|
|
|
|
"git.sr.ht/~rjarry/aerc/widgets"
|
2019-07-16 22:48:25 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Attach struct{}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
register(Attach{})
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Aliases() []string {
|
2019-07-16 22:48:25 +02:00
|
|
|
return []string{"attach"}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Complete(aerc *widgets.Aerc, args []string) []string {
|
2019-09-20 18:16:29 +02:00
|
|
|
path := strings.Join(args, " ")
|
2019-07-30 21:10:58 +02:00
|
|
|
return commands.CompletePath(path)
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:34:03 +02:00
|
|
|
func (Attach) Execute(aerc *widgets.Aerc, args []string) error {
|
2019-09-20 18:16:29 +02:00
|
|
|
if len(args) == 1 {
|
2019-07-16 22:48:25 +02:00
|
|
|
return fmt.Errorf("Usage: :attach <path>")
|
|
|
|
}
|
|
|
|
|
2019-09-20 18:16:29 +02:00
|
|
|
path := strings.Join(args[1:], " ")
|
2019-07-16 22:48:25 +02:00
|
|
|
|
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-16 22:48:25 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
pathinfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2021-01-30 13:51:32 +01:00
|
|
|
aerc.PushError(err.Error())
|
2019-07-16 22:48:25 +02:00
|
|
|
return err
|
|
|
|
} else if pathinfo.IsDir() {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError("Attachment must be a file, not a directory")
|
2019-07-16 22:48:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-18 12:54:55 +02:00
|
|
|
composer, _ := aerc.SelectedTabContent().(*widgets.Composer)
|
2019-07-16 22:48:25 +02:00
|
|
|
composer.AddAttachment(path)
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
aerc.PushSuccess(fmt.Sprintf("Attached %s", pathinfo.Name()))
|
2019-07-16 22:48:25 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|