Skip to content

Commit

Permalink
Adds context (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickschuch authored Aug 6, 2024
1 parent aa30bbc commit 406c000
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
13 changes: 11 additions & 2 deletions internal/provider/default/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package local

import (
"context"
"fmt"
"log"
"net/mail"
Expand All @@ -22,7 +23,15 @@ const (
)

// Send the email to Mailhog.
func Send(to []string, msg *mail.Message) error {
func Send(ctx context.Context, to []string, msg *mail.Message) error {
// The GO SMTP package is difficult to cancel using context.
// This provider should only ever be used for local development tasks.
go func() {
<-ctx.Done()
fmt.Println("Context cancelled")
os.Exit(1)
}()

data, err := mailutils.MessageToBytes(msg)
if err != nil {
return err
Expand All @@ -38,7 +47,7 @@ func Send(to []string, msg *mail.Message) error {
}

from := os.Getenv(EnvFrom)
if addr == "" {
if from == "" {
addr = FallbackFrom
}

Expand Down
6 changes: 3 additions & 3 deletions internal/provider/ses/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
const AccessKeyPrefix = "AKIA"

// Send email via AWS SES.
func Send(region, username, password, from string, to []string, msg *mail.Message) error {
cfg, err := config.LoadDefaultConfig(context.TODO(),
func Send(ctx context.Context, region, username, password, from string, to []string, msg *mail.Message) error {
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(region),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(username, password, ""),
Expand Down Expand Up @@ -53,7 +53,7 @@ func Send(region, username, password, from string, to []string, msg *mail.Messag
Source: aws.String(from),
}

output, err := ses.NewFromConfig(cfg).SendRawEmail(context.TODO(), input)
output, err := ses.NewFromConfig(cfg).SendRawEmail(ctx, input)
if err != nil {
return fmt.Errorf("failed to send message via ses %w", err)
}
Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"context"
"errors"
"log"
"net/mail"
"os"
"strings"

kingpin "github.com/alecthomas/kingpin/v2"

skprconfig "github.com/skpr/go-config"

defaultprovider "github.com/skpr/mail/internal/provider/default"
"github.com/skpr/mail/internal/provider/ses"
)
Expand Down Expand Up @@ -38,6 +39,7 @@ var (
cliFrom = kingpin.Flag("from", "The from address (ignored)").Short('f').String()
cliRecipientsFromMsg = kingpin.Flag("to-from-message", "Read message for to (ignored)").Short('t').Bool()
cliIgnoreDots = kingpin.Flag("ignore-dots", "Ignore dots alone on lines by themselves in incoming messages (ignored).").Short('i').Bool()
cliTimeout = kingpin.Flag("timeout", "How long to wait before timing out and exiting.").Default("30s").Duration()
)

func main() {
Expand Down Expand Up @@ -73,18 +75,21 @@ func main() {
log.Fatalf("failed to read message from stdin: %s", err)
}

err = send(region, username, password, from, *cliTo, msg)
ctx, cancel := context.WithTimeout(context.Background(), *cliTimeout)
defer cancel()

err = send(ctx, region, username, password, from, *cliTo, msg)
if err != nil {
log.Fatalf("failed to send message: %s", err)
}
}

// Send email based on parameters.
func send(region, username, password, from string, to []string, msg *mail.Message) error {
func send(ctx context.Context, region, username, password, from string, to []string, msg *mail.Message) error {
// Use AWS if the credentials match what we would expect for IAM.
if strings.HasPrefix(username, ses.AccessKeyPrefix) {
return ses.Send(region, username, password, from, to, msg)
return ses.Send(ctx, region, username, password, from, to, msg)
}

return defaultprovider.Send(to, msg)
return defaultprovider.Send(ctx, to, msg)
}

0 comments on commit 406c000

Please sign in to comment.