04059d8af6
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.sr.ht/~timharek/yr/pkg/browser"
|
|
"git.sr.ht/~timharek/yr/pkg/nominatim"
|
|
"git.sr.ht/~timharek/yr/yr"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func forecastHelper(args []string) *yr.ForecastResult {
|
|
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)
|
|
}
|
|
|
|
if isDebug {
|
|
if !isUTC {
|
|
f.Expires = f.Expires.Local()
|
|
f.LastModified = f.LastModified.Local()
|
|
}
|
|
fmt.Printf("Expires: %v\n", f.Expires)
|
|
fmt.Printf("LastModified: %v\n", f.LastModified)
|
|
fmt.Printf("Default cache-dir: %v\n", os.TempDir())
|
|
}
|
|
|
|
return f
|
|
}
|
|
|
|
// Opens u in browser with added coordinates and exits
|
|
func openBrowser(u string, f *yr.ForecastResult) {
|
|
url := u
|
|
if f != nil {
|
|
url = fmt.Sprintf(u, f.Coordinates.Latitude, f.Coordinates.Longitude)
|
|
}
|
|
err := browser.OpenURL(url)
|
|
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)
|
|
os.Exit(0)
|
|
}
|