| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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 a name from keyboards.json")
- 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")
- }
|