Skip to content

Commit 406c000

Browse files
authored
Adds context (#18)
1 parent aa30bbc commit 406c000

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

internal/provider/default/default.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package local
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"net/mail"
@@ -22,7 +23,15 @@ const (
2223
)
2324

2425
// Send the email to Mailhog.
25-
func Send(to []string, msg *mail.Message) error {
26+
func Send(ctx context.Context, to []string, msg *mail.Message) error {
27+
// The GO SMTP package is difficult to cancel using context.
28+
// This provider should only ever be used for local development tasks.
29+
go func() {
30+
<-ctx.Done()
31+
fmt.Println("Context cancelled")
32+
os.Exit(1)
33+
}()
34+
2635
data, err := mailutils.MessageToBytes(msg)
2736
if err != nil {
2837
return err
@@ -38,7 +47,7 @@ func Send(to []string, msg *mail.Message) error {
3847
}
3948

4049
from := os.Getenv(EnvFrom)
41-
if addr == "" {
50+
if from == "" {
4251
addr = FallbackFrom
4352
}
4453

internal/provider/ses/ses.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
const AccessKeyPrefix = "AKIA"
2121

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

56-
output, err := ses.NewFromConfig(cfg).SendRawEmail(context.TODO(), input)
56+
output, err := ses.NewFromConfig(cfg).SendRawEmail(ctx, input)
5757
if err != nil {
5858
return fmt.Errorf("failed to send message via ses %w", err)
5959
}

main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"log"
67
"net/mail"
78
"os"
89
"strings"
910

1011
kingpin "github.com/alecthomas/kingpin/v2"
11-
1212
skprconfig "github.com/skpr/go-config"
13+
1314
defaultprovider "github.com/skpr/mail/internal/provider/default"
1415
"github.com/skpr/mail/internal/provider/ses"
1516
)
@@ -38,6 +39,7 @@ var (
3839
cliFrom = kingpin.Flag("from", "The from address (ignored)").Short('f').String()
3940
cliRecipientsFromMsg = kingpin.Flag("to-from-message", "Read message for to (ignored)").Short('t').Bool()
4041
cliIgnoreDots = kingpin.Flag("ignore-dots", "Ignore dots alone on lines by themselves in incoming messages (ignored).").Short('i').Bool()
42+
cliTimeout = kingpin.Flag("timeout", "How long to wait before timing out and exiting.").Default("30s").Duration()
4143
)
4244

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

76-
err = send(region, username, password, from, *cliTo, msg)
78+
ctx, cancel := context.WithTimeout(context.Background(), *cliTimeout)
79+
defer cancel()
80+
81+
err = send(ctx, region, username, password, from, *cliTo, msg)
7782
if err != nil {
7883
log.Fatalf("failed to send message: %s", err)
7984
}
8085
}
8186

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

89-
return defaultprovider.Send(to, msg)
94+
return defaultprovider.Send(ctx, to, msg)
9095
}

0 commit comments

Comments
 (0)