Skip to content

Commit

Permalink
add support for multiple templates
Browse files Browse the repository at this point in the history
  • Loading branch information
holger-waschke committed Feb 5, 2024
1 parent d43cbab commit 951942b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:"-"`
Expand Down Expand Up @@ -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")
}

Expand Down
5 changes: 3 additions & 2 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down

0 comments on commit 951942b

Please sign in to comment.