feat: Introduce cmd flags package

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-02 15:47:49 +02:00
parent d84f17e7ab
commit 36309212a7
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
3 changed files with 15 additions and 6 deletions

7
cmd/flags/flags.go Normal file
View file

@ -0,0 +1,7 @@
package flags
const (
LON = "lon"
LAT = "lat"
JSON = "json"
)

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"git.sr.ht/~timharek/yr-go/cmd/flags"
"git.sr.ht/~timharek/yr-go/internal/nominatim" "git.sr.ht/~timharek/yr-go/internal/nominatim"
"git.sr.ht/~timharek/yr-go/yr" "git.sr.ht/~timharek/yr-go/yr"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -23,10 +24,10 @@ func init() {
} }
func now(cmd *cobra.Command, args []string) { func now(cmd *cobra.Command, args []string) {
isJson, err := cmd.Flags().GetBool("json") isJson, err := cmd.Flags().GetBool(flags.JSON)
cobra.CheckErr(err) cobra.CheckErr(err)
lon, _ := cmd.Flags().GetFloat64("lon") lon, _ := cmd.Flags().GetFloat64(flags.LON)
lat, _ := cmd.Flags().GetFloat64("lat") lat, _ := cmd.Flags().GetFloat64(flags.LAT)
c, err := yr.New() c, err := yr.New()
cobra.CheckErr(err) cobra.CheckErr(err)

View file

@ -3,6 +3,7 @@ package cmd
import ( import (
"os" "os"
"git.sr.ht/~timharek/yr-go/cmd/flags"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -20,7 +21,7 @@ func Execute() {
} }
func init() { func init() {
rootCmd.PersistentFlags().Bool("json", false, "Result in JSON") rootCmd.PersistentFlags().Bool(flags.JSON, false, "Result in JSON")
rootCmd.PersistentFlags().Float64P("lon", "x", 0, "Longitude coordinate") rootCmd.PersistentFlags().Float64P(flags.LON, "x", 0, "Longitude coordinate")
rootCmd.PersistentFlags().Float64P("lat", "y", 0, "Latitude coordinate") rootCmd.PersistentFlags().Float64P(flags.LAT, "y", 0, "Latitude coordinate")
} }