diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..a105d4ac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.48.1 - Unreleased +- Contacts: default `contacts search` and `contacts other search` to `--max 30`, the most results Google returns for these searches, and say so on stderr when a larger `--max` is given. Both previously asked for 50 and showed at most 30 with no sign the list was cut short. (#1323) — thanks @saariuslystoned. + ## 0.48.0 - 2026-11-05 **Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates. diff --git a/docs/commands/gog-contacts-other-search.md b/docs/commands/gog-contacts-other-search.md index f497569ab..88330da04 100644 --- a/docs/commands/gog-contacts-other-search.md +++ b/docs/commands/gog-contacts-other-search.md @@ -31,7 +31,7 @@ gog contacts (contact) other search ... [flags] | `-h`
`--help` | `kong.helpFlag` | | Show context-sensitive help. | | `--home` | `string` | | Override gogcli config/data/state/cache root (equivalent to GOG_HOME) | | `-j`
`--json`
`--machine` | `bool` | false | Output JSON to stdout (best for scripting) | -| `--max`
`--limit` | `int64` | 50 | Max results | +| `--max`
`--limit` | `int64` | 30 | Max results (Google returns at most 30) | | `--no-input`
`--non-interactive`
`--noninteractive` | `bool` | | Never prompt; fail instead (useful for CI) | | `-p`
`--plain`
`--tsv` | `bool` | false | Output stable, parseable text to stdout (TSV; no colors) | | `--quota-project` | `string` | | Google Cloud project to bill for API usage (sent as X-Goog-User-Project; some APIs require it with --access-token or ADC) | diff --git a/docs/commands/gog-contacts-search.md b/docs/commands/gog-contacts-search.md index 43062069e..2b1c7b568 100644 --- a/docs/commands/gog-contacts-search.md +++ b/docs/commands/gog-contacts-search.md @@ -31,7 +31,7 @@ gog contacts (contact) search ... [flags] | `-h`
`--help` | `kong.helpFlag` | | Show context-sensitive help. | | `--home` | `string` | | Override gogcli config/data/state/cache root (equivalent to GOG_HOME) | | `-j`
`--json`
`--machine` | `bool` | false | Output JSON to stdout (best for scripting) | -| `--max`
`--limit` | `int64` | 50 | Max results | +| `--max`
`--limit` | `int64` | 30 | Max results (Google returns at most 30) | | `--no-input`
`--non-interactive`
`--noninteractive` | `bool` | | Never prompt; fail instead (useful for CI) | | `-p`
`--plain`
`--tsv` | `bool` | false | Output stable, parseable text to stdout (TSV; no colors) | | `--quota-project` | `string` | | Google Cloud project to bill for API usage (sent as X-Goog-User-Project; some APIs require it with --access-token or ADC) | diff --git a/internal/cmd/contacts.go b/internal/cmd/contacts.go index 034003a7f..ec52847a7 100644 --- a/internal/cmd/contacts.go +++ b/internal/cmd/contacts.go @@ -28,7 +28,7 @@ type ContactsCmd struct { type ContactsSearchCmd struct { Query []string `arg:"" name:"query" help:"Search query"` - Max int64 `name:"max" aliases:"limit" help:"Max results" default:"50"` + Max int64 `name:"max" aliases:"limit" help:"Max results (Google returns at most 30)" default:"30"` } func (c *ContactsSearchCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -50,7 +50,7 @@ func (c *ContactsSearchCmd) Run(ctx context.Context, flags *RootFlags) error { warmSearchContactsCache(ctx, svc) resp, err := svc.People.SearchContacts(). Query(query). - PageSize(c.Max). + PageSize(peopleSearchPageSize(u, c.Max)). ReadMask(contactsReadMask). Do() if err != nil { @@ -267,3 +267,18 @@ func userDefinedFields(p *people.Person) map[string]string { } return fields } + +// peopleSearchMaxPageSize is the most results people.searchContacts and +// otherContacts.search return: larger pageSize values are capped to 30 and +// neither method returns a page token. +const peopleSearchMaxPageSize = 30 + +func peopleSearchPageSize(u *ui.UI, requested int64) int64 { + if requested <= peopleSearchMaxPageSize { + return requested + } + if u != nil { + u.Err().Linef("# Google returns at most %d contact search results; showing up to %d of the requested %d", peopleSearchMaxPageSize, peopleSearchMaxPageSize, requested) + } + return peopleSearchMaxPageSize +} diff --git a/internal/cmd/contacts_directory.go b/internal/cmd/contacts_directory.go index 9fb671810..ae4d785ab 100644 --- a/internal/cmd/contacts_directory.go +++ b/internal/cmd/contacts_directory.go @@ -305,7 +305,7 @@ func (c *ContactsOtherListCmd) Run(ctx context.Context, flags *RootFlags) error type ContactsOtherSearchCmd struct { Query []string `arg:"" name:"query" help:"Search query"` - Max int64 `name:"max" aliases:"limit" help:"Max results" default:"50"` + Max int64 `name:"max" aliases:"limit" help:"Max results (Google returns at most 30)" default:"30"` } func (c *ContactsOtherSearchCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -328,7 +328,7 @@ func (c *ContactsOtherSearchCmd) Run(ctx context.Context, flags *RootFlags) erro resp, err := svc.OtherContacts.Search(). Query(query). ReadMask(contactsOtherReadMask). - PageSize(c.Max). + PageSize(peopleSearchPageSize(u, c.Max)). Do() if err != nil { return err diff --git a/internal/cmd/contacts_search_page_cap_test.go b/internal/cmd/contacts_search_page_cap_test.go new file mode 100644 index 000000000..76cb3e6f8 --- /dev/null +++ b/internal/cmd/contacts_search_page_cap_test.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" + "sync/atomic" + "testing" +) + +// Both people.searchContacts and otherContacts.search cap pageSize at 30 and +// return no page token, so a larger --max can never be met. +func capped30SearchHandler(endpoint string, sent *atomic.Value) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.URL.Path, endpoint) || r.URL.Query().Get("query") == "" { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"results": []any{}}) + return + } + sent.Store(r.URL.Query().Get("pageSize")) + n, _ := strconv.Atoi(r.URL.Query().Get("pageSize")) + if n > 30 { + n = 30 + } + results := make([]map[string]any, 0, n) + for i := range n { + results = append(results, map[string]any{"person": map[string]any{ + "resourceName": fmt.Sprintf("people/c%d", i), + "names": []map[string]any{{"displayName": fmt.Sprintf("Ada %d", i)}}, + }}) + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"results": results}) + } +} + +func TestContactsSearch_RespectsThe30ResultCap(t *testing.T) { + for _, tc := range []struct { + name string + verb []string + endpoint string + }{ + {name: "contacts search", verb: []string{"contacts", "search", "ada"}, endpoint: "people:searchContacts"}, + {name: "contacts other search", verb: []string{"contacts", "other", "search", "ada"}, endpoint: "otherContacts:search"}, + } { + for _, flags := range []struct { + args []string + wantWarn bool + }{ + {args: nil, wantWarn: false}, + {args: []string{"--max", "50"}, wantWarn: true}, + } { + t.Run(fmt.Sprintf("%s %v", tc.name, flags.args), func(t *testing.T) { + var sent atomic.Value + svc, closeSrv := newPeopleService(t, capped30SearchHandler(tc.endpoint, &sent)) + t.Cleanup(closeSrv) + args := append(append([]string{"--json", "--account", "a@b.com"}, tc.verb...), flags.args...) + res := executeWithPeopleTestServices(t, args, peopleTestServices{ + Contacts: fixedPeopleTestService(svc), + Other: fixedPeopleTestService(svc), + }) + if res.err != nil { + t.Fatalf("Execute: %v", res.err) + } + if got := sent.Load(); got != "30" { + t.Fatalf("pageSize sent = %v, want 30", got) + } + warned := strings.Contains(res.stderr, "30") + if warned != flags.wantWarn { + t.Fatalf("stderr = %q, want warning about the 30-result cap: %v", res.stderr, flags.wantWarn) + } + }) + } + } +}