From c7223843f0a9c75c20e87483f75760b138ee6437 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 31 Jul 2018 13:36:02 -0700 Subject: [PATCH] Clean up the Weather module's API credentials loading Now prioritizes API key configuration in the config file over ENV vars. --- weatherservices/weather/widget.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/weatherservices/weather/widget.go b/weatherservices/weather/widget.go index 48062285..fcfb3904 100644 --- a/weatherservices/weather/widget.go +++ b/weatherservices/weather/widget.go @@ -1,13 +1,13 @@ package weather import ( - "fmt" + //"fmt" "os" owm "github.com/briandowns/openweathermap" "github.com/gdamore/tcell" "github.com/rivo/tview" - "github.com/senorprogrammer/wtf/logger" + //"github.com/senorprogrammer/wtf/logger" "github.com/senorprogrammer/wtf/wtf" ) @@ -43,14 +43,10 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { app: app, pages: pages, - APIKey: os.Getenv("WTF_OWM_API_KEY"), - Idx: 0, + Idx: 0, } - if widget.APIKey == "" { - logger.Log("loading weather API key from config") - widget.APIKey = wtf.Config.UString(fmt.Sprintf("wtf.mods.%s.apiKey", configKey), "") - } + widget.loadAPICredentials() widget.View.SetInputCapture(widget.keyboardIntercept) @@ -188,6 +184,15 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { } } +// loadAPICredentials loads the API authentication credentials for this module +// First checks to see if they're in the config file. If not, checks the ENV var +func (widget *Widget) loadAPICredentials() { + widget.APIKey = wtf.Config.UString( + "wtf.mods.weather.apiKey", + os.Getenv("WTF_OWM_API_KEY"), + ) +} + func (widget *Widget) showHelp() { closeFunc := func() { widget.pages.RemovePage("help")