mirror of
https://codeberg.org/andyscott/weather-cli.git
synced 2024-12-21 12:43:09 -05:00
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// Use WAN address to obtain location data
|
|
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
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(res.Body)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
err = json.Unmarshal(body, &app.Config)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
loc := strings.Split(app.Config.Location, ",")
|
|
app.Config.Latitude, app.Config.Longitude = loc[0], loc[1]
|
|
}
|
|
|
|
// Prompt user for location
|
|
func getPreciseLocation(app *application) {
|
|
|
|
fmt.Print("\nEnter latitude: ")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
input, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
app.Config.Latitude = strings.TrimSuffix(input, "\n")
|
|
|
|
fmt.Print("\nEnter longitude: ")
|
|
|
|
input, err = reader.ReadString('\n')
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
app.Config.Longitude = strings.TrimSuffix(input, "\n")
|
|
}
|