weather-cli/cmd/main.go

112 lines
2.4 KiB
Go
Raw Normal View History

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"`
}
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"`
ApiKey string `json:"appid"`
}
type application struct {
2022-07-09 15:33:10 -04:00
Forecast forecast
Config config
Version string
}
func main() {
2022-07-26 22:21:35 -04:00
// Load .env
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
}
2022-07-26 22:21:35 -04:00
// Read API_KEY from .env and create app
2022-07-07 23:43:16 -04:00
cfg := config{
Units: "imperial",
ApiKey: os.Getenv("API_KEY"),
2022-07-07 23:43:16 -04:00
}
fcst := forecast{}
app := application{
2022-07-09 15:33:10 -04:00
Config: cfg,
Forecast: fcst,
Version: version,
}
2022-07-26 22:21:35 -04:00
// Display main menu
fmt.Println("\n=====================================================")
fmt.Printf("| Welcome to the OpenWeatherMap-gRPC Client! v%s |\n", app.Version)
fmt.Println("=====================================================")
2022-07-13 17:05:07 -04:00
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")
2022-07-26 22:21:35 -04:00
var option string
// Menu loop
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-13 17:05:07 -04:00
fmt.Println("2. Advanced options (Change units, precise location, etc.)")
fmt.Print("0. Exit\n\n")
2022-07-26 22:21:35 -04:00
// Read user input
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
2022-07-07 23:43:16 -04:00
log.Println(err)
}
option = strings.TrimSuffix(input, "\n")
2022-07-26 23:03:56 -04:00
}
// Check user input
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")
}
}