feat(docs): add template preview (#1777)

This commit is contained in:
nils måsén 2023-10-02 16:11:04 +02:00 committed by GitHub
parent 9b28fbc24d
commit 9180e9558e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 944 additions and 20 deletions

View file

@ -0,0 +1,36 @@
package preview
import (
"fmt"
"strings"
"text/template"
"github.com/containrrr/watchtower/pkg/notifications/preview/data"
"github.com/containrrr/watchtower/pkg/notifications/templates"
)
func Render(input string, states []data.State, loglevels []data.LogLevel) (string, error) {
data := data.New()
tpl, err := template.New("").Funcs(templates.Funcs).Parse(input)
if err != nil {
return "", fmt.Errorf("failed to parse template: %e", err)
}
for _, state := range states {
data.AddFromState(state)
}
for _, level := range loglevels {
data.AddLogEntry(level)
}
var buf strings.Builder
err = tpl.Execute(&buf, data)
if err != nil {
return "", fmt.Errorf("failed to execute template: %e", err)
}
return buf.String(), nil
}