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 channel: backlight, rgblight, rgb_matrix, audio or led_matrix, or the name this keyboard's definition gives it") cmd.PersistentFlags().StringVar(&definitionFlag, "definition", "", "VIA definition file to read effect names from, instead of the one in the data directory") cmd.PersistentFlags().BoolVar(&jsonOutput, "json", false, "Print JSON instead of text") 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 text unless --json // asks otherwise. The 1-based index that --device accepts is in the "index" // field, so the machine shape emits no banner alongside it. Whether a keyboard // has effect names is reported per keyboard, because it differs: one board may // have a definition and the next may not. func runKeyboardInfo(out io.Writer) error { devices, err := discoverAll() if err != nil { return err } if devices == nil { devices = []device.Device{} } lines := describeDevices(devices) if !jsonOutput { return printKeyboardInfoText(out, lines) } type info struct { Devices []keyboardLine `json:"devices"` Total int `json:"total"` } return encodeJSON(out, info{Devices: lines, Total: len(lines)}) } 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.", Args: cobra.NoArgs, 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.AddCommand(NewDefinitionCmd()) rootCmd.Version = version rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n") }