From 936acc0326df8750f5b45ce5594f672427d21621 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sat, 13 Apr 2019 11:36:24 -0700 Subject: [PATCH] WTF-400 BambooHR extracted to new config format --- main.go | 3 ++- modules/bamboohr/settings.go | 27 +++++++++++++++++++++++++++ modules/bamboohr/widget.go | 23 ++++++++++++----------- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 modules/bamboohr/settings.go diff --git a/main.go b/main.go index 9d37b7e1..e78b430a 100644 --- a/main.go +++ b/main.go @@ -182,7 +182,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w // Always in alphabetical order switch widgetName { case "bamboohr": - widget = bamboohr.NewWidget(app) + cfg := bamboohr.NewSettingsFromYAML(wtf.Config) + widget = bamboohr.NewWidget(app, cfg) case "bargraph": widget = bargraph.NewWidget(app) case "bittrex": diff --git a/modules/bamboohr/settings.go b/modules/bamboohr/settings.go new file mode 100644 index 00000000..223b8c32 --- /dev/null +++ b/modules/bamboohr/settings.go @@ -0,0 +1,27 @@ +package bamboohr + +import ( + "os" + + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type Settings struct { + Common *cfg.Common + + APIKey string + Subdomain string +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.bamboohr") + + settings := Settings{ + Common: cfg.NewCommonSettingsFromYAML(ymlConfig), + APIKey: localConfig.UString("apiKey", os.Getenv("WTF_BAMBOO_HR_TOKEN")), + Subdomain: localConfig.UString("subdomain", os.Getenv("WTF_BAMBOO_HR_SUBDOMAIN")), + } + + return &settings +} diff --git a/modules/bamboohr/widget.go b/modules/bamboohr/widget.go index a757e40a..5fd874c1 100644 --- a/modules/bamboohr/widget.go +++ b/modules/bamboohr/widget.go @@ -2,19 +2,25 @@ package bamboohr import ( "fmt" - "os" + // "os" "github.com/rivo/tview" "github.com/wtfutil/wtf/wtf" ) +const APIURI = "https://api.bamboohr.com/api/gateway.php" + type Widget struct { wtf.TextWidget + + settings *Settings } -func NewWidget(app *tview.Application) *Widget { +func NewWidget(app *tview.Application, settings *Settings) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(app, "BambooHR", "bamboohr", false), + + settings: settings, } return &widget @@ -23,17 +29,12 @@ func NewWidget(app *tview.Application) *Widget { /* -------------------- Exported Functions -------------------- */ func (widget *Widget) Refresh() { - apiKey := wtf.Config.UString( - "wtf.mods.bamboohr.apiKey", - os.Getenv("WTF_BAMBOO_HR_TOKEN"), + client := NewClient( + APIURI, + widget.settings.APIKey, + widget.settings.Subdomain, ) - subdomain := wtf.Config.UString( - "wtf.mods.bamboohr.subdomain", - os.Getenv("WTF_BAMBOO_HR_SUBDOMAIN"), - ) - - client := NewClient("https://api.bamboohr.com/api/gateway.php", apiKey, subdomain) todayItems := client.Away( "timeOff", wtf.Now().Format(wtf.DateFormat),