Commit 6613d9b555beec211b2c30d7049bb75957faeee9

Parents: 09744e9272eb5a8951b1fb3d8caa034ca56bdef0

From: Robin Jarry <robin@jarry.cc>
Date: Mon Oct 30 13:12:30 2023 +0700

cf: fix unexpected argument on notmuch
Notmuch supports creating "dynamic" folders with the :cf command. When
the backend is notmuch, :cf must accept multiple arguments since it can
be a free-form notmuch query.

Detect if the backend is notmuch and build a quoted query based on the
provided arguments. Otherwise require a single argument.

Fixes: e54486ee40c9 ("commands: parse arguments with go-opt")
Reported-by: Inwit <inwit@sindominio.net>
Signed-off-by: Robin Jarry <robin@jarry.cc>
Tested-by: Inwit <inwit@sindominio.net>

Stats

commands/account/cf.go +21/-3

Changeset

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
diff --git a/commands/account/cf.go b/commands/account/cf.go
index d73d49780d9fd54b5ad562fa3ab4b650aeefd5d0..fe225e6184cdc30e5841dc6b4937be098b3e39f6 100644
--- a/commands/account/cf.go
+++ b/commands/account/cf.go
@@ -2,16 +2,20 @@ package account
 
 import (
 	"errors"
+	"reflect"
 
 	"git.sr.ht/~rjarry/aerc/app"
 	"git.sr.ht/~rjarry/aerc/commands"
 	"git.sr.ht/~rjarry/aerc/lib/state"
+	"git.sr.ht/~rjarry/aerc/worker/handlers"
+	"git.sr.ht/~rjarry/aerc/worker/types"
+	"git.sr.ht/~rjarry/go-opt"
 )
 
 var history map[string]string
 
 type ChangeFolder struct {
-	Folder string `opt:"folder" complete:"CompleteFolder"`
+	Folder []string `opt:"..." complete:"CompleteFolder"`
 }
 
 func init() {
@@ -32,16 +36,30 @@ 	acct := app.SelectedAccount()
 	if acct == nil {
 		return errors.New("No account selected")
 	}
+
+	var target string
+
+	notmuch, _ := handlers.GetHandlerForScheme("notmuch", new(types.Worker))
+	switch {
+	case reflect.TypeOf(notmuch) == reflect.TypeOf(acct.Worker().Backend):
+		// notmuch query may have arguments that require quoting
+		target = opt.QuoteArgs(c.Folder...).String()
+	case len(c.Folder) == 1:
+		target = c.Folder[0]
+	default:
+		return errors.New("Unexpected argument(s). Usage: cf <folder>")
+	}
+
 	previous := acct.Directories().Selected()
 
-	if c.Folder == "-" {
+	if target == "-" {
 		if dir, ok := history[acct.Name()]; ok {
 			acct.Directories().Select(dir)
 		} else {
 			return errors.New("No previous folder to return to")
 		}
 	} else {
-		acct.Directories().Select(c.Folder)
+		acct.Directories().Select(target)
 	}
 	history[acct.Name()] = previous