1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/opsgenie/widget.go
Chris Cummer a62b910893 Closes #43. Add CommandRunner module to the app.
CommandRunner allows you to define a terminal command and arguments, run
it on a schedule, and view the output.

Examples:

   ping -3 cisco.com
2018-05-17 17:15:03 -07:00

78 lines
1.6 KiB
Go

package opsgenie
import (
"fmt"
"strings"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
// Config is a pointer to the global config object
var Config *config.Config
type Widget struct {
wtf.TextWidget
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" ⏰ OpsGenie ", "opsgenie", false),
}
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
data, err := Fetch()
widget.UpdateRefreshedAt()
widget.View.SetTitle(" ⏰ On Call ")
widget.View.Clear()
if err != nil {
widget.View.SetWrap(true)
fmt.Fprintf(widget.View, "%s", err)
} else {
widget.View.SetWrap(false)
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
}
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string {
str := ""
displayEmpty := Config.UBool("wtf.mods.opsgenie.displayEmpty", true)
for _, data := range onCallResponse.OnCallData {
if (len(data.Recipients) == 0) && (displayEmpty == false) {
continue
}
var msg string
if len(data.Recipients) == 0 {
msg = " [gray]no one[white]\n\n"
} else {
msg = fmt.Sprintf(" %s\n\n", strings.Join(wtf.NamesFromEmails(data.Recipients), ", "))
}
str = str + widget.cleanScheduleName(data.Parent.Name)
str = str + msg
}
return str
}
func (widget *Widget) cleanScheduleName(schedule string) string {
cleanedName := strings.Replace(schedule, "_", " ", -1)
return fmt.Sprintf(" [green]%s[white]\n", cleanedName)
}