Skip to content

Commit

Permalink
Merge pull request #3 from nixwiz/use_sdk_templates
Browse files Browse the repository at this point in the history
Switch to SDK template
  • Loading branch information
Todd Campbell authored May 1, 2020
2 parents ed06c69 + a9742fe commit fd32549
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Versioning](http://semver.org/spec/v2.0.0.html).

### Change
- Formatting cleanup and goreportcard badge
- Use Sensu SDK templating engine

## [0.3.0] - 2020-05-01

Expand Down
33 changes: 2 additions & 31 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"text/template"

"github.com/opsgenie/opsgenie-go-sdk/alertsv2"
alerts "github.com/opsgenie/opsgenie-go-sdk/alertsv2"
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
"github.com/sensu/sensu-go/types"
)

Expand All @@ -32,11 +30,6 @@ type Config struct {
MessageLimit int
}

// used to handle getting text/template or html/template
type templater interface {
Execute(wr io.Writer, data interface{}) error
}

var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Expand Down Expand Up @@ -134,7 +127,7 @@ func checkArgs(_ *types.Event) error {
// []string contains Entity.Name Check.Name Entity.Namespace, event.Entity.EntityClass to use as tags in Opsgenie
func parseEventKeyTags(event *types.Event) (title string, alias string, tags []string) {
alias = fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name)
title, err := resolveTemplate(plugin.MessageTemplate, event)
title, err := templates.EvalTemplate("title", plugin.MessageTemplate, event)
if err != nil {
return "", "", []string{}
}
Expand Down Expand Up @@ -329,28 +322,6 @@ func getNote(event *types.Event) (string, error) {
return fmt.Sprintf("Event data update:\n\n%s", eventJSON), nil
}

// resolveTemplate func resolves events in go templates
func resolveTemplate(templateValue string, event *types.Event) (string, error) {
var (
resolved bytes.Buffer
tmpl templater
err error
)

tmpl, err = template.New("test").Parse(templateValue)

if err != nil {
return "", err
}

err = tmpl.Execute(&resolved, *event)
if err != nil {
return "", err
}

return resolved.String(), nil
}

// time func returns only the first n bytes of a string
func trim(s string, n int) string {
if len(s) > n {
Expand Down

0 comments on commit fd32549

Please sign in to comment.