aboutsummaryrefslogtreecommitdiffstats
path: root/commands/account/compose.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-03 22:12:10 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:49 +0200
commite54486ee40c9cedde9d4dae3e89d8b5f932188ee (patch)
tree254f032ef94920c28fbb9be8f918e61082a2015d /commands/account/compose.go
parent9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (diff)
downloadaerc-e54486ee40c9cedde9d4dae3e89d8b5f932188ee.tar.gz
commands: parse arguments with go-opt
Use the argument parsing framework introduced earlier to unify the parsing of (almost) all command options. Remove custom parsing code and to avoid extraneous types, add fields with `opt` tags on command structs that have options and arguments. Commands that take no argument do not need anything. Since the command objects now carry data, create a new temporary instance of them before passing them to opt.ArgsToStruct when executing a command. A few of the commands use specific semantics for parsing (:choose), or are delegating argument parsing to another function (:sort, :search, :filter). For these commands, simply add a dummy "-" passthrough argument. Since all commands still have the argument list (after split) nothing needs to be changed in this area. There should be no functional change besides the Usage strings and reported errors which are now generated automatically. Signed-off-by: Robin Jarry <robin@jarry.cc> Reviewed-by: Koni Marti <koni.marti@gmail.com> Tested-by: Moritz Poldrack <moritz@poldrack.dev> Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'commands/account/compose.go')
-rw-r--r--commands/account/compose.go85
1 files changed, 32 insertions, 53 deletions
diff --git a/commands/account/compose.go b/commands/account/compose.go
index 56079868..81eb3de0 100644
--- a/commands/account/compose.go
+++ b/commands/account/compose.go
@@ -12,15 +12,31 @@ import (
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/config"
- "git.sr.ht/~sircmpwn/getopt"
)
-type Compose struct{}
+type Compose struct {
+ Headers string `opt:"-H" action:"ParseHeader"`
+ Template string `opt:"-T"`
+ Edit bool `opt:"-e"`
+ NoEdit bool `opt:"-E"`
+ Body string `opt:"..." required:"false"`
+}
func init() {
register(Compose{})
}
+func (c *Compose) ParseHeader(arg string) error {
+ if strings.Contains(arg, ":") {
+ // ensure first colon is followed by a single space
+ re := regexp.MustCompile(`^(.*?):\s*(.*)`)
+ c.Headers += re.ReplaceAllString(arg, "$1: $2\r\n")
+ } else {
+ c.Headers += arg + ":\r\n"
+ }
+ return nil
+}
+
func (Compose) Aliases() []string {
return []string{"compose"}
}
@@ -29,20 +45,25 @@ func (Compose) Complete(args []string) []string {
return nil
}
-func (Compose) Execute(args []string) error {
- body, template, editHeaders, err := buildBody(args)
- if err != nil {
- return err
+func (c Compose) Execute(args []string) error {
+ if c.Headers != "" {
+ if c.Body != "" {
+ c.Body = c.Headers + "\r\n" + c.Body
+ } else {
+ c.Body = c.Headers + "\r\n\r\n"
+ }
}
+ if c.Template == "" {
+ c.Template = config.Templates.NewMessage
+ }
+ editHeaders := (config.Compose.EditHeaders || c.Edit) && !c.NoEdit
+
acct := app.SelectedAccount()
if acct == nil {
return errors.New("No account selected")
}
- if template == "" {
- template = config.Templates.NewMessage
- }
- msg, err := gomail.ReadMessage(strings.NewReader(body))
+ msg, err := gomail.ReadMessage(strings.NewReader(c.Body))
if errors.Is(err, io.EOF) { // completely empty
msg = &gomail.Message{Body: strings.NewReader("")}
} else if err != nil {
@@ -52,52 +73,10 @@ func (Compose) Execute(args []string) error {
composer, err := app.NewComposer(acct,
acct.AccountConfig(), acct.Worker(), editHeaders,
- template, &headers, nil, msg.Body)
+ c.Template, &headers, nil, msg.Body)
if err != nil {
return err
}
composer.Tab = app.NewTab(composer, "New email")
return nil
}
-
-func buildBody(args []string) (string, string, bool, error) {
- var body, template, headers string
- editHeaders := config.Compose.EditHeaders
- opts, optind, err := getopt.Getopts(args, "H:T:eE")
- if err != nil {
- return "", "", false, err
- }
- for _, opt := range opts {
- switch opt.Option {
- case 'H':
- if strings.Contains(opt.Value, ":") {
- // ensure first colon is followed by a single space
- re := regexp.MustCompile(`^(.*?):\s*(.*)`)
- headers += re.ReplaceAllString(opt.Value, "$1: $2") + "\n"
- } else {
- headers += opt.Value + ":\n"
- }
- case 'T':
- template = opt.Value
- case 'e':
- editHeaders = true
- case 'E':
- editHeaders = false
- }
- }
- posargs := args[optind:]
- if len(posargs) > 1 {
- return "", "", false, errors.New("Usage: compose [-H header] [-T template] [-e|-E] [body]")
- }
- if len(posargs) == 1 {
- body = posargs[0]
- }
- if headers != "" {
- if len(body) > 0 {
- body = headers + "\n" + body
- } else {
- body = headers + "\n\n"
- }
- }
- return body, template, editHeaders, nil
-}