diff --git a/main.go b/main.go index eb18f89c..2ac8b77b 100644 --- a/main.go +++ b/main.go @@ -310,7 +310,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := victorops.NewSettingsFromYAML(wtf.Config) widget = victorops.NewWidget(app, settings) case "weather": - widget = weather.NewWidget(app, pages) + settings := weather.NewSettingsFromYAML(wtf.Config) + widget = weather.NewWidget(app, pages, settings) case "zendesk": widget = zendesk.NewWidget(app) default: diff --git a/modules/weatherservices/weather/display.go b/modules/weatherservices/weather/display.go index 12b6119d..929e4b57 100644 --- a/modules/weatherservices/weather/display.go +++ b/modules/weatherservices/weather/display.go @@ -9,7 +9,6 @@ import ( ) func (widget *Widget) display() { - if widget.apiKeyValid() == false { widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set") return @@ -54,19 +53,17 @@ func (widget *Widget) sunInfo(cityData *owm.CurrentWeatherData) string { } func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string { - tempUnit := wtf.Config.UString("wtf.mods.weather.tempUnit", "C") - - str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, tempUnit) + str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, widget.settings.tempUnit) str = str + fmt.Sprintf( "%8s: [%s]%4.1f° %s[white]\n", "Current", - wtf.Config.UString("wtf.mods.weather.colors.current", "green"), + widget.settings.colors.current, cityData.Main.Temp, - tempUnit, + widget.settings.tempUnit, ) - str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, tempUnit) + str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, widget.settings.tempUnit) return str } diff --git a/modules/weatherservices/weather/settings.go b/modules/weatherservices/weather/settings.go new file mode 100644 index 00000000..1f52db16 --- /dev/null +++ b/modules/weatherservices/weather/settings.go @@ -0,0 +1,39 @@ +package weather + +import ( + "os" + + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type colors struct { + current string +} + +type Settings struct { + colors + common *cfg.Common + + apiKey string + cityIDs []interface{} + language string + tempUnit string +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.weather") + + settings := Settings{ + common: cfg.NewCommonSettingsFromYAML(ymlConfig), + + apiKey: localConfig.UString("apiKey", os.Getenv("WTF_OWM_API_KEY")), + cityIDs: localConfig.UList("cityids"), + language: localConfig.UString("language", "EN"), + tempUnit: localConfig.UString("tempUnit", "C"), + } + + settings.colors.current = localConfig.UString("colors.current", "green") + + return &settings +} diff --git a/modules/weatherservices/weather/widget.go b/modules/weatherservices/weather/widget.go index b7dba99e..13bcb5ed 100644 --- a/modules/weatherservices/weather/widget.go +++ b/modules/weatherservices/weather/widget.go @@ -1,8 +1,6 @@ package weather import ( - "os" - owm "github.com/briandowns/openweathermap" "github.com/gdamore/tcell" "github.com/rivo/tview" @@ -25,22 +23,24 @@ type Widget struct { wtf.HelpfulWidget wtf.TextWidget - APIKey string - Data []*owm.CurrentWeatherData - Idx int + // APIKey string + Data []*owm.CurrentWeatherData + Idx int + settings *Settings } // NewWidget creates and returns a new instance of the weather Widget. -func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { - configKey := "weather" +func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { widget := Widget{ HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), - TextWidget: wtf.NewTextWidget(app, "Weather", configKey, true), + TextWidget: wtf.NewTextWidget(app, "Weather", "weather", true), - Idx: 0, + Idx: 0, + settings: settings, } - widget.loadAPICredentials() + // widget.loadAPICredentials() + // widget.APIKey widget.HelpfulWidget.SetView(widget.View) widget.View.SetInputCapture(widget.keyboardIntercept) @@ -57,7 +57,7 @@ func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData { data := []*owm.CurrentWeatherData{} for _, cityID := range cityIDs { - result, err := widget.currentWeather(widget.APIKey, cityID) + result, err := widget.currentWeather(cityID) if err == nil { data = append(data, result) } @@ -70,7 +70,7 @@ func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData { // widget's view for rendering func (widget *Widget) Refresh() { if widget.apiKeyValid() { - widget.Data = widget.Fetch(wtf.ToInts(wtf.Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes()))) + widget.Data = widget.Fetch(wtf.ToInts(widget.settings.cityIDs)) } widget.display() @@ -101,11 +101,11 @@ func (widget *Widget) Prev() { /* -------------------- Unexported Functions -------------------- */ func (widget *Widget) apiKeyValid() bool { - if widget.APIKey == "" { + if widget.settings.apiKey == "" { return false } - if len(widget.APIKey) != 32 { + if len(widget.settings.apiKey) != 32 { return false } @@ -124,11 +124,11 @@ func (widget *Widget) currentData() *owm.CurrentWeatherData { return widget.Data[widget.Idx] } -func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) { +func (widget *Widget) currentWeather(cityCode int) (*owm.CurrentWeatherData, error) { weather, err := owm.NewCurrent( - wtf.Config.UString("wtf.mods.weather.tempUnit", "C"), - wtf.Config.UString("wtf.mods.weather.language", "EN"), - apiKey, + widget.settings.tempUnit, + widget.settings.language, + widget.settings.apiKey, ) if err != nil { return nil, err @@ -142,17 +142,6 @@ func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentW return weather, nil } -func (widget *Widget) defaultCityCodes() []interface{} { - defaultArr := []int{3370352} - - var defaults = make([]interface{}, len(defaultArr)) - for i, d := range defaultArr { - defaults[i] = d - } - - return defaults -} - func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch string(event.Rune()) { case "/": @@ -177,12 +166,3 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { return event } } - -// 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"), - ) -}