|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "errors"
|
5 | 6 | "log"
|
6 | 7 | "net/mail"
|
7 | 8 | "os"
|
8 | 9 | "strings"
|
9 | 10 |
|
10 | 11 | kingpin "github.com/alecthomas/kingpin/v2"
|
11 |
| - |
12 | 12 | skprconfig "github.com/skpr/go-config"
|
| 13 | + |
13 | 14 | defaultprovider "github.com/skpr/mail/internal/provider/default"
|
14 | 15 | "github.com/skpr/mail/internal/provider/ses"
|
15 | 16 | )
|
|
38 | 39 | cliFrom = kingpin.Flag("from", "The from address (ignored)").Short('f').String()
|
39 | 40 | cliRecipientsFromMsg = kingpin.Flag("to-from-message", "Read message for to (ignored)").Short('t').Bool()
|
40 | 41 | 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() |
41 | 43 | )
|
42 | 44 |
|
43 | 45 | func main() {
|
@@ -73,18 +75,21 @@ func main() {
|
73 | 75 | log.Fatalf("failed to read message from stdin: %s", err)
|
74 | 76 | }
|
75 | 77 |
|
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) |
77 | 82 | if err != nil {
|
78 | 83 | log.Fatalf("failed to send message: %s", err)
|
79 | 84 | }
|
80 | 85 | }
|
81 | 86 |
|
82 | 87 | // 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 { |
84 | 89 | // Use AWS if the credentials match what we would expect for IAM.
|
85 | 90 | 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) |
87 | 92 | }
|
88 | 93 |
|
89 |
| - return defaultprovider.Send(to, msg) |
| 94 | + return defaultprovider.Send(ctx, to, msg) |
90 | 95 | }
|
0 commit comments