Skip to content

Commit c23e489

Browse files
authored
Merge pull request #103 from salmonumbrella/fix/gmail-thread-read
fix(gmail): allow read alias for threads
2 parents 16cdb14 + 6d37254 commit c23e489

4 files changed

Lines changed: 77 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- Docs: update Gmail command examples in README. (#95) — thanks @chrisrodz.
2020
- Contacts: include birthdays in contact get output. (#102) — thanks @salmonumbrella.
2121
- Calendar: force custom reminders payload to send UseDefault=false. (#100) — thanks @salmonumbrella.
22+
- Gmail: add read alias + default thread get. (#103) — thanks @salmonumbrella.
2223

2324
## 0.7.0 - 2026-01-17
2425

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"net/http/httptest"
8+
"strings"
9+
"testing"
10+
11+
"google.golang.org/api/gmail/v1"
12+
"google.golang.org/api/option"
13+
)
14+
15+
func TestExecute_GmailThreadAliases(t *testing.T) {
16+
origNew := newGmailService
17+
t.Cleanup(func() { newGmailService = origNew })
18+
19+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
20+
path := strings.TrimPrefix(r.URL.Path, "/gmail/v1")
21+
switch {
22+
case r.Method == http.MethodGet && path == "/users/me/threads/t1":
23+
w.Header().Set("Content-Type", "application/json")
24+
_ = json.NewEncoder(w).Encode(map[string]any{
25+
"id": "t1",
26+
"messages": []map[string]any{
27+
{
28+
"id": "m1",
29+
"payload": map[string]any{
30+
"headers": []map[string]any{
31+
{"name": "From", "value": "me@example.com"},
32+
{"name": "To", "value": "you@example.com"},
33+
{"name": "Subject", "value": "Hello"},
34+
{"name": "Date", "value": "Wed, 21 Jan 2026 12:00:00 +0000"},
35+
},
36+
},
37+
},
38+
},
39+
})
40+
return
41+
default:
42+
http.NotFound(w, r)
43+
return
44+
}
45+
}))
46+
defer srv.Close()
47+
48+
svc, err := gmail.NewService(context.Background(),
49+
option.WithoutAuthentication(),
50+
option.WithHTTPClient(srv.Client()),
51+
option.WithEndpoint(srv.URL+"/"),
52+
)
53+
if err != nil {
54+
t.Fatalf("NewService: %v", err)
55+
}
56+
newGmailService = func(context.Context, string) (*gmail.Service, error) { return svc, nil }
57+
58+
cases := [][]string{
59+
{"--plain", "--account", "a@b.com", "gmail", "read", "t1"},
60+
{"--plain", "--account", "a@b.com", "gmail", "thread", "t1"},
61+
}
62+
for _, args := range cases {
63+
out := captureStdout(t, func() {
64+
_ = captureStderr(t, func() {
65+
if execErr := Execute(args); execErr != nil {
66+
t.Fatalf("Execute %v: %v", args, execErr)
67+
}
68+
})
69+
})
70+
if !strings.Contains(out, "Thread contains 1 message(s)") {
71+
t.Fatalf("unexpected output for %v: %q", args, out)
72+
}
73+
}
74+
}

internal/cmd/gmail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var newGmailService = googleapi.NewGmail
2020

2121
type GmailCmd struct {
2222
Search GmailSearchCmd `cmd:"" name:"search" group:"Read" help:"Search threads using Gmail query syntax"`
23-
Thread GmailThreadCmd `cmd:"" name:"thread" group:"Organize" help:"Thread operations (get, modify)"`
23+
Thread GmailThreadCmd `cmd:"" name:"thread" aliases:"read" group:"Organize" help:"Thread operations (get, modify)"`
2424
Get GmailGetCmd `cmd:"" name:"get" group:"Read" help:"Get a message (full|metadata|raw)"`
2525
Attachment GmailAttachmentCmd `cmd:"" name:"attachment" group:"Read" help:"Download a single attachment"`
2626
URL GmailURLCmd `cmd:"" name:"url" group:"Read" help:"Print Gmail web URLs for threads"`

internal/cmd/gmail_thread.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func stripHTMLTags(s string) string {
4343
}
4444

4545
type GmailThreadCmd struct {
46-
Get GmailThreadGetCmd `cmd:"" name:"get" help:"Get a thread with all messages (optionally download attachments)"`
46+
Get GmailThreadGetCmd `cmd:"" name:"get" default:"withargs" help:"Get a thread with all messages (optionally download attachments)"`
4747
Modify GmailThreadModifyCmd `cmd:"" name:"modify" help:"Modify labels on all messages in a thread"`
4848
Attachments GmailThreadAttachmentsCmd `cmd:"" name:"attachments" help:"List all attachments in a thread"`
4949
}

0 commit comments

Comments
 (0)