yr/cmd/forecast_helper.go
Tim Hårek Andreassen 7f72ae918b
refactor: Move duplicate code to helper
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-04 20:39:05 +02:00

53 lines
1.1 KiB
Go

package cmd
import (
"fmt"
"os"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/internal/nominatim"
"git.sr.ht/~timharek/yr/yr"
"github.com/spf13/cobra"
)
type forecastH struct {
isJson bool
isUTC bool
isWeb bool
f *yr.ForecastResult
}
func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
isJson, err := cmd.Flags().GetBool(flags.JSON)
cobra.CheckErr(err)
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)
}
return &forecastH{
isJson,
isUTC,
isWeb,
f,
}
}