package main import ( "encoding/json" "fmt" "os" "github.com/spf13/cobra" "netdome.biz/paul/impact-80/internal/device" ) // version is set at build time via -ldflags. var version = "dev" var ( targetDevice string targetZone string ) func newRootCommand() *cobra.Command { cmd := &cobra.Command{ Use: "qmk-rgb-tool", Short: "QMK RGB CLI — control VIA-compatible keyboard lighting", SilenceErrors: true, SilenceUsage: true, } cmd.PersistentFlags().StringVar(&targetDevice, "device", "", "HID device path to use") cmd.PersistentFlags().StringVar(&targetZone, "zone", "", "RGB lighting zone (logo, backlight, side)") cmd.PersistentPreRunE = func(*cobra.Command, []string) error { _, err := selectedZones() return err } return cmd } var rootCmd = newRootCommand() func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } } func main() { Execute() } var keyboardCmd = &cobra.Command{ Use: "keyboard", Short: "Keyboard management commands", } var keyboardInfoCmd = &cobra.Command{ Use: "info", Short: "Discover connected VIA-compatible keyboards", Long: "Scan for connected VIA-compatible keyboards and print device info as JSON.", Run: func(cmd *cobra.Command, args []string) { keyboards, err := device.LoadKeyboards() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } devices, err := device.Discover(keyboards) if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } if len(devices) == 0 { fmt.Println(`{"devices":[]}`) return } type Info struct { Devices []device.Device `json:"devices"` Total int `json:"total"` } out := Info{Devices: devices, Total: len(devices)} data, _ := json.MarshalIndent(out, "", " ") fmt.Println(string(data)) }, } var keyboardListCmd = &cobra.Command{ Use: "list", Short: "List supported keyboards", Long: "Print all keyboards from keyboards.json as JSON.", Run: func(cmd *cobra.Command, args []string) { keyboards, err := device.LoadKeyboards() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } if len(keyboards) == 0 { fmt.Println(`{"keyboards":[]}`) return } type Info struct { Keyboards []device.Keyboard `json:"keyboards"` Total int `json:"total"` } out := Info{Keyboards: keyboards, Total: len(keyboards)} data, _ := json.MarshalIndent(out, "", " ") fmt.Println(string(data)) }, } func init() { rootCmd.AddCommand(keyboardCmd) keyboardCmd.AddCommand(keyboardInfoCmd) keyboardCmd.AddCommand(keyboardListCmd) rootCmd.AddCommand(NewEnableCmd()) rootCmd.AddCommand(NewDisableCmd()) rootCmd.AddCommand(NewInfoCmd()) rootCmd.AddCommand(NewEffectCmd()) rootCmd.AddCommand(NewBrightnessCmd()) rootCmd.AddCommand(NewSpeedCmd()) rootCmd.AddCommand(NewColorCmd()) rootCmd.AddCommand(NewModeCmd()) rootCmd.AddCommand(NewProfileSaveCmd()) rootCmd.AddCommand(NewProfileLoadCmd()) rootCmd.AddCommand(NewProfileListCmd()) rootCmd.AddCommand(NewProfileDeleteCmd()) rootCmd.Version = version rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n") }