From 6b8e4a3b8675eb6d4d5dbc63679a103417fc166a Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 7 Jul 2022 23:43:16 -0400 Subject: [PATCH] Added automatic location fetching --- cmd/client/{advancedMenu.go => advanced.go} | 13 ++++-- cmd/client/location.go | 45 +++++++++++++++++++++ cmd/client/main.go | 25 +++++++++--- cmd/client/weather.go | 9 +++++ 4 files changed, 82 insertions(+), 10 deletions(-) rename cmd/client/{advancedMenu.go => advanced.go} (64%) create mode 100644 cmd/client/location.go create mode 100644 cmd/client/weather.go diff --git a/cmd/client/advancedMenu.go b/cmd/client/advanced.go similarity index 64% rename from cmd/client/advancedMenu.go rename to cmd/client/advanced.go index 5e00b50..2536b6d 100644 --- a/cmd/client/advancedMenu.go +++ b/cmd/client/advanced.go @@ -9,7 +9,7 @@ import ( "sync" ) -func advancedMenu(wg *sync.WaitGroup) { +func advancedMenu(wg *sync.WaitGroup, cfg *config) { var option string @@ -17,7 +17,7 @@ func advancedMenu(wg *sync.WaitGroup) { fmt.Print("\nAdvanced Menu\n-------------\n\n") - fmt.Println("1. Change units (Fahrenheit/Celsius)") + fmt.Println("1. Change units (Default: Fahrenheit)") fmt.Println("2. Enter precise location") fmt.Print("0. Back\n\n") @@ -30,11 +30,16 @@ func advancedMenu(wg *sync.WaitGroup) { option = strings.TrimSuffix(input, "\n") if option == "1" { - fmt.Print("\nChanged units...\n\n") + current := cfg.Units + if current == "F" { + cfg.Units = "C" + } else { + cfg.Units = "F" + } + fmt.Printf("\nChanged units from %s to %s...\n\n", current, cfg.Units) } else if option == "2" { fmt.Print("Precise location selected...\n\n") } } - wg.Done() } diff --git a/cmd/client/location.go b/cmd/client/location.go new file mode 100644 index 0000000..2fd4b5a --- /dev/null +++ b/cmd/client/location.go @@ -0,0 +1,45 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "strings" + "sync" +) + +func getLocation(wg *sync.WaitGroup, cfg *config) { + + errMsg := "\nSorry, couldn't get your location automatically" + + res, err := http.Get("https://ipinfo.io/json") + if err != nil { + fmt.Println(errMsg) + log.Println(err) + return + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + fmt.Println(errMsg) + log.Panicln(err) + } + + err = json.Unmarshal(body, &cfg) + if err != nil { + fmt.Println(errMsg) + log.Fatalln(err) + } + + loc := strings.Split(cfg.Location, ",") + cfg.Latitude, cfg.Longitude = loc[0], loc[1] + + wg.Done() +} + +func getPreciseLocation(wg *sync.WaitGroup, cfg *config) { + +} diff --git a/cmd/client/main.go b/cmd/client/main.go index dc850d8..8f83f5e 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -9,36 +9,49 @@ import ( "sync" ) +type config struct { + Units string `json:"units"` + Location string `json:"loc"` + Longitude string `json:"lat"` + Latitude string `json:"lon"` +} + func main() { + cfg := config{ + Units: "F", + } + var option string var wg sync.WaitGroup fmt.Println("\n==============================================") fmt.Println("| Welcome to the OpenWeatherMap-gRPC Client! |") - fmt.Print("==============================================\n") + fmt.Println("==============================================") for option != "0" { fmt.Print("\nMain Menu\n---------\n\n") - fmt.Println("1. Today's forecast (use current location)") + fmt.Println("1. Today's forecast (use current location, default)") fmt.Println("2. Advanced options") fmt.Print("0. Exit\n\n") reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') if err != nil { - log.Fatal(err) + log.Println(err) } option = strings.TrimSuffix(input, "\n") - if option == "1" { - fmt.Println("Option 1 selected...") + if option == "1" || option == "" { + wg.Add(1) + go getLocation(&wg, &cfg) + wg.Wait() } else if option == "2" { wg.Add(1) - go advancedMenu(&wg) + go advancedMenu(&wg, &cfg) wg.Wait() } else if option == "0" { return diff --git a/cmd/client/weather.go b/cmd/client/weather.go new file mode 100644 index 0000000..2348349 --- /dev/null +++ b/cmd/client/weather.go @@ -0,0 +1,9 @@ +package main + +import "sync" + +func getToday(wg *sync.WaitGroup, cfg *config) { + + // errMsg := "\nSorry, couldn't get today's weather. Perhaps you've been rate limited?" + +}