package cli import ( "fmt" "github.com/spf13/cobra" ) var ( profileLat float64 profileLon float64 profileTZ string ) func init() { profileCmd := &cobra.Command{ Use: "profile", Short: "Manage location profiles", } createCmd := &cobra.Command{ Use: "create ", Short: "Create a new location profile", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { p, err := db.CreateProfile(args[0], profileLat, profileLon, profileTZ) if err != nil { return err } fmt.Printf("Profile created: %s (ID: %d, %.4f, %.4f)\n", p.Name, p.ID, p.Latitude, p.Longitude) return nil }, } createCmd.Flags().Float64Var(&profileLat, "lat", 0, "latitude") createCmd.Flags().Float64Var(&profileLon, "lon", 0, "longitude") createCmd.Flags().StringVar(&profileTZ, "tz", "Europe/Berlin", "timezone") createCmd.MarkFlagRequired("lat") createCmd.MarkFlagRequired("lon") showCmd := &cobra.Command{ Use: "show", Short: "Display current profile", RunE: func(cmd *cobra.Command, args []string) error { p, err := getActiveProfile() if err != nil { return err } fmt.Printf("Profile: %s (ID: %d)\n", p.Name, p.ID) fmt.Printf(" Location: %.4f, %.4f\n", p.Latitude, p.Longitude) fmt.Printf(" Timezone: %s\n", p.Timezone) fmt.Printf(" Created: %s\n", p.CreatedAt.Format("2006-01-02 15:04")) return nil }, } editCmd := &cobra.Command{ Use: "edit ", Short: "Edit a profile field", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { p, err := getActiveProfile() if err != nil { return err } return db.UpdateProfile(p.ID, args[0], args[1]) }, } deleteCmd := &cobra.Command{ Use: "delete", Short: "Delete current profile", RunE: func(cmd *cobra.Command, args []string) error { p, err := getActiveProfile() if err != nil { return err } if err := db.DeleteProfile(p.ID); err != nil { return err } fmt.Printf("Profile %q deleted\n", p.Name) return nil }, } listCmd := &cobra.Command{ Use: "list", Short: "List all profiles", RunE: func(cmd *cobra.Command, args []string) error { profiles, err := db.ListProfiles() if err != nil { return err } if len(profiles) == 0 { fmt.Println("No profiles found") return nil } for _, p := range profiles { fmt.Printf(" [%d] %s (%.4f, %.4f, %s)\n", p.ID, p.Name, p.Latitude, p.Longitude, p.Timezone) } return nil }, } profileCmd.AddCommand(createCmd, showCmd, editCmd, deleteCmd, listCmd) rootCmd.AddCommand(profileCmd) }