-
Notifications
You must be signed in to change notification settings - Fork 3
/
notify.go
61 lines (50 loc) · 1.54 KB
/
notify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"context"
"fmt"
"html/template"
"log"
"time"
"github.com/cloudfoundry-community/go-cfclient/v3/resource"
)
func notifySpaceUsers(
ctx context.Context,
cfClient *cfResourceClient,
opts Options,
userGUIDs map[string]bool,
org *resource.Organization,
details SpaceDetails,
mailSender mailer,
) error {
notifyTemplate, err := template.ParseFiles("../../templates/base.html", "../../templates/notify.tmpl")
if err != nil {
return fmt.Errorf("error reading notify template: %w", err)
}
spaceUsers, err := cfClient.Spaces.ListUsersAll(ctx, details.Space.GUID, nil)
if err != nil {
return fmt.Errorf("error listing users on space %s: %w", details.Space.Name, err)
}
recipients, err := listRecipients(userGUIDs, spaceUsers)
if err != nil {
return fmt.Errorf("error listing recipients on space %s: %w", details.Space.Name, err)
}
log.Printf("Notifying space %s; recipients %+v", details.Space.Name, recipients)
if opts.DryRun {
return nil
}
data := map[string]interface{}{
"org": org,
"space": details.Space,
"date": details.Timestamp.Add(24 * time.Duration(opts.PurgeDays) * time.Hour),
"days": opts.PurgeDays,
}
body, err := renderTemplate(notifyTemplate, data)
if err != nil {
return fmt.Errorf("error rendering email: %w", err)
}
log.Printf("sending to %s: %s", recipients, body)
if err := mailSender.sendMail(opts.SMTPOptions, opts.MailSender, opts.NotifyMailSubject, body, recipients); err != nil {
return fmt.Errorf("error sending mail on space %s: %w", details.Space.Name, err)
}
return nil
}