-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtemplate.go
60 lines (52 loc) · 1.48 KB
/
template.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
// vpnnotify - notify a user of their brand new vpn session!
// template.go: code related to building a notification
//
// Copyright 2017-2022 F5 Inc.
// Licensed under the BSD 3-clause license; see LICENSE for more information.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"text/template"
"time"
"github.com/oschwald/geoip2-golang"
)
// templInfo is the info we can pass to template.New
type templInfo struct {
City string
Country string
Env string
GeoIP bool
IP string
State string
}
// makeMessage: This is where we actually build the message to go to the user
func makeMessage(config VPNNotifyConfig, lt time.Time, env string, geo *geoip2.City, ip string) string {
rawTemplate, err := ioutil.ReadFile(config.TemplatePath)
if err != nil {
fmt.Printf("%s VPNNotify: Unable to read template for Slack message\n", lt.Format("Mon Jan _2 15:04:05 2006"))
}
tmpl := template.Must(template.New("vpnnotify").Parse(string(rawTemplate)))
var vars templInfo
vars.IP = ip
vars.Env = env
if config.GeoIPEnabled {
vars.GeoIP = true
vars.City = geo.City.Names["en"]
if len(geo.Subdivisions) == 1 {
vars.State = geo.Subdivisions[0].IsoCode
} else {
vars.State = ""
}
vars.Country = geo.Country.IsoCode
} else {
vars.GeoIP = false
}
var rendered bytes.Buffer
err = tmpl.Execute(&rendered, vars)
if err != nil {
fmt.Printf("%s VPNNotify: Unable to render template for Slack message\n", lt.Format("Mon Jan _2 15:04:05 2006"))
}
return rendered.String()
}