mirror of
https://codeberg.org/andyscott/weather-cli.git
synced 2024-12-21 12:43:09 -05:00
Added automatic location fetching
This commit is contained in:
parent
9e8e00e915
commit
6b8e4a3b86
4 changed files with 82 additions and 10 deletions
|
@ -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()
|
||||
}
|
45
cmd/client/location.go
Normal file
45
cmd/client/location.go
Normal file
|
@ -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) {
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
9
cmd/client/weather.go
Normal file
9
cmd/client/weather.go
Normal file
|
@ -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?"
|
||||
|
||||
}
|
Loading…
Reference in a new issue