-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
78 lines (65 loc) · 1.84 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2023 Julian Müller (ChaoticByte)
package main
import (
"bytes"
"text/template"
)
const DEFAULT_SUBJECT_TEMPLATE = "[{{ .Classification }}] {{ .Title }}"
const DEFAULT_BODY_TEMPLATE = `{{ if .Status }}[{{ .Status }}] {{ end }}{{ .Name }}
-> {{ .PortalUrl }}
{{- if eq .NoPatch "true" }}
No patch available!
{{- end }}
{{ if gt .Basescore -1 }}
Basescore: {{ .Basescore }}{{- end }}
Published: {{ .Published }}
{{- if .ProductNames }}
Affected Products:{{ range $product := .ProductNames }}
- {{ $product }}
{{- end }}{{ end }}
{{- if .Cves }}
Assigned CVEs:{{ range $cve := .Cves }}
- {{ $cve }} -> https://www.cve.org/CVERecord?id={{ $cve }}
{{- end }}{{ end }}
Sent by WidNotifier {{ .WidNotifierVersion }}
`
type TemplateData struct {
WidNotice
WidNotifierVersion string
}
type MailTemplateConfig struct {
SubjectTemplate string `json:"subject"`
BodyTemplate string `json:"body"`
}
type MailTemplate struct {
SubjectTemplate template.Template
BodyTemplate template.Template
}
func (t MailTemplate) generate(data TemplateData) (MailContent, error) {
c := MailContent{}
buffer := &bytes.Buffer{}
err := t.SubjectTemplate.Execute(buffer, data)
if err != nil { return c, err }
c.Subject = buffer.String()
buffer.Truncate(0) // we can recycle our buffer
err = t.BodyTemplate.Execute(buffer, data)
if err != nil { return c, err }
c.Body = buffer.String()
return c, nil
}
func NewTemplateFromTemplateConfig(tc MailTemplateConfig) MailTemplate {
subjectTemplate, err := template.New("subject").Parse(tc.SubjectTemplate)
if err != nil {
logger.error("Could not parse template")
panic(err)
}
bodyTemplate, err := template.New("body").Parse(tc.BodyTemplate)
if err != nil {
logger.error("Could not parse template")
panic(err)
}
return MailTemplate{
SubjectTemplate: *subjectTemplate,
BodyTemplate: *bodyTemplate,
}
}