2024-10-04 18:39:05 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-10-04 18:51:20 +00:00
|
|
|
"encoding/json"
|
2024-10-04 18:39:05 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"git.sr.ht/~timharek/yr/cmd/flags"
|
2024-10-04 19:05:21 +00:00
|
|
|
"git.sr.ht/~timharek/yr/pkg/nominatim"
|
2024-10-04 18:39:05 +00:00
|
|
|
"git.sr.ht/~timharek/yr/yr"
|
2024-10-04 18:44:57 +00:00
|
|
|
"github.com/pkg/browser"
|
2024-10-04 18:39:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type forecastH struct {
|
2024-10-04 21:20:26 +00:00
|
|
|
isJson bool
|
|
|
|
isDebug bool
|
|
|
|
isUTC bool
|
|
|
|
isWeb bool
|
|
|
|
f *yr.ForecastResult
|
2024-10-04 18:39:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
|
|
|
|
isJson, err := cmd.Flags().GetBool(flags.JSON)
|
|
|
|
cobra.CheckErr(err)
|
2024-10-04 21:20:26 +00:00
|
|
|
isDebug, err := cmd.Flags().GetBool(flags.DEBUG)
|
|
|
|
cobra.CheckErr(err)
|
2024-10-04 18:39:05 +00:00
|
|
|
isUTC, err := cmd.Flags().GetBool(flags.UTC)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
isWeb, err := cmd.Flags().GetBool(flags.WEB)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
lon, _ := cmd.Flags().GetFloat64(flags.LON)
|
|
|
|
lat, _ := cmd.Flags().GetFloat64(flags.LAT)
|
|
|
|
|
|
|
|
c, err := yr.New()
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
if len(args) == 0 && (lon == 0 || lat == 0) {
|
|
|
|
fmt.Fprintln(os.Stderr, "No location or coordinates provided.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
f := &yr.ForecastResult{}
|
|
|
|
if len(args) == 0 {
|
|
|
|
f, err = c.ForecastCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
} else {
|
|
|
|
location := args[0]
|
|
|
|
f, err = c.Forecast(location)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
}
|
|
|
|
|
2024-10-04 21:20:26 +00:00
|
|
|
if isDebug {
|
2024-10-04 21:38:23 +00:00
|
|
|
if !isUTC {
|
|
|
|
f.Expires = f.Expires.Local()
|
|
|
|
f.LastModified = f.LastModified.Local()
|
|
|
|
}
|
2024-10-04 21:20:26 +00:00
|
|
|
fmt.Printf("Expires: %v\n", f.Expires)
|
|
|
|
fmt.Printf("LastModified: %v\n", f.LastModified)
|
|
|
|
fmt.Printf("Default cache-dir: %v\n", os.TempDir())
|
|
|
|
}
|
|
|
|
|
2024-10-04 18:39:05 +00:00
|
|
|
return &forecastH{
|
|
|
|
isJson,
|
2024-10-04 21:20:26 +00:00
|
|
|
isDebug,
|
2024-10-04 18:39:05 +00:00
|
|
|
isUTC,
|
|
|
|
isWeb,
|
|
|
|
f,
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 18:44:57 +00:00
|
|
|
|
|
|
|
// Opens u in browser with added coordinates and exits
|
|
|
|
func openBrowser(u string, f *yr.ForecastResult) {
|
2024-10-04 21:26:35 +00:00
|
|
|
url := u
|
|
|
|
if f != nil {
|
|
|
|
url = fmt.Sprintf(u, f.Coordinates.Latitude, f.Coordinates.Longitude)
|
|
|
|
}
|
2024-10-04 18:44:57 +00:00
|
|
|
err := browser.OpenURL(url)
|
2024-10-04 18:51:20 +00:00
|
|
|
cobra.CheckErr(err)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns forecast in pretty printed JSON and exits
|
|
|
|
func outputJson(v any) {
|
|
|
|
j, err := json.MarshalIndent(v, "", " ")
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Printf("%s\n", j)
|
2024-10-04 18:44:57 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|