weather-cli/cmd/history.go

69 lines
1.4 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
pb "codeberg.org/andcscott/weather-cli/proto"
)
// 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", &app.Config.Date.Year)
if err != nil {
fmt.Println("Invalid year")
}
fmt.Print("Enter month (1-12): ")
_, 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", &app.Config.Date.Day)
if err != nil {
fmt.Println("Invalid year")
}
}
// Query SimplrWeather for date in Unix time
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
}
// Query SimplrWeather for historical data
func getHistoricalData(c pb.RouteGuideClient, app *Application) {
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)
}
}