From 50e40f8cc270a371d32fa33ea84f95bd73d08512 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Tue, 27 Jun 2023 14:34:28 -0400 Subject: [PATCH] fix: seed attachments from command line --- email.go | 42 +++++++++++++++++++++++------------------- main.go | 9 +++++---- model.go | 4 ++++ 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/email.go b/email.go index d1afe9a..b7a4e15 100644 --- a/email.go +++ b/email.go @@ -43,6 +43,28 @@ func sendEmail(to []string, from, subject, body string, paths []string) error { // If the conversion fails, we'll simply send the plain-text body. _ = goldmark.Convert([]byte(body), html) + request := &resend.SendEmailRequest{ + From: from, + To: to, + Subject: subject, + Html: html.String(), + Text: body, + Attachments: makeAttachments(paths), + } + + _, err := client.Emails.Send(request) + if err != nil { + return err + } + + return nil +} + +func makeAttachments(paths []string) []resend.Attachment { + if len(paths) == 0 { + return nil + } + attachments := make([]resend.Attachment, len(paths)) for i, a := range paths { f, err := os.ReadFile(a) @@ -55,23 +77,5 @@ func sendEmail(to []string, from, subject, body string, paths []string) error { } } - if len(attachments) == 0 { - attachments = nil - } - - request := &resend.SendEmailRequest{ - From: from, - To: to, - Subject: subject, - Html: html.String(), - Text: body, - Attachments: attachments, - } - - _, err := client.Emails.Send(request) - if err != nil { - return err - } - - return nil + return attachments } diff --git a/main.go b/main.go index d9686e2..330a34b 100644 --- a/main.go +++ b/main.go @@ -54,10 +54,11 @@ var rootCmd = &cobra.Command{ } p := tea.NewProgram(NewModel(resend.SendEmailRequest{ - From: from, - To: to, - Subject: subject, - Text: body, + From: from, + To: to, + Subject: subject, + Text: body, + Attachments: makeAttachments(attachments), })) m, err := p.Run() if err != nil { diff --git a/model.go b/model.go index 97a7ffa..b035883 100644 --- a/model.go +++ b/model.go @@ -127,6 +127,10 @@ func NewModel(defaults resend.SendEmailRequest) Model { attachments.SetStatusBarItemName("attachment", "attachments") attachments.SetShowPagination(false) + for _, a := range defaults.Attachments { + attachments.InsertItem(0, attachment(a.Filename)) + } + picker := filepicker.New() picker.CurrentDirectory, _ = os.UserHomeDir()