From c1a53b7e4b46a6a14480895751c3726c7b43d252 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 8 Jul 2022 22:14:35 -0400 Subject: [PATCH] Various improvments to structs, added godotenv to handle variable info --- cmd/client/main.go | 94 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 11 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 8f83f5e..38212e8 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -7,27 +7,95 @@ import ( "os" "strings" "sync" + + "github.com/joho/godotenv" ) +const version string = "0.1.0" + +type weatherMain struct { + Temp float32 `json:"temp"` + FeelsLike float32 `json:"feels_like"` + HighTemp float32 `json:"temp_max"` + LowTemp float32 `json:"temp_min"` + Pressure uint `json:"pressure"` + Humidity uint `json:"humidity"` +} + +type weatherWind struct { + Speed float32 `json:"speed"` + Gust float32 `json:"gust"` +} + +type forecast struct { + //Overview weatherOverview `json:"weather"` + Main weatherMain `json:"main"` + Wind weatherWind `json:"wind"` +} + type config struct { Units string `json:"units"` Location string `json:"loc"` Longitude string `json:"lat"` Latitude string `json:"lon"` + ApiKey string `json:"appid"` +} + +type application struct { + Forecast forecast + Config config + WaitGroup sync.WaitGroup + Version string +} + +func printWeather(app *application) { + + var unitString string + if app.Config.Units == "imperial" { + unitString = "Farhenheit/mph" + } else { + unitString = "Celsius/kph" + } + + fmt.Printf("\nUnits: %s (%s)\n\n", app.Config.Units, unitString) + fmt.Printf("Current temperature: %.2f\n", app.Forecast.Main.Temp) + fmt.Printf("Feels like: %.2f\n", app.Forecast.Main.FeelsLike) + fmt.Printf("High: %.2f\n", app.Forecast.Main.HighTemp) + fmt.Printf("Low: %.2f\n", app.Forecast.Main.LowTemp) + fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Main.Pressure) + fmt.Printf("Humidty (%%): %d\n", app.Forecast.Main.Humidity) + fmt.Printf("Wind speed: %.2f\n", app.Forecast.Wind.Speed) + fmt.Printf("Gust: %.2f\n", app.Forecast.Wind.Gust) } func main() { + err := godotenv.Load() + if err != nil { + log.Fatalln(err) + } + cfg := config{ - Units: "F", + Units: "imperial", + ApiKey: os.Getenv("API_KEY"), + } + + fcst := forecast{} + + var wg sync.WaitGroup + + app := application{ + Config: cfg, + Forecast: fcst, + WaitGroup: wg, + Version: version, } var option string - var wg sync.WaitGroup - fmt.Println("\n==============================================") - fmt.Println("| Welcome to the OpenWeatherMap-gRPC Client! |") - fmt.Println("==============================================") + fmt.Println("\n=====================================================") + fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version) + fmt.Println("=====================================================") for option != "0" { @@ -46,13 +114,17 @@ func main() { option = strings.TrimSuffix(input, "\n") if option == "1" || option == "" { - wg.Add(1) - go getLocation(&wg, &cfg) - wg.Wait() + app.WaitGroup.Add(1) + go getLocation(&app) + app.WaitGroup.Wait() + app.WaitGroup.Add(1) + go getCurrent(&app) + app.WaitGroup.Wait() + printWeather(&app) } else if option == "2" { - wg.Add(1) - go advancedMenu(&wg, &cfg) - wg.Wait() + app.WaitGroup.Add(1) + go advancedMenu(&app) + app.WaitGroup.Wait() } else if option == "0" { return } else {