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