refactor: Move duplicate code to helper

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-04 20:39:05 +02:00
parent 2de7524dea
commit 7f72ae918b
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
4 changed files with 69 additions and 94 deletions

View file

@ -7,8 +7,6 @@ import (
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"git.sr.ht/~timharek/yr/internal/nominatim"
"git.sr.ht/~timharek/yr/yr"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
@ -27,34 +25,12 @@ func init() {
}
func forecast(cmd *cobra.Command, args []string) {
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)
helper := forecastHelper(cmd, args)
f := helper.f
interval, _ := cmd.Flags().GetInt(flags.INTERVAL)
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 isWeb {
if helper.isWeb {
url := fmt.Sprintf("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url)
if err != nil {
@ -67,7 +43,7 @@ func forecast(cmd *cobra.Command, args []string) {
f.Forecast = f.Forecast[:interval]
}
if isJson {
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
@ -78,7 +54,7 @@ func forecast(cmd *cobra.Command, args []string) {
for _, item := range f.Forecast {
itemTime := item.Time.Local()
if isUTC {
if helper.isUTC {
itemTime = item.Time
}
t.Row(

53
cmd/forecast_helper.go Normal file
View file

@ -0,0 +1,53 @@
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,
}
}

View file

@ -5,9 +5,6 @@ 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/pkg/browser"
"github.com/spf13/cobra"
)
@ -25,35 +22,10 @@ func init() {
}
func now(cmd *cobra.Command, args []string) {
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)
helper := forecastHelper(cmd, args)
f := helper.f
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.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil)
cobra.CheckErr(err)
} else {
location := args[0]
f, err = c.Now(location)
cobra.CheckErr(err)
}
if isWeb {
if helper.isWeb {
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f/?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url)
if err != nil {
@ -64,7 +36,7 @@ func now(cmd *cobra.Command, args []string) {
n := f.Forecast[0]
if isJson {
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
@ -72,7 +44,7 @@ func now(cmd *cobra.Command, args []string) {
}
itemTime := n.Time.Local()
if isUTC {
if helper.isUTC {
itemTime = n.Time
}
fmt.Printf(

View file

@ -6,10 +6,7 @@ import (
"os"
"time"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"git.sr.ht/~timharek/yr/internal/nominatim"
"git.sr.ht/~timharek/yr/yr"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)
@ -26,33 +23,10 @@ func init() {
}
func today(cmd *cobra.Command, args []string) {
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)
helper := forecastHelper(cmd, args)
f := helper.f
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 isWeb {
if helper.isWeb {
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url)
if err != nil {
@ -61,7 +35,7 @@ func today(cmd *cobra.Command, args []string) {
os.Exit(0)
}
if isJson {
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
@ -76,7 +50,7 @@ func today(cmd *cobra.Command, args []string) {
continue
}
itemTime := item.Time.Local()
if isUTC {
if helper.isUTC {
itemTime = item.Time
}
t.Row(