Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions internal/commands/contact_lists/contact_lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion internal/commands/contact_lists/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand All @@ -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
}

Expand All @@ -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
}
Loading