From 0cea4f3e5c9d0fbde818b4d7050e79855d2491f0 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 21:48:28 -0400 Subject: [PATCH 01/12] Update submodule --- SimplrWeather | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SimplrWeather b/SimplrWeather index 8c4b035..da7e324 160000 --- a/SimplrWeather +++ b/SimplrWeather @@ -1 +1 @@ -Subproject commit 8c4b03510c4a3b89e1adb28d5ee36ad091240f0a +Subproject commit da7e32450045132e40ab6c09ff2dbc199f450f9d From 5dd913e1ec3c861cfcbe0f01dd4d631676ff66aa Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 22:15:35 -0400 Subject: [PATCH 02/12] Move main menu to separate file --- cmd/menu.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 cmd/menu.go diff --git a/cmd/menu.go b/cmd/menu.go new file mode 100644 index 0000000..37de86c --- /dev/null +++ b/cmd/menu.go @@ -0,0 +1,55 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" +) + +func mainMenu(app *application) { + + // Display main menu + fmt.Println("\n=====================================================") + fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version) + fmt.Println("=====================================================") + + fmt.Printf("New in version %s:\n", app.Version) + fmt.Println(" - Advanced option: Get historical data back to 1972") + fmt.Println("New in version 0.1.0") + fmt.Println(" - Default option: Automatically determine location") + fmt.Println(" - Advanced option: Enter exact location for anywhere in the world") + fmt.Println(" - Advanced option: Change back and forth between imperial and metric units of measurement") + + var option string + // Menu loop + for option != "0" { + fmt.Print("\nMain Menu\n---------\n\n") + fmt.Println("1. Today's forecast (use current location, default)") + fmt.Println("2. Advanced options (Change units, precise location, etc.)") + fmt.Print("0. Exit\n\n") + + // Read user input + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + log.Println(err) + } + option = strings.TrimSuffix(input, "\n") + + // Check user input + if option == "1" || option == "" { + getLocation(app) + getCurrent(app) + printWeather(app) + } else if option == "2" { + advancedMenu(app) + } else if option == "0" { + return + } else { + fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n") + } + } + +} From 84990100c6552f2ce19d669a48ddc01a699cf2fd Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 22:16:38 -0400 Subject: [PATCH 03/12] Added grpc connection to SimplrWeather --- cmd/main.go | 58 ++++++++++++----------------------------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 4b2319f..58b8e76 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,13 +1,13 @@ package main import ( - "bufio" - "fmt" "log" "os" - "strings" + pb "codeberg.org/andcscott/weather-cli/proto" "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) const version string = "0.2.0" @@ -44,12 +44,19 @@ type application struct { Forecast forecast Config config Version string + Client pb.RouteGuideClient } func main() { + conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + log.Fatalln(err) + } + defer conn.Close() + // Load .env - err := godotenv.Load() + err = godotenv.Load() if err != nil { log.Fatalln(err) } @@ -66,48 +73,9 @@ func main() { Config: cfg, Forecast: fcst, Version: version, + Client: pb.NewRouteGuideClient(conn), } - // Display main menu - fmt.Println("\n=====================================================") - fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version) - fmt.Println("=====================================================") - - fmt.Printf("New in version %s:\n", app.Version) - fmt.Println(" - Advanced option: Get historical data back to 1972") - fmt.Println("New in version 0.1.0") - fmt.Println(" - Default option: Automatically determine location") - fmt.Println(" - Advanced option: Enter exact location for anywhere in the world") - fmt.Println(" - Advanced option: Change back and forth between imperial and metric units of measurement") - - var option string - // Menu loop - for option != "0" { - fmt.Print("\nMain Menu\n---------\n\n") - fmt.Println("1. Today's forecast (use current location, default)") - fmt.Println("2. Advanced options (Change units, precise location, etc.)") - fmt.Print("0. Exit\n\n") - - // Read user input - reader := bufio.NewReader(os.Stdin) - input, err := reader.ReadString('\n') - if err != nil { - log.Println(err) - } - option = strings.TrimSuffix(input, "\n") - - // Check user input - if option == "1" || option == "" { - getLocation(&app) - getCurrent(&app) - printWeather(&app) - } else if option == "2" { - advancedMenu(&app) - } else if option == "0" { - return - } else { - fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n") - } - } + mainMenu(&app) } From 583e89e76c58ea27b9f0ae731d3bcf80cdc3770c Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 22:16:54 -0400 Subject: [PATCH 04/12] Updated module --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d262ff1..d8f7ed5 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module codeberg.org/andcscott/OpenWeatherMap-gRPC-API +module codeberg.org/andcscott/weather-cli go 1.18 From cdf1cb097167d296e02c0fa76f4815faf28c5e9d Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:25:50 -0400 Subject: [PATCH 05/12] Added history.go --- cmd/history.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 cmd/history.go diff --git a/cmd/history.go b/cmd/history.go new file mode 100644 index 0000000..2149554 --- /dev/null +++ b/cmd/history.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + pb "codeberg.org/andcscott/weather-cli/proto" +) + +func getDate(app *Application) (int32, int32, int32) { + + var year, month, day int32 + + fmt.Print("Enter 4-digit year: ") + + _, err := fmt.Scanf("%d", &year) + if err != nil { + fmt.Println("Invalid year") + } + + fmt.Print("Enter month (1-12): ") + _, err = fmt.Scanf("%d", &month) + if err != nil { + fmt.Println("Invalid year") + } + + fmt.Print("Enter day (1-31): ") + _, err = fmt.Scanf("%d", &day) + if err != nil { + fmt.Println("Invalid year") + } + return year, month, day +} + +func getUnixTime(c pb.RouteGuideClient) int32 { + + var year, month, day int32 + + res, err := c.GetUnixTime(context.Background(), &pb.Date{ + Year: year, + Month: month, + Day: day, + }) + if err != nil { + fmt.Printf("Error getting Unix time: %v\n", err) + } + return res.Unixtime +} + +func getHistorical(c pb.RouteGuideClient) { + + var lat, lon, year, month, day int32 + + res, err := c.GetHistoricalData(context.Background(), &pb.LocationDate{ + Latitude: lat, + Longitude: lon, + Year: year, + Month: month, + Day: day, + }) + if err != nil { + fmt.Printf("Error getting historical data: %v", err) + } + + err = json.Unmarshal([]byte(res.Data), &app.Forecast) + if err != nil { + fmt.Printf("Error reading data from server: %v", err) + } +} From e74d4fe74fc3f016defa30353b80c5e212fbd67b Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:26:09 -0400 Subject: [PATCH 06/12] Added historical data option --- cmd/advanced.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cmd/advanced.go b/cmd/advanced.go index abc4b35..5840590 100644 --- a/cmd/advanced.go +++ b/cmd/advanced.go @@ -8,7 +8,7 @@ import ( "strings" ) -func advancedMenu(app *application) { +func advancedMenu(app *Application) { var option string @@ -19,6 +19,7 @@ func advancedMenu(app *application) { fmt.Printf("1. Change units (Default: imperial; Current: %s)\n", app.Config.Units) fmt.Println("2. Enter precise location") + fmt.Println("3. Get historical data") fmt.Print("0. Back\n\n") // Read user input @@ -27,7 +28,6 @@ func advancedMenu(app *application) { if err != nil { log.Println(err) } - option = strings.TrimSuffix(input, "\n") // Check user input @@ -54,7 +54,22 @@ func advancedMenu(app *application) { fmt.Println("Are you sure the coordinates are valid?") } } + printWeather(app) + app.Forecast.Main.Temp = 0.00 + } else if option == "3" { + var validLoc bool + for !validLoc { + getPreciseLocation(app) + getHistorical(app.Client) + } + + if app.Forecast.Main.Temp != 0.00 { + validLoc = true + } else { + fmt.Println("I couldn't get any information for that location.") + fmt.Println("Are you sure the coordinates are valid?") + } printWeather(app) app.Forecast.Main.Temp = 0.00 } From 0e88ed0445ca7198cb8a6dd81787ddafc012977e Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:26:28 -0400 Subject: [PATCH 07/12] Structs updated --- cmd/location.go | 4 ++-- cmd/main.go | 24 ++++++++++++------------ cmd/menu.go | 2 +- cmd/render.go | 2 +- cmd/weather.go | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cmd/location.go b/cmd/location.go index bee23a0..e889439 100644 --- a/cmd/location.go +++ b/cmd/location.go @@ -12,7 +12,7 @@ import ( ) // Use WAN address to obtain location data -func getLocation(app *application) { +func getLocation(app *Application) { res, err := http.Get("https://ipinfo.io/json") if err != nil { @@ -37,7 +37,7 @@ func getLocation(app *application) { } // Prompt user for location -func getPreciseLocation(app *application) { +func getPreciseLocation(app *Application) { fmt.Print("\nEnter latitude: ") diff --git a/cmd/main.go b/cmd/main.go index 58b8e76..35e5df3 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,7 +12,7 @@ import ( const version string = "0.2.0" -type weatherMain struct { +type Weather struct { Temp float32 `json:"temp"` FeelsLike float32 `json:"feels_like"` HighTemp float32 `json:"temp_max"` @@ -21,18 +21,18 @@ type weatherMain struct { Humidity uint `json:"humidity"` } -type weatherWind struct { +type Wind struct { Speed float32 `json:"speed"` Gust float32 `json:"gust"` } -type forecast struct { +type Forecast struct { //Overview weatherOverview `json:"weather"` - Main weatherMain `json:"main"` - Wind weatherWind `json:"wind"` + Main Weather `json:"main"` + Wind Wind `json:"wind"` } -type config struct { +type Config struct { Units string `json:"units"` Location string `json:"loc"` Longitude string `json:"lat"` @@ -40,9 +40,9 @@ type config struct { ApiKey string `json:"appid"` } -type application struct { - Forecast forecast - Config config +type Application struct { + Forecast Forecast + Config Config Version string Client pb.RouteGuideClient } @@ -62,14 +62,14 @@ func main() { } // Read API_KEY from .env and create app - cfg := config{ + cfg := Config{ Units: "imperial", ApiKey: os.Getenv("API_KEY"), } - fcst := forecast{} + fcst := Forecast{} - app := application{ + app := Application{ Config: cfg, Forecast: fcst, Version: version, diff --git a/cmd/menu.go b/cmd/menu.go index 37de86c..5ef0455 100644 --- a/cmd/menu.go +++ b/cmd/menu.go @@ -8,7 +8,7 @@ import ( "strings" ) -func mainMenu(app *application) { +func mainMenu(app *Application) { // Display main menu fmt.Println("\n=====================================================") diff --git a/cmd/render.go b/cmd/render.go index a17e85f..be36e0e 100644 --- a/cmd/render.go +++ b/cmd/render.go @@ -3,7 +3,7 @@ package main import "fmt" // Prints saved weather data to the terminal -func printWeather(app *application) { +func printWeather(app *Application) { var unitStr string if app.Config.Units == "imperial" { diff --git a/cmd/weather.go b/cmd/weather.go index 8e9dab4..71491c7 100644 --- a/cmd/weather.go +++ b/cmd/weather.go @@ -8,7 +8,7 @@ import ( ) // Get and store the current forecast -func getCurrent(app *application) { +func getCurrent(app *Application) { lat := "lat=" + app.Config.Latitude lon := "&lon=" + app.Config.Longitude From 29414b28c90b72b74bfdbf35c770ba7f4b62cdc3 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:28:57 -0400 Subject: [PATCH 08/12] Updated parameters for getHistoricalData --- cmd/history.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/history.go b/cmd/history.go index 2149554..25e4cb0 100644 --- a/cmd/history.go +++ b/cmd/history.go @@ -48,7 +48,7 @@ func getUnixTime(c pb.RouteGuideClient) int32 { return res.Unixtime } -func getHistorical(c pb.RouteGuideClient) { +func getHistoricalData(c pb.RouteGuideClient, app *Application) { var lat, lon, year, month, day int32 From 76a4785a0f93d5e7eb761f7b24f9b7816b811c57 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:40:40 -0400 Subject: [PATCH 09/12] Added Date to Config struct --- cmd/advanced.go | 3 ++- cmd/history.go | 15 +++++++-------- cmd/main.go | 7 +++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/cmd/advanced.go b/cmd/advanced.go index 5840590..fb3602c 100644 --- a/cmd/advanced.go +++ b/cmd/advanced.go @@ -61,7 +61,8 @@ func advancedMenu(app *Application) { var validLoc bool for !validLoc { getPreciseLocation(app) - getHistorical(app.Client) + getDate(app) + getHistoricalData(app.Client, app) } if app.Forecast.Main.Temp != 0.00 { diff --git a/cmd/history.go b/cmd/history.go index 25e4cb0..ea94fef 100644 --- a/cmd/history.go +++ b/cmd/history.go @@ -8,31 +8,29 @@ import ( pb "codeberg.org/andcscott/weather-cli/proto" ) -func getDate(app *Application) (int32, int32, int32) { - - var year, month, day int32 +// Get a date from the user and save it to the config +func getDate(app *Application) { fmt.Print("Enter 4-digit year: ") - - _, err := fmt.Scanf("%d", &year) + _, err := fmt.Scanf("%d", &app.Config.Date.Year) if err != nil { fmt.Println("Invalid year") } fmt.Print("Enter month (1-12): ") - _, err = fmt.Scanf("%d", &month) + _, err = fmt.Scanf("%d", &app.Config.Date.Month) if err != nil { fmt.Println("Invalid year") } fmt.Print("Enter day (1-31): ") - _, err = fmt.Scanf("%d", &day) + _, err = fmt.Scanf("%d", &app.Config.Date.Day) if err != nil { fmt.Println("Invalid year") } - return year, month, day } +// Query SimplrWeather for date in Unix time func getUnixTime(c pb.RouteGuideClient) int32 { var year, month, day int32 @@ -48,6 +46,7 @@ func getUnixTime(c pb.RouteGuideClient) int32 { return res.Unixtime } +// Query SimplrWeather for historical data func getHistoricalData(c pb.RouteGuideClient, app *Application) { var lat, lon, year, month, day int32 diff --git a/cmd/main.go b/cmd/main.go index 35e5df3..e2cdd90 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -32,11 +32,18 @@ type Forecast struct { Wind Wind `json:"wind"` } +type Date struct { + Year int32 + Month int32 + Day int32 +} + type Config struct { Units string `json:"units"` Location string `json:"loc"` Longitude string `json:"lat"` Latitude string `json:"lon"` + Date Date ApiKey string `json:"appid"` } From 1cc715522e61f3c8264041917a3bbeb3e64be53b Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 2 Aug 2022 23:56:24 -0400 Subject: [PATCH 10/12] Use user input --- cmd/history.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cmd/history.go b/cmd/history.go index ea94fef..937e7b5 100644 --- a/cmd/history.go +++ b/cmd/history.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "strconv" pb "codeberg.org/andcscott/weather-cli/proto" ) @@ -49,14 +50,16 @@ func getUnixTime(c pb.RouteGuideClient) int32 { // Query SimplrWeather for historical data func getHistoricalData(c pb.RouteGuideClient, app *Application) { - var lat, lon, year, month, day int32 + var lat, lon int + lat, _ = strconv.Atoi(app.Config.Latitude) + lon, _ = strconv.Atoi(app.Config.Longitude) res, err := c.GetHistoricalData(context.Background(), &pb.LocationDate{ - Latitude: lat, - Longitude: lon, - Year: year, - Month: month, - Day: day, + Latitude: int32(lat), + Longitude: int32(lon), + Year: app.Config.Date.Year, + Month: app.Config.Date.Month, + Day: app.Config.Date.Day, }) if err != nil { fmt.Printf("Error getting historical data: %v", err) From 7ce1f6f86196013a74f09e9220681cb4995b1507 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 3 Aug 2022 00:00:11 -0400 Subject: [PATCH 11/12] Indendation/brackets --- cmd/advanced.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/advanced.go b/cmd/advanced.go index fb3602c..6fcbc03 100644 --- a/cmd/advanced.go +++ b/cmd/advanced.go @@ -63,13 +63,13 @@ func advancedMenu(app *Application) { getPreciseLocation(app) getDate(app) getHistoricalData(app.Client, app) - } - if app.Forecast.Main.Temp != 0.00 { - validLoc = true - } else { - fmt.Println("I couldn't get any information for that location.") - fmt.Println("Are you sure the coordinates are valid?") + if app.Forecast.Main.Temp != 0.00 { + validLoc = true + } else { + fmt.Println("I couldn't get any information for that location.") + fmt.Println("Are you sure the coordinates are valid?") + } } printWeather(app) app.Forecast.Main.Temp = 0.00 From 10dc33a45c39dfb2c8927f8db07dcd4019fa56a1 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 3 Aug 2022 00:22:02 -0400 Subject: [PATCH 12/12] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ba2acb9..cfc5d29 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,6 @@ go.work # Partner project SimplrWeather/ + +# Protobuf definitions +proto/ \ No newline at end of file