package main import ( "encoding/json" "fmt" "os" "github.com/spf13/cobra" "github.com/wobkey/rgb/cmd/wobkey/rgb" "github.com/wobkey/rgb/internal/device" ) // version is set at build time via -ldflags. var version = "dev" func newRootCommand() *cobra.Command { return &cobra.Command{ Use: "wobkey", Short: "Wobkey RGB CLI — control VIA-compatible keyboard lighting", SilenceErrors: true, SilenceUsage: true, } } 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) rgbCmd := rgb.Init() rgbCmd.AddCommand( rgb.NewEffectCmd(), rgb.NewBrightnessCmd(), rgb.NewSpeedCmd(), rgb.NewColorCmd(), rgb.NewModeCmd(), rgb.NewEnableCmd(), rgb.NewDisableCmd(), rgb.NewInfoCmd(), rgb.NewProfileSaveCmd(), rgb.NewProfileLoadCmd(), rgb.NewProfileListCmd(), rgb.NewProfileDeleteCmd(), ) rootCmd.AddCommand(rgbCmd) rootCmd.Version = version rootCmd.SetVersionTemplate("wobkey {{.Version}}\n") }