weather-cli/cmd/client/main.go
2022-07-13 17:05:07 -04:00

131 lines
3.1 KiB
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/joho/godotenv"
)
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"`
}
type config struct {
Units string `json:"units"`
Location string `json:"loc"`
Longitude string `json:"lat"`
Latitude string `json:"lon"`
ApiKey string `json:"appid"`
}
type application struct {
Forecast forecast
Config config
Version string
}
func printWeather(app *application) {
var unitStr string
if app.Config.Units == "imperial" {
unitStr = "Farhenheit/mph"
} else {
unitStr = "Celsius/kph"
}
fmt.Println("\nNote! A value of 0 or 0.00 indicates the data is not available at this time.")
fmt.Printf("Units: %s (%s)\n\n", app.Config.Units, unitStr)
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)
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
}
cfg := config{
Units: "imperial",
ApiKey: os.Getenv("API_KEY"),
}
fcst := forecast{}
app := application{
Config: cfg,
Forecast: fcst,
Version: version,
}
var option string
fmt.Println("\n=====================================================")
fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version)
fmt.Println("=====================================================")
fmt.Println("New in version 0.1.0:")
fmt.Println(" - Default option: Automatically determine location")
fmt.Println(" - Advanced option: Enter exact location for anywhere in the world")
fmt.Println(" - Advanced option: Change back and forth between imperial and metric units of measurement")
for option != "0" {
fmt.Print("\nMain Menu\n---------\n\n")
fmt.Println("1. Today's forecast (use current location, default)")
fmt.Println("2. Advanced options (Change units, precise location, etc.)")
fmt.Print("0. Exit\n\n")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
log.Println(err)
}
option = strings.TrimSuffix(input, "\n")
if option == "1" || option == "" {
getLocation(&app)
getCurrent(&app)
printWeather(&app)
} else if option == "2" {
advancedMenu(&app)
} else if option == "0" {
return
} else {
fmt.Print("\nOops! An error occurred, please choose a valid option.\n\n")
}
}
}