diff --git a/README.md b/README.md index 1bb51d4..ad7ebd6 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ mailtrap webhooks delete --id 1 # Contacts mailtrap contacts create --email "user@example.com" --first-name "John" mailtrap contact-lists list +mailtrap contact-lists list --search news mailtrap contact-fields create --name "Company" --data-type text --merge-tag "{{company}}" # Sandboxes & projects diff --git a/internal/commands/contact_lists/contact_lists_test.go b/internal/commands/contact_lists/contact_lists_test.go index 29a558d..5cb649b 100644 --- a/internal/commands/contact_lists/contact_lists_test.go +++ b/internal/commands/contact_lists/contact_lists_test.go @@ -116,6 +116,39 @@ func TestContactListsListJSON(t *testing.T) { } } +func TestContactListsListWithSearch(t *testing.T) { + f, buf, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + + searchParam := r.URL.Query().Get("search") + if searchParam != "news" { + t.Errorf("expected search query param 'news', got %q", searchParam) + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode([]map[string]interface{}{ + {"id": 1, "name": "Newsletter"}, + }) + }) + defer cleanup() + + cmd := contact_lists.NewCmdContactLists(f) + cmd.SetArgs([]string{"list", "--search", "news"}) + cmd.SetOut(buf) + + err := cmd.Execute() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + output := buf.String() + if !strings.Contains(output, "Newsletter") { + t.Errorf("expected output to contain 'Newsletter', got:\n%s", output) + } +} + func TestContactListsGet(t *testing.T) { f, buf, cleanup := setupTest(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { diff --git a/internal/commands/contact_lists/list.go b/internal/commands/contact_lists/list.go index c3b2d58..eef8208 100644 --- a/internal/commands/contact_lists/list.go +++ b/internal/commands/contact_lists/list.go @@ -2,6 +2,7 @@ package contact_lists import ( "context" + "net/url" "github.com/mailtrap/mailtrap-cli/internal/client" "github.com/mailtrap/mailtrap-cli/internal/cmdutil" @@ -21,6 +22,8 @@ var contactListColumns = []output.Column{ } func NewCmdList(f *cmdutil.Factory) *cobra.Command { + var search string + cmd := &cobra.Command{ Use: "list", Short: "List all contact lists", @@ -37,8 +40,13 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command { path := cmdutil.AccountPath("contacts", "lists") + query := url.Values{} + if search != "" { + query.Set("search", search) + } + var lists []ContactList - if err := c.Get(context.Background(), client.BaseGeneral, path, nil, &lists); err != nil { + if err := c.Get(context.Background(), client.BaseGeneral, path, query, &lists); err != nil { return err } @@ -47,5 +55,7 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command { }, } + cmd.Flags().StringVar(&search, "search", "", "Filter by name (case-insensitive prefix match)") + return cmd }