aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2023-06-21 22:13:03 +0200
committerRobin Jarry <robin@jarry.cc>2023-06-22 10:55:25 +0200
commit822bd3620a456fefcdb828f2768c0677e4442f05 (patch)
treece304de9f3e88599b557c4c3576d2656a5831348
parent0f37a0ce2cdc07a090e33bbe53b1b67081cce741 (diff)
downloadaerc-822bd3620a456fefcdb828f2768c0677e4442f05.tar.gz
aerc-822bd3620a456fefcdb828f2768c0677e4442f05.zip
lib: parse query-map and folder-map files
Combine the query-map and folder-map parsing functionality. Add tests. Signed-off-by: Koni Marti <koni.marti@gmail.com> Tested-by: Bence Ferdinandy <bence@ferdinandy.com> Signed-off-by: Robin Jarry <robin@jarry.cc>
-rw-r--r--worker/lib/foldermap.go34
-rw-r--r--worker/lib/foldermap_test.go54
-rw-r--r--worker/notmuch/worker.go19
3 files changed, 90 insertions, 17 deletions
diff --git a/worker/lib/foldermap.go b/worker/lib/foldermap.go
new file mode 100644
index 00000000..4fd15979
--- /dev/null
+++ b/worker/lib/foldermap.go
@@ -0,0 +1,34 @@
+package lib
+
+import (
+ "fmt"
+ "io"
+
+ "github.com/go-ini/ini"
+)
+
+func ParseFolderMap(r io.Reader) (map[string]string, []string, error) {
+ cfg, err := ini.Load(r)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ sec, err := cfg.GetSection("")
+ if err != nil {
+ return nil, nil, err
+ }
+
+ order := sec.KeyStrings()
+
+ for _, k := range order {
+ v, err := sec.GetKey(k)
+ switch {
+ case v.String() == "":
+ return nil, nil, fmt.Errorf("no value for key '%s'", k)
+ case err != nil:
+ return nil, nil, err
+ }
+ }
+
+ return sec.KeysHash(), order, nil
+}
diff --git a/worker/lib/foldermap_test.go b/worker/lib/foldermap_test.go
new file mode 100644
index 00000000..ec8b6d52
--- /dev/null
+++ b/worker/lib/foldermap_test.go
@@ -0,0 +1,54 @@
+package lib_test
+
+import (
+ "reflect"
+ "strings"
+ "testing"
+
+ "git.sr.ht/~rjarry/aerc/worker/lib"
+)
+
+func TestFolderMap(t *testing.T) {
+ text := `#this is comment
+
+ Sent = [Gmail]/Sent
+
+ # a comment between entries
+ Spam=[Gmail]/Spam # this is comment after the values
+ `
+ fmap, order, err := lib.ParseFolderMap(strings.NewReader(text))
+ if err != nil {
+ t.Errorf("parsing failed: %v", err)
+ }
+
+ want_map := map[string]string{
+ "Sent": "[Gmail]/Sent",
+ "Spam": "[Gmail]/Spam",
+ }
+ want_order := []string{"Sent", "Spam"}
+
+ if !reflect.DeepEqual(order, want_order) {
+ t.Errorf("order is not correct; want: %v, got: %v",
+ want_order, order)
+ }
+
+ if !reflect.DeepEqual(fmap, want_map) {
+ t.Errorf("map is not correct; want: %v, got: %v",
+ want_map, fmap)
+ }
+}
+
+func TestFolderMap_ExpectFails(t *testing.T) {
+ tests := []string{
+ `key = `,
+ ` = value`,
+ ` = `,
+ `key = #value`,
+ }
+ for _, text := range tests {
+ _, _, err := lib.ParseFolderMap(strings.NewReader(text))
+ if err == nil {
+ t.Errorf("expected to fail, but it did not: %v", text)
+ }
+ }
+}
diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go
index f47f3889..70dca1e2 100644
--- a/worker/notmuch/worker.go
+++ b/worker/notmuch/worker.go
@@ -4,7 +4,6 @@
package notmuch
import (
- "bufio"
"bytes"
"context"
"errors"
@@ -610,22 +609,8 @@ func (w *worker) loadQueryMap(acctConfig *config.AccountConfig) error {
return err
}
defer f.Close()
- w.nameQueryMap = make(map[string]string)
- scanner := bufio.NewScanner(f)
- for scanner.Scan() {
- line := scanner.Text()
- if line == "" || line[0] == '#' {
- continue
- }
-
- split := strings.SplitN(line, "=", 2)
- if len(split) != 2 {
- return fmt.Errorf("%v: invalid line %q, want name=query", file, line)
- }
- w.nameQueryMap[strings.TrimSpace(split[0])] = split[1]
- w.queryMapOrder = append(w.queryMapOrder, strings.TrimSpace(split[0]))
- }
- return nil
+ w.nameQueryMap, w.queryMapOrder, err = lib.ParseFolderMap(f)
+ return err
}
func (w *worker) loadExcludeTags(