package main import ( "fmt" "io" "os" "github.com/spf13/cobra" "netdome.biz/paul/qmk-rgb/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 QMK keyboard lighting", SilenceErrors: true, SilenceUsage: true, } cmd.PersistentFlags().StringVar(&targetDevice, "device", "", "Keyboard number as printed by 'keyboard info', starting at 1") cmd.PersistentFlags().StringVar(&targetZone, "zone", "", "RGB lighting zone (logo, backlight, side)") // The zone name is resolved against the connected board, which needs // enumeration but no HID handle, so an unknown name still fails before // anything is opened. openTarget does the resolving. cmd.PersistentPreRunE = func(*cobra.Command, []string) error { _, err := prepareTarget() 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", } // discoverAll is a seam for tests. var discoverAll = device.DiscoverAll // runKeyboardInfo writes the connected QMK keyboards as JSON. The 1-based // index that --device accepts travels in the "index" field, so the command // emits no human-readable banner alongside it. func runKeyboardInfo(out io.Writer) error { devices, err := discoverAll() if err != nil { return err } if devices == nil { devices = []device.Device{} } type info struct { Devices []device.Device `json:"devices"` Total int `json:"total"` } return encodeJSON(out, info{Devices: devices, Total: len(devices)}) } var keyboardInfoCmd = &cobra.Command{ Use: "info", Short: "Discover connected QMK keyboards", Long: "Scan for connected QMK keyboards and print device info as JSON.\n" + "Each device carries the 1-based index that --device accepts.", RunE: func(cmd *cobra.Command, args []string) error { return runKeyboardInfo(cmd.OutOrStdout()) }, } func init() { rootCmd.AddCommand(keyboardCmd) keyboardCmd.AddCommand(keyboardInfoCmd) 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") }