mirror of
https://github.com/containrrr/watchtower.git
synced 2025-09-21 21:30:48 +02:00
feat(docs): add template preview (#1777)
This commit is contained in:
parent
9b28fbc24d
commit
9180e9558e
15 changed files with 944 additions and 20 deletions
36
pkg/notifications/preview/tplprev.go
Normal file
36
pkg/notifications/preview/tplprev.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue