From 51ffa88fe9bb94c2f58f0f121f426ac2b7ca2af0 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 8 Jul 2022 22:13:16 -0400 Subject: [PATCH] Prompt for longitude and latitude --- cmd/client/location.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/cmd/client/location.go b/cmd/client/location.go index 94d3853..b0c38f3 100644 --- a/cmd/client/location.go +++ b/cmd/client/location.go @@ -1,18 +1,21 @@ package main import ( + "bufio" "encoding/json" + "fmt" "io/ioutil" "log" "net/http" + "os" "strings" - "sync" ) -func getLocation(wg *sync.WaitGroup, cfg *config) { +func getLocation(app *application) { res, err := http.Get("https://ipinfo.io/json") if err != nil { + fmt.Println("Unable to automatically obtain location, please try again.") log.Println(err) return } @@ -23,17 +26,34 @@ func getLocation(wg *sync.WaitGroup, cfg *config) { log.Println(err) } - err = json.Unmarshal(body, &cfg) + err = json.Unmarshal(body, &app.Config) if err != nil { log.Println(err) } - loc := strings.Split(cfg.Location, ",") - cfg.Latitude, cfg.Longitude = loc[0], loc[1] + loc := strings.Split(app.Config.Location, ",") + app.Config.Latitude, app.Config.Longitude = loc[0], loc[1] - wg.Done() + app.WaitGroup.Done() } -func getPreciseLocation(wg *sync.WaitGroup, cfg *config) { +func getPreciseLocation(app *application) { + fmt.Print("\nEnter longitude: ") + + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + log.Println(err) + } + app.Config.Longitude = strings.TrimSuffix(input, "\n") + + fmt.Print("\nEnter latitude: ") + + input, err = reader.ReadString('\n') + if err != nil { + log.Println(err) + } + app.Config.Latitude = strings.TrimSuffix(input, "\n") + //app.WaitGroup.Done() }