From 951942bacd12a74cefb29654cf1912460aa886c2 Mon Sep 17 00:00:00 2001 From: holger-waschke Date: Mon, 5 Feb 2024 17:07:20 +0100 Subject: [PATCH] add support for multiple templates --- pkg/config/config.go | 8 +++++--- pkg/template/template.go | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 7fc63c4..5a2fee4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -112,7 +112,9 @@ func resolveFilepaths(baseDir string, cfg *Config, logger log.Logger) { return absFp } - cfg.Template = join(cfg.Template) + for i, v := range cfg.Template { + cfg.Template[i] = join(v) + } } // AutoResolve is the struct used for defining jira resolution state when alert is resolved. @@ -180,7 +182,7 @@ func (rc *ReceiverConfig) UnmarshalYAML(unmarshal func(interface{}) error) error type Config struct { Defaults *ReceiverConfig `yaml:"defaults,omitempty" json:"defaults,omitempty"` Receivers []*ReceiverConfig `yaml:"receivers,omitempty" json:"receivers,omitempty"` - Template string `yaml:"template" json:"template"` + Template []string `yaml:"template" json:"template"` // Catches all undefined fields and must be empty after parsing. XXX map[string]interface{} `yaml:",inline" json:"-"` @@ -330,7 +332,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { return fmt.Errorf("no receivers defined") } - if c.Template == "" { + if len(c.Template) == 0 { return fmt.Errorf("missing template file") } diff --git a/pkg/template/template.go b/pkg/template/template.go index 648ae7d..e9f42b4 100644 --- a/pkg/template/template.go +++ b/pkg/template/template.go @@ -41,6 +41,7 @@ var funcs = template.FuncMap{ return strings.Join(s, sep) }, "match": regexp.MatchString, + "split": strings.Split, "reReplaceAll": func(pattern, repl, text string) string { re := regexp.MustCompile(pattern) return re.ReplaceAllString(text, repl) @@ -54,9 +55,9 @@ var funcs = template.FuncMap{ } // LoadTemplate reads and parses all templates defined in the given file and constructs a jiralert.Template. -func LoadTemplate(path string, logger log.Logger) (*Template, error) { +func LoadTemplate(path []string, logger log.Logger) (*Template, error) { level.Debug(logger).Log("msg", "loading templates", "path", path) - tmpl, err := template.New("").Option("missingkey=zero").Funcs(funcs).ParseFiles(path) + tmpl, err := template.New("").Option("missingkey=zero").Funcs(funcs).ParseFiles(path...) if err != nil { return nil, err }