diff --git a/main.go b/main.go index 467d15a3..eb18f89c 100644 --- a/main.go +++ b/main.go @@ -307,7 +307,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := twitter.NewSettingsFromYAML(wtf.Config) widget = twitter.NewWidget(app, pages, settings) case "victorops": - widget = victorops.NewWidget(app) + settings := victorops.NewSettingsFromYAML(wtf.Config) + widget = victorops.NewWidget(app, settings) case "weather": widget = weather.NewWidget(app, pages) case "zendesk": diff --git a/modules/victorops/client.go b/modules/victorops/client.go index 8c952e28..06069313 100644 --- a/modules/victorops/client.go +++ b/modules/victorops/client.go @@ -4,34 +4,20 @@ import ( "encoding/json" "fmt" "net/http" - "os" "strings" "github.com/wtfutil/wtf/logger" - "github.com/wtfutil/wtf/wtf" ) // Fetch gets the current oncall users -func Fetch() ([]OnCallTeam, error) { +func Fetch(apiID, apiKey string) ([]OnCallTeam, error) { scheduleURL := "https://api.victorops.com/api-public/v1/oncall/current" - response, err := victorOpsRequest(scheduleURL, apiID(), apiKey()) + response, err := victorOpsRequest(scheduleURL, apiID, apiKey) + return response, err } /* ---------------- Unexported Functions ---------------- */ -func apiID() string { - return wtf.Config.UString( - "wtf.mods.victorops.apiID", - os.Getenv("WTF_VICTOROPS_API_ID"), - ) -} - -func apiKey() string { - return wtf.Config.UString( - "wtf.mods.victorops.apiKey", - os.Getenv("WTF_VICTOROPS_API_KEY"), - ) -} func victorOpsRequest(url string, apiID string, apiKey string) ([]OnCallTeam, error) { req, err := http.NewRequest("GET", url, nil) diff --git a/modules/victorops/settings.go b/modules/victorops/settings.go new file mode 100644 index 00000000..ba4c106c --- /dev/null +++ b/modules/victorops/settings.go @@ -0,0 +1,30 @@ +package victorops + +import ( + "os" + + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type Settings struct { + common *cfg.Common + + apiID string + apiKey string + team string +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.victorops") + + settings := Settings{ + common: cfg.NewCommonSettingsFromYAML(ymlConfig), + + apiID: localConfig.UString("apiID", os.Getenv("WTF_VICTOROPS_API_ID")), + apiKey: localConfig.UString("apiKey", os.Getenv("WTF_VICTOROPS_API_KEY")), + team: localConfig.UString("team"), + } + + return &settings +} diff --git a/modules/victorops/widget.go b/modules/victorops/widget.go index a4de256f..24a49a52 100644 --- a/modules/victorops/widget.go +++ b/modules/victorops/widget.go @@ -19,11 +19,13 @@ const HelpText = ` // Widget contains text info type Widget struct { wtf.TextWidget - teams []OnCallTeam + + teams []OnCallTeam + settings *Settings } // NewWidget creates a new widget -func NewWidget(app *tview.Application) *Widget { +func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(app, "VictorOps - OnCall", "victorops", true), } @@ -40,7 +42,7 @@ func (widget *Widget) Refresh() { return } - teams, err := Fetch() + teams, err := Fetch(widget.settings.apiID, widget.settings.apiKey) widget.View.SetTitle(widget.ContextualTitle(widget.Name())) if err != nil { @@ -64,10 +66,10 @@ func (widget *Widget) display() { } func (widget *Widget) contentFrom(teams []OnCallTeam) string { - teamToDisplay := wtf.Config.UString("wtf.mods.victorops.team") var str string + for _, team := range teams { - if len(teamToDisplay) > 0 && teamToDisplay != team.Slug { + if len(widget.settings.team) > 0 && widget.settings.team != team.Slug { continue }