-
Notifications
You must be signed in to change notification settings - Fork 1
Add Inbound Email API commands #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
44d7bd0
5db4884
2c7ae49
e25a83d
3c4565d
16d45b7
5dda99a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package cmdutil | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // EmailAddr represents an email address with an optional display name. | ||
| type EmailAddr struct { | ||
| Email string `json:"email"` | ||
| Name string `json:"name,omitempty"` | ||
| } | ||
|
|
||
| var emailAddrRe = regexp.MustCompile(`^(.+?)\s*<([^>]+)>$`) | ||
|
|
||
| // ParseEmailAddr parses an email address string in either "email" or "Name <email>" format. | ||
| func ParseEmailAddr(s string) (EmailAddr, error) { | ||
| s = strings.TrimSpace(s) | ||
| if s == "" { | ||
| return EmailAddr{}, fmt.Errorf("empty email address") | ||
| } | ||
|
|
||
| // Try "Name <email>" format. | ||
| if matches := emailAddrRe.FindStringSubmatch(s); matches != nil { | ||
| return EmailAddr{ | ||
| Name: strings.TrimSpace(matches[1]), | ||
| Email: strings.TrimSpace(matches[2]), | ||
| }, nil | ||
| } | ||
|
|
||
| // Plain email address. | ||
| return EmailAddr{Email: s}, nil | ||
| } | ||
|
|
||
| // ParseEmailAddrs parses a slice of email address strings. | ||
| func ParseEmailAddrs(addrs []string) ([]EmailAddr, error) { | ||
| result := make([]EmailAddr, 0, len(addrs)) | ||
| for _, s := range addrs { | ||
| addr, err := ParseEmailAddr(s) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid email address %q: %w", s, err) | ||
| } | ||
| result = append(result, addr) | ||
| } | ||
| return result, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package emaillogs | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||
| "net/url" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| "github.com/mailtrap/mailtrap-cli/internal/client" | ||||||||||||||||||||||||||||||||||
|
|
@@ -12,12 +13,16 @@ import ( | |||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type EmailLog struct { | ||||||||||||||||||||||||||||||||||
| MessageID string `json:"message_id"` | ||||||||||||||||||||||||||||||||||
| Subject string `json:"subject"` | ||||||||||||||||||||||||||||||||||
| From string `json:"from"` | ||||||||||||||||||||||||||||||||||
| To string `json:"to"` | ||||||||||||||||||||||||||||||||||
| Status string `json:"status"` | ||||||||||||||||||||||||||||||||||
| SentAt string `json:"sent_at"` | ||||||||||||||||||||||||||||||||||
| MessageID string `json:"message_id"` | ||||||||||||||||||||||||||||||||||
| Subject string `json:"subject"` | ||||||||||||||||||||||||||||||||||
| From string `json:"from"` | ||||||||||||||||||||||||||||||||||
| To string `json:"to"` | ||||||||||||||||||||||||||||||||||
| Status string `json:"status"` | ||||||||||||||||||||||||||||||||||
| SentAt string `json:"sent_at"` | ||||||||||||||||||||||||||||||||||
| RFCMessageID string `json:"rfc_message_id,omitempty"` | ||||||||||||||||||||||||||||||||||
| InReplyTo string `json:"in_reply_to,omitempty"` | ||||||||||||||||||||||||||||||||||
| References []string `json:"references,omitempty"` | ||||||||||||||||||||||||||||||||||
| ThreadID string `json:"thread_id,omitempty"` | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| type emailLogListResponse struct { | ||||||||||||||||||||||||||||||||||
|
|
@@ -33,6 +38,7 @@ var emailLogColumns = []output.Column{ | |||||||||||||||||||||||||||||||||
| {Header: "TO", Field: "to"}, | ||||||||||||||||||||||||||||||||||
| {Header: "STATUS", Field: "status"}, | ||||||||||||||||||||||||||||||||||
| {Header: "SENT AT", Field: "sent_at"}, | ||||||||||||||||||||||||||||||||||
| {Header: "THREAD ID", Field: "thread_id"}, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func NewCmdList(f *cmdutil.Factory) *cobra.Command { | ||||||||||||||||||||||||||||||||||
|
|
@@ -110,7 +116,13 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| format := cmdutil.GetOutputFormat() | ||||||||||||||||||||||||||||||||||
| return output.Print(f.IOStreams.Out, format, resp.Messages, emailLogColumns) | ||||||||||||||||||||||||||||||||||
| if err := output.Print(f.IOStreams.Out, format, resp.Messages, emailLogColumns); err != nil { | ||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if format != output.FormatJSON && resp.NextPageCursor != "" { | ||||||||||||||||||||||||||||||||||
| fmt.Fprintf(f.IOStreams.Out, "\nNext page: --cursor %s\n", resp.NextPageCursor) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+119
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Propagate pagination output errors.
Proposed fix- fmt.Fprintf(f.IOStreams.Out, "\nNext page: --cursor %s\n", resp.NextPageCursor)
+ if _, err := fmt.Fprintf(f.IOStreams.Out, "\nNext page: --cursor %s\n", resp.NextPageCursor); err != nil {
+ return err
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package folders | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/mailtrap/mailtrap-cli/internal/client" | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/mailtrap/mailtrap-cli/internal/output" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdCreate(f *cmdutil.Factory) *cobra.Command { | ||
| var name string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create an inbound folder", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := cmdutil.RequireFlag("name", name); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| c, err := f.NewClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| body := map[string]interface{}{"name": name} | ||
|
|
||
| var resp InboundFolder | ||
| if err := c.Post(context.Background(), client.BaseGeneral, "/api/inbound/folders", body, &resp); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return output.Print(f.IOStreams.Out, cmdutil.GetOutputFormat(), resp, folderColumns) | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&name, "name", "", "Folder name (required)") | ||
|
|
||
| return cmd | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package folders | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/mailtrap/mailtrap-cli/internal/client" | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func NewCmdDelete(f *cmdutil.Factory) *cobra.Command { | ||
| var folderID string | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "delete", | ||
| Short: "Delete an inbound folder", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if err := cmdutil.RequireFlag("id", folderID); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| c, err := f.NewClient() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| path := fmt.Sprintf("/api/inbound/folders/%s", folderID) | ||
|
|
||
| if err := c.Delete(context.Background(), client.BaseGeneral, path, nil); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fmt.Fprintln(f.IOStreams.Out, "Inbound folder deleted successfully.") | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().StringVar(&folderID, "id", "", "Folder ID (required)") | ||
|
|
||
| return cmd | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package folders | ||
|
|
||
| import ( | ||
| "github.com/mailtrap/mailtrap-cli/internal/cmdutil" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| // NewCmdFolders creates the `inbound folders` command group. | ||
| func NewCmdFolders(f *cmdutil.Factory) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "folders", | ||
| Short: "Manage inbound folders", | ||
| } | ||
|
|
||
| cmd.AddCommand(NewCmdList(f)) | ||
| cmd.AddCommand(NewCmdGet(f)) | ||
| cmd.AddCommand(NewCmdCreate(f)) | ||
| cmd.AddCommand(NewCmdUpdate(f)) | ||
| cmd.AddCommand(NewCmdDelete(f)) | ||
|
|
||
| return cmd | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.