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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ mailtrap domains send-setup-instructions --id 123 --email "admin@yourdomain.com"
mailtrap templates list
mailtrap templates create --name "Welcome" --subject "Hello {{name}}" --body-html '<h1>Hi!</h1>'

# Webhooks
mailtrap webhooks list
mailtrap webhooks get --id 1
mailtrap webhooks create --url "https://example.com/hooks" --type email_sending --sending-stream transactional --event-types delivery,bounce
mailtrap webhooks update --id 1 --active=false --event-types delivery,bounce,unsubscribe
mailtrap webhooks delete --id 1

# Contacts
mailtrap contacts create --email "user@example.com" --first-name "John"
mailtrap contact-lists list
Expand Down Expand Up @@ -141,6 +148,7 @@ mailtrap domains list --output text
| **Domains** | `domains list`, `domains get`, `domains create`, `domains delete`, `domains send-setup-instructions` |
| **Templates** | `templates list`, `templates get`, `templates create`, `templates update`, `templates delete` |
| **Suppressions** | `suppressions list`, `suppressions delete` |
| **Webhooks** | `webhooks list`, `webhooks get`, `webhooks create`, `webhooks update`, `webhooks delete` |
| **Stats** | `stats get`, `stats by-domain`, `stats by-category`, `stats by-esp`, `stats by-date` |
| **Email Logs** | `email-logs list`, `email-logs get` |
| **Contacts** | `contacts get`, `contacts create`, `contacts update`, `contacts delete`, `contacts import`, `contacts export`, `contacts import-status`, `contacts export-status`, `contacts create-event` |
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/mailtrap/mailtrap-cli/internal/commands/suppressions"
"github.com/mailtrap/mailtrap-cli/internal/commands/templates"
"github.com/mailtrap/mailtrap-cli/internal/commands/tokens"
"github.com/mailtrap/mailtrap-cli/internal/commands/webhooks"
)

func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
Expand Down Expand Up @@ -57,6 +58,7 @@ func NewRootCmd(f *cmdutil.Factory) *cobra.Command {
cmd.AddCommand(stats.NewCmdStats(f))
cmd.AddCommand(templates.NewCmdTemplates(f))
cmd.AddCommand(email_logs.NewCmdEmailLogs(f))
cmd.AddCommand(webhooks.NewCmdWebhooks(f))

// Sandbox
cmd.AddCommand(projects.NewCmdProjects(f))
Expand Down
98 changes: 98 additions & 0 deletions internal/commands/webhooks/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package webhooks

import (
"context"

"github.com/mailtrap/mailtrap-cli/internal/client"
"github.com/mailtrap/mailtrap-cli/internal/cmdutil"
"github.com/mailtrap/mailtrap-cli/internal/config"
"github.com/mailtrap/mailtrap-cli/internal/output"
"github.com/spf13/cobra"
)

type WebhookWithSecret struct {
Webhook
SigningSecret string `json:"signing_secret,omitempty"`
}

type webhookCreateResponse struct {
Data WebhookWithSecret `json:"data"`
}

var webhookCreateColumns = append(append([]output.Column{}, webhookColumns...), output.Column{Header: "SIGNING SECRET", Field: "signing_secret"})

func NewCmdCreate(f *cmdutil.Factory) *cobra.Command {
var (
webhookURL string
webhookType string
active bool
payloadFormat string
sendingStream string
eventTypes []string
domainID int
)

cmd := &cobra.Command{
Use: "create",
Short: "Create a webhook",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.RequireFlag("url", webhookURL); err != nil {
return err
}
if err := cmdutil.RequireFlag("type", webhookType); err != nil {
return err
}

c, err := f.NewClient()
if err != nil {
return err
}

if _, err := config.RequireAccountID(); err != nil {
return err
}

path := cmdutil.AccountPath("webhooks")

webhookFields := map[string]interface{}{
"url": webhookURL,
"webhook_type": webhookType,
}
if cmd.Flags().Changed("active") {
webhookFields["active"] = active
}
if cmd.Flags().Changed("payload-format") {
webhookFields["payload_format"] = payloadFormat
}
if cmd.Flags().Changed("sending-stream") {
webhookFields["sending_stream"] = sendingStream
}
if cmd.Flags().Changed("event-types") {
webhookFields["event_types"] = eventTypes
}
if cmd.Flags().Changed("domain-id") {
webhookFields["domain_id"] = domainID
}

body := map[string]interface{}{"webhook": webhookFields}

var resp webhookCreateResponse
if err := c.Post(context.Background(), client.BaseGeneral, path, body, &resp); err != nil {
return err
}

format := cmdutil.GetOutputFormat()
return output.Print(f.IOStreams.Out, format, resp.Data, webhookCreateColumns)
},
}

cmd.Flags().StringVar(&webhookURL, "url", "", "Webhook URL (required)")
cmd.Flags().StringVar(&webhookType, "type", "", "Webhook type: email_sending, audit_log (required)")
cmd.Flags().BoolVar(&active, "active", true, "Whether the webhook is active")
cmd.Flags().StringVar(&payloadFormat, "payload-format", "", "Payload format: json, jsonlines")
cmd.Flags().StringVar(&sendingStream, "sending-stream", "", "Sending stream: transactional, bulk")
cmd.Flags().StringSliceVar(&eventTypes, "event-types", nil, "Event types (comma-separated): delivery, soft_bounce, bounce, suspension, unsubscribe, open, spam_complaint, click, reject")
cmd.Flags().IntVar(&domainID, "domain-id", 0, "Domain ID to scope the webhook to")

return cmd
}
47 changes: 47 additions & 0 deletions internal/commands/webhooks/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package webhooks

import (
"context"
"fmt"

"github.com/mailtrap/mailtrap-cli/internal/client"
"github.com/mailtrap/mailtrap-cli/internal/cmdutil"
"github.com/mailtrap/mailtrap-cli/internal/config"
"github.com/spf13/cobra"
)

func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
var webhookID string

cmd := &cobra.Command{
Use: "delete",
Short: "Delete a webhook",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.RequireFlag("id", webhookID); err != nil {
return err
}

c, err := f.NewClient()
if err != nil {
return err
}

if _, err := config.RequireAccountID(); err != nil {
return err
}

path := cmdutil.AccountPath("webhooks", webhookID)

if err := c.Delete(context.Background(), client.BaseGeneral, path, nil); err != nil {
return err
}

fmt.Fprintln(f.IOStreams.Out, "Webhook deleted successfully.")
return nil
},
}

cmd.Flags().StringVar(&webhookID, "id", "", "Webhook ID")

return cmd
}
48 changes: 48 additions & 0 deletions internal/commands/webhooks/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package webhooks

import (
"context"

"github.com/mailtrap/mailtrap-cli/internal/client"
"github.com/mailtrap/mailtrap-cli/internal/cmdutil"
"github.com/mailtrap/mailtrap-cli/internal/config"
"github.com/mailtrap/mailtrap-cli/internal/output"
"github.com/spf13/cobra"
)

func NewCmdGet(f *cmdutil.Factory) *cobra.Command {
var webhookID string

cmd := &cobra.Command{
Use: "get",
Short: "Get a webhook",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.RequireFlag("id", webhookID); err != nil {
return err
}

c, err := f.NewClient()
if err != nil {
return err
}

if _, err := config.RequireAccountID(); err != nil {
return err
}

path := cmdutil.AccountPath("webhooks", webhookID)

var resp webhookResponse
if err := c.Get(context.Background(), client.BaseGeneral, path, nil, &resp); err != nil {
return err
}

format := cmdutil.GetOutputFormat()
return output.Print(f.IOStreams.Out, format, resp.Data, webhookColumns)
},
}

cmd.Flags().StringVar(&webhookID, "id", "", "Webhook ID")

return cmd
}
70 changes: 70 additions & 0 deletions internal/commands/webhooks/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package webhooks

import (
"context"

"github.com/mailtrap/mailtrap-cli/internal/client"
"github.com/mailtrap/mailtrap-cli/internal/cmdutil"
"github.com/mailtrap/mailtrap-cli/internal/config"
"github.com/mailtrap/mailtrap-cli/internal/output"
"github.com/spf13/cobra"
)

type Webhook struct {
ID int `json:"id"`
URL string `json:"url"`
Active bool `json:"active"`
WebhookType string `json:"webhook_type"`
PayloadFormat string `json:"payload_format"`
SendingStream *string `json:"sending_stream,omitempty"`
DomainID *int `json:"domain_id,omitempty"`
EventTypes []string `json:"event_types,omitempty"`
}

type webhookListResponse struct {
Data []Webhook `json:"data"`
}

type webhookResponse struct {
Data Webhook `json:"data"`
}

var webhookColumns = []output.Column{
{Header: "ID", Field: "id"},
{Header: "URL", Field: "url"},
{Header: "TYPE", Field: "webhook_type"},
{Header: "ACTIVE", Field: "active"},
{Header: "FORMAT", Field: "payload_format"},
{Header: "STREAM", Field: "sending_stream"},
{Header: "DOMAIN ID", Field: "domain_id"},
{Header: "EVENTS", Field: "event_types"},
}

func NewCmdList(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "List all webhooks",
RunE: func(cmd *cobra.Command, args []string) error {
c, err := f.NewClient()
if err != nil {
return err
}

if _, err := config.RequireAccountID(); err != nil {
return err
}

path := cmdutil.AccountPath("webhooks")

var resp webhookListResponse
if err := c.Get(context.Background(), client.BaseGeneral, path, nil, &resp); err != nil {
return err
}

format := cmdutil.GetOutputFormat()
return output.Print(f.IOStreams.Out, format, resp.Data, webhookColumns)
},
}

return cmd
}
74 changes: 74 additions & 0 deletions internal/commands/webhooks/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package webhooks

import (
"context"

"github.com/mailtrap/mailtrap-cli/internal/client"
"github.com/mailtrap/mailtrap-cli/internal/cmdutil"
"github.com/mailtrap/mailtrap-cli/internal/config"
"github.com/mailtrap/mailtrap-cli/internal/output"
"github.com/spf13/cobra"
)

func NewCmdUpdate(f *cmdutil.Factory) *cobra.Command {
var (
webhookID string
webhookURL string
active bool
payloadFormat string
eventTypes []string
)

cmd := &cobra.Command{
Use: "update",
Short: "Update a webhook",
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmdutil.RequireFlag("id", webhookID); err != nil {
return err
}

c, err := f.NewClient()
if err != nil {
return err
}

if _, err := config.RequireAccountID(); err != nil {
return err
}

path := cmdutil.AccountPath("webhooks", webhookID)

webhookFields := map[string]interface{}{}
if cmd.Flags().Changed("url") {
webhookFields["url"] = webhookURL
}
if cmd.Flags().Changed("active") {
webhookFields["active"] = active
}
if cmd.Flags().Changed("payload-format") {
webhookFields["payload_format"] = payloadFormat
}
if cmd.Flags().Changed("event-types") {
webhookFields["event_types"] = eventTypes
}

body := map[string]interface{}{"webhook": webhookFields}

var resp webhookResponse
if err := c.Patch(context.Background(), client.BaseGeneral, path, body, &resp); err != nil {
return err
}

format := cmdutil.GetOutputFormat()
return output.Print(f.IOStreams.Out, format, resp.Data, webhookColumns)
},
}

cmd.Flags().StringVar(&webhookID, "id", "", "Webhook ID (required)")
cmd.Flags().StringVar(&webhookURL, "url", "", "Webhook URL")
cmd.Flags().BoolVar(&active, "active", true, "Whether the webhook is active")
cmd.Flags().StringVar(&payloadFormat, "payload-format", "", "Payload format: json, jsonlines")
cmd.Flags().StringSliceVar(&eventTypes, "event-types", nil, "Event types (comma-separated): delivery, soft_bounce, bounce, suspension, unsubscribe, open, spam_complaint, click, reject")

return cmd
}
Loading
Loading