Skip to content

Commit

Permalink
now using the sensu-plugins-sdk templating function
Browse files Browse the repository at this point in the history
  • Loading branch information
Caleb Hailey committed Mar 3, 2020
1 parent fbe3537 commit bb8d722
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"bytes"
"fmt"
"os"
"strings"
"text/template"

"github.com/bluele/slack"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)

Expand Down Expand Up @@ -148,19 +147,6 @@ func formattedMessage(event *corev2.Event) string {
return fmt.Sprintf("%s - %s", formattedEventAction(event), eventSummary(event, 100))
}

func templatedDescription(event *corev2.Event, descriptionTemplate string) string {
output := new(bytes.Buffer)
t, err := template.New("slackMessageDescription").Parse(descriptionTemplate)
if err != nil {
fmt.Errorf("Error parsing description template: %s", err)
}
err = t.Execute(output, event)
if err != nil {
fmt.Errorf("Error processing description template: %s", err)
}
return output.String()
}

func messageColor(event *corev2.Event) string {
switch event.Check.Status {
case 0:
Expand All @@ -184,9 +170,13 @@ func messageStatus(event *corev2.Event) string {
}

func messageAttachment(event *corev2.Event) *slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
fmt.Errorf("Error processing template: %s", err)
}
attachment := &slack.Attachment{
Title: "Description",
Text: templatedDescription(event, config.slackDescriptionTemplate),
Text: description,
Fallback: formattedMessage(event),
Color: messageColor(event),
Fields: []*slack.AttachmentField{
Expand Down

0 comments on commit bb8d722

Please sign in to comment.