From 5a141459719142e266f44280b082d5c31b535cf9 Mon Sep 17 00:00:00 2001 From: David Banham Date: Sat, 17 Mar 2018 21:53:04 +1100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Gopkg.lock | 32 ++++++++++++++++ Gopkg.toml | 30 +++++++++++++++ notifications.go | 85 +++++++++++++++++++++++++++++++++++++++++++ notifications_test.go | 19 ++++++++++ 5 files changed, 167 insertions(+) create mode 100644 .gitignore create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 notifications.go create mode 100644 notifications_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61ead86 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..890e1d8 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,32 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/aws/aws-sdk-go" + packages = ["aws","aws/awserr","aws/awsutil","aws/client","aws/client/metadata","aws/corehandlers","aws/credentials","aws/credentials/ec2rolecreds","aws/credentials/endpointcreds","aws/credentials/stscreds","aws/defaults","aws/ec2metadata","aws/endpoints","aws/request","aws/session","aws/signer/v4","internal/sdkio","internal/sdkrand","internal/shareddefaults","private/protocol","private/protocol/query","private/protocol/query/queryutil","private/protocol/rest","private/protocol/xml/xmlutil","service/ses","service/sts"] + revision = "f0872da8a448f8cb10f4f0c95c2100e4f7356448" + version = "v1.13.16" + +[[projects]] + branch = "master" + name = "github.com/davidbanham/required_env" + packages = ["."] + revision = "a84628a4c24414ddb921e4a100dc6656d312ce37" + +[[projects]] + name = "github.com/go-ini/ini" + packages = ["."] + revision = "6333e38ac20b8949a8dd68baa3650f4dee8f39f0" + version = "v1.33.0" + +[[projects]] + name = "github.com/jmespath/go-jmespath" + packages = ["."] + revision = "0b12d6b5" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "b176bd447f3f857392e67800942d9d9d4d33ca1c4c2408fcf3d9cb89df81932d" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..5d245cd --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,30 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + name = "github.com/aws/aws-sdk-go" + version = "1.13.16" + +[[constraint]] + branch = "master" + name = "github.com/davidbanham/required_env" diff --git a/notifications.go b/notifications.go new file mode 100644 index 0000000..763eb4e --- /dev/null +++ b/notifications.go @@ -0,0 +1,85 @@ +package notifications + +import ( + "log" + "os" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ses" + "github.com/davidbanham/required_env" +) + +var svc *ses.SES +var testMode bool + +func init() { + if os.Getenv("TEST_MOCKS_ON") == "true" { + testMode = true + return + } + required_env.Ensure(map[string]string{ + "AWS_ACCESS_KEY_ID": "", + "AWS_SECRET_ACCESS_KEY": "", + }) + sess, err := session.NewSession(&aws.Config{ + Region: aws.String("us-east-1"), + }) + if err != nil { + log.Fatal(err) + } + svc = ses.New(sess) +} + +type Email struct { + To string + From string + ReplyTo string + Text string + HTML string + Subject string +} + +func SendEmail(email Email) (err error) { + if testMode { + log.Println("INFO notifications TESTMODE dropping email to", email.To, "from", email.From) + return + } + log.Println("INFO notifications sending email to", email.To, "from", email.From) + + body := &ses.Body{ + Text: &ses.Content{ + Data: aws.String(email.Text), + Charset: aws.String("UTF8"), + }, + } + + if email.HTML != "" { + body.Html = &ses.Content{ + Data: aws.String(email.HTML), + Charset: aws.String("UTF8"), + } + } + + params := &ses.SendEmailInput{ + Destination: &ses.Destination{ + ToAddresses: []*string{ + aws.String(email.To), + }, + }, + Message: &ses.Message{ + Body: body, + Subject: &ses.Content{ + Data: aws.String(email.Subject), + Charset: aws.String("UTF8"), + }, + }, + Source: aws.String(email.From), + ReplyToAddresses: []*string{ + aws.String(email.ReplyTo), + }, + } + _, err = svc.SendEmail(params) + + return +} diff --git a/notifications_test.go b/notifications_test.go new file mode 100644 index 0000000..967eafd --- /dev/null +++ b/notifications_test.go @@ -0,0 +1,19 @@ +package notifications + +import ( + "testing" +) + +func TestNotificationsLive(t *testing.T) { + err := SendEmail(Email{ + To: "david@banham.id.au", + From: "testrun@takehome.io", + ReplyTo: "lolwut@takehome.io", + Text: "this is a test run", + HTML: "this is a test run", + Subject: "test run", + }) + if err != nil { + t.Fatal(err) + } +}