2022-07-07 20:27:00 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2022-07-08 22:14:35 -04:00
|
|
|
|
|
|
|
"github.com/joho/godotenv"
|
2022-07-07 20:27:00 -04:00
|
|
|
)
|
|
|
|
|
2022-07-08 22:14:35 -04:00
|
|
|
const version string = "0.1.0"
|
|
|
|
|
|
|
|
type weatherMain struct {
|
|
|
|
Temp float32 `json:"temp"`
|
|
|
|
FeelsLike float32 `json:"feels_like"`
|
|
|
|
HighTemp float32 `json:"temp_max"`
|
|
|
|
LowTemp float32 `json:"temp_min"`
|
|
|
|
Pressure uint `json:"pressure"`
|
|
|
|
Humidity uint `json:"humidity"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type weatherWind struct {
|
|
|
|
Speed float32 `json:"speed"`
|
|
|
|
Gust float32 `json:"gust"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type forecast struct {
|
|
|
|
//Overview weatherOverview `json:"weather"`
|
|
|
|
Main weatherMain `json:"main"`
|
|
|
|
Wind weatherWind `json:"wind"`
|
|
|
|
}
|
|
|
|
|
2022-07-07 23:43:16 -04:00
|
|
|
type config struct {
|
|
|
|
Units string `json:"units"`
|
|
|
|
Location string `json:"loc"`
|
|
|
|
Longitude string `json:"lat"`
|
|
|
|
Latitude string `json:"lon"`
|
2022-07-08 22:14:35 -04:00
|
|
|
ApiKey string `json:"appid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type application struct {
|
2022-07-09 15:33:10 -04:00
|
|
|
Forecast forecast
|
|
|
|
Config config
|
|
|
|
Version string
|
2022-07-08 22:14:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func printWeather(app *application) {
|
|
|
|
|
2022-07-09 15:33:10 -04:00
|
|
|
var unitStr string
|
2022-07-08 22:14:35 -04:00
|
|
|
if app.Config.Units == "imperial" {
|
2022-07-09 15:33:10 -04:00
|
|
|
unitStr = "Farhenheit/mph"
|
2022-07-08 22:14:35 -04:00
|
|
|
} else {
|
2022-07-09 15:33:10 -04:00
|
|
|
unitStr = "Celsius/kph"
|
2022-07-08 22:14:35 -04:00
|
|
|
}
|
|
|
|
|
2022-07-09 15:33:10 -04:00
|
|
|
fmt.Printf("\nUnits: %s (%s)\n\n", app.Config.Units, unitStr)
|
2022-07-08 22:14:35 -04:00
|
|
|
fmt.Printf("Current temperature: %.2f\n", app.Forecast.Main.Temp)
|
|
|
|
fmt.Printf("Feels like: %.2f\n", app.Forecast.Main.FeelsLike)
|
|
|
|
fmt.Printf("High: %.2f\n", app.Forecast.Main.HighTemp)
|
|
|
|
fmt.Printf("Low: %.2f\n", app.Forecast.Main.LowTemp)
|
|
|
|
fmt.Printf("Pressure (hPa): %d\n", app.Forecast.Main.Pressure)
|
|
|
|
fmt.Printf("Humidty (%%): %d\n", app.Forecast.Main.Humidity)
|
|
|
|
fmt.Printf("Wind speed: %.2f\n", app.Forecast.Wind.Speed)
|
|
|
|
fmt.Printf("Gust: %.2f\n", app.Forecast.Wind.Gust)
|
2022-07-07 23:43:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-07 20:27:00 -04:00
|
|
|
func main() {
|
|
|
|
|
2022-07-08 22:14:35 -04:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
2022-07-07 23:43:16 -04:00
|
|
|
cfg := config{
|
2022-07-08 22:14:35 -04:00
|
|
|
Units: "imperial",
|
|
|
|
ApiKey: os.Getenv("API_KEY"),
|
2022-07-07 23:43:16 -04:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:14:35 -04:00
|
|
|
fcst := forecast{}
|
|
|
|
|
|
|
|
app := application{
|
2022-07-09 15:33:10 -04:00
|
|
|
Config: cfg,
|
|
|
|
Forecast: fcst,
|
|
|
|
Version: version,
|
2022-07-08 22:14:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var option string
|
|
|
|
|
|
|
|
fmt.Println("\n=====================================================")
|
|
|
|
fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version)
|
|
|
|
fmt.Println("=====================================================")
|
2022-07-07 20:27:00 -04:00
|
|
|
|
|
|
|
for option != "0" {
|
|
|
|
|
|
|
|
fmt.Print("\nMain Menu\n---------\n\n")
|
|
|
|
|
2022-07-07 23:43:16 -04:00
|
|
|
fmt.Println("1. Today's forecast (use current location, default)")
|
2022-07-07 20:27:00 -04:00
|
|
|
fmt.Println("2. Advanced options")
|
|
|
|
fmt.Print("0. Exit\n\n")
|
|
|
|
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
input, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
2022-07-07 23:43:16 -04:00
|
|
|
log.Println(err)
|
2022-07-07 20:27:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
option = strings.TrimSuffix(input, "\n")
|
|
|
|
|
2022-07-07 23:43:16 -04:00
|
|
|
if option == "1" || option == "" {
|
2022-07-09 15:33:10 -04:00
|
|
|
getLocation(&app)
|
|
|
|
getCurrent(&app)
|
2022-07-08 22:14:35 -04:00
|
|
|
printWeather(&app)
|
2022-07-07 20:27:00 -04:00
|
|
|
} else if option == "2" {
|
2022-07-09 15:33:10 -04:00
|
|
|
advancedMenu(&app)
|
2022-07-07 20:27:00 -04:00
|
|
|
} else if option == "0" {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|