1
0
mirror of https://github.com/containrrr/watchtower.git synced 2024-12-12 09:04:17 +02:00

Add string functions for lowercase, uppercase and capitalize to shoutrrr templates (#593)

* Added string functions for lowercase, uppercase and capitalize to shoutrrr templates

* Update pkg/notifications/shoutrrr.go

Co-authored-by: nils måsén <nils@piksel.se>

* Update pkg/notifications/shoutrrr.go

Co-authored-by: nils måsén <nils@piksel.se>

* Update pkg/notifications/shoutrrr.go

Co-authored-by: nils måsén <nils@piksel.se>

* Update pkg/notifications/shoutrrr_test.go

Co-authored-by: nils måsén <nils@piksel.se>

* escape quotation marks in test

Co-authored-by: nils måsén <nils@piksel.se>
This commit is contained in:
Possible Triangle 2020-07-21 13:27:51 +02:00 committed by GitHub
parent d0f3ea3683
commit f76c48a95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"text/template" "text/template"
"strings"
"github.com/containrrr/shoutrrr" "github.com/containrrr/shoutrrr"
"github.com/containrrr/shoutrrr/pkg/router" "github.com/containrrr/shoutrrr/pkg/router"
@ -109,10 +110,16 @@ func getShoutrrrTemplate(c *cobra.Command) *template.Template {
tplString, err := flags.GetString("notification-template") tplString, err := flags.GetString("notification-template")
funcs := template.FuncMap{
"ToUpper": strings.ToUpper,
"ToLower": strings.ToLower,
"Title": strings.Title,
}
// If we succeed in getting a non-empty template configuration // If we succeed in getting a non-empty template configuration
// try to parse the template string. // try to parse the template string.
if tplString != "" && err == nil { if tplString != "" && err == nil {
tpl, err = template.New("").Parse(tplString) tpl, err = template.New("").Funcs(funcs).Parse(tplString)
} }
// In case of errors (either from parsing the template string // In case of errors (either from parsing the template string
@ -128,7 +135,7 @@ func getShoutrrrTemplate(c *cobra.Command) *template.Template {
// template wasn't configured (the empty template string) // template wasn't configured (the empty template string)
// fallback to using the default template. // fallback to using the default template.
if err != nil || tplString == "" { if err != nil || tplString == "" {
tpl = template.Must(template.New("").Parse(shoutrrrDefaultTemplate)) tpl = template.Must(template.New("").Funcs(funcs).Parse(shoutrrrDefaultTemplate))
} }
return tpl return tpl

View File

@ -51,6 +51,30 @@ func TestShoutrrrTemplate(t *testing.T) {
require.Equal(t, "info: foo bar\n", s) require.Equal(t, "info: foo bar\n", s)
} }
func TestShoutrrrStringFunctions(t *testing.T) {
cmd := new(cobra.Command)
flags.RegisterNotificationFlags(cmd)
err := cmd.ParseFlags([]string{"--notification-template={{range .}}{{.Level | printf \"%v\" | ToUpper }}: {{.Message | ToLower }} {{.Message | Title }}{{println}}{{end}}"})
require.NoError(t, err)
shoutrrr := &shoutrrrTypeNotifier{
template: getShoutrrrTemplate(cmd),
}
entries := []*log.Entry{
{
Level: log.InfoLevel,
Message: "foo Bar",
},
}
s := shoutrrr.buildMessage(entries)
require.Equal(t, "INFO: foo bar Foo Bar\n", s)
}
func TestShoutrrrInvalidTemplateUsesTemplate(t *testing.T) { func TestShoutrrrInvalidTemplateUsesTemplate(t *testing.T) {
cmd := new(cobra.Command) cmd := new(cobra.Command)