wid-notifier/template.go

74 lines
1.7 KiB
Go
Raw Normal View History

// Copyright (c) 2023 Julian Müller (ChaoticByte)
2023-10-11 22:14:01 +02:00
package main
import (
"bytes"
"text/template"
)
2023-10-19 20:50:32 +02:00
const DEFAULT_SUBJECT_TEMPLATE = "[{{ .Classification }}] {{ .Title }}"
const DEFAULT_BODY_TEMPLATE = `{{ if .Status }}[{{ .Status }}] {{ end }}{{ .Name }}
-> {{ .PortalUrl }}
{{- if eq .NoPatch "true" }}
2023-10-11 22:14:01 +02:00
2023-10-19 20:50:32 +02:00
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 }}`
2023-10-11 22:14:01 +02:00
type MailTemplateConfig struct {
SubjectTemplate string `json:"subject"`
BodyTemplate string `json:"body"`
}
type MailTemplate struct {
SubjectTemplate template.Template
BodyTemplate template.Template
}
func (t MailTemplate) generate(notice WidNotice) (MailContent, error) {
c := MailContent{}
buffer := &bytes.Buffer{}
err := t.SubjectTemplate.Execute(buffer, notice)
if err != nil {
return c, err
}
c.Subject = buffer.String()
buffer.Truncate(0) // we can recycle our buffer
err = t.BodyTemplate.Execute(buffer, notice)
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")
2023-10-11 22:14:01 +02:00
panic(err)
}
bodyTemplate, err := template.New("body").Parse(tc.BodyTemplate)
if err != nil {
logger.error("Could not parse template")
2023-10-11 22:14:01 +02:00
panic(err)
}
return MailTemplate{
SubjectTemplate: *subjectTemplate,
BodyTemplate: *bodyTemplate,
}
}