From f76c48a95ec0bb61d84080a3546d529d8ea3f839 Mon Sep 17 00:00:00 2001 From: Possible Triangle Date: Tue, 21 Jul 2020 13:27:51 +0200 Subject: [PATCH] Add string functions for lowercase, uppercase and capitalize to shoutrrr templates (#593) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added string functions for lowercase, uppercase and capitalize to shoutrrr templates * Update pkg/notifications/shoutrrr.go Co-authored-by: nils måsén * Update pkg/notifications/shoutrrr.go Co-authored-by: nils måsén * Update pkg/notifications/shoutrrr.go Co-authored-by: nils måsén * Update pkg/notifications/shoutrrr_test.go Co-authored-by: nils måsén * escape quotation marks in test Co-authored-by: nils måsén --- pkg/notifications/shoutrrr.go | 11 +++++++++-- pkg/notifications/shoutrrr_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pkg/notifications/shoutrrr.go b/pkg/notifications/shoutrrr.go index 8cf3d7c..9a7cd62 100644 --- a/pkg/notifications/shoutrrr.go +++ b/pkg/notifications/shoutrrr.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "text/template" + "strings" "github.com/containrrr/shoutrrr" "github.com/containrrr/shoutrrr/pkg/router" @@ -109,10 +110,16 @@ func getShoutrrrTemplate(c *cobra.Command) *template.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 // try to parse the template string. 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 @@ -128,7 +135,7 @@ func getShoutrrrTemplate(c *cobra.Command) *template.Template { // template wasn't configured (the empty template string) // fallback to using the default template. if err != nil || tplString == "" { - tpl = template.Must(template.New("").Parse(shoutrrrDefaultTemplate)) + tpl = template.Must(template.New("").Funcs(funcs).Parse(shoutrrrDefaultTemplate)) } return tpl diff --git a/pkg/notifications/shoutrrr_test.go b/pkg/notifications/shoutrrr_test.go index 9795c2a..5db7473 100644 --- a/pkg/notifications/shoutrrr_test.go +++ b/pkg/notifications/shoutrrr_test.go @@ -51,6 +51,30 @@ func TestShoutrrrTemplate(t *testing.T) { 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) { cmd := new(cobra.Command)