main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "netdome.biz/paul/qmk-rgb/internal/device"
  8. )
  9. // version is set at build time via -ldflags.
  10. var version = "dev"
  11. var (
  12. targetDevice string
  13. targetZone string
  14. )
  15. func newRootCommand() *cobra.Command {
  16. cmd := &cobra.Command{
  17. Use: "qmk-rgb-tool",
  18. Short: "QMK RGB CLI — control QMK keyboard lighting",
  19. SilenceErrors: true,
  20. SilenceUsage: true,
  21. }
  22. cmd.PersistentFlags().StringVar(&targetDevice, "device", "", "Keyboard number as printed by 'keyboard info', starting at 1")
  23. cmd.PersistentFlags().StringVar(&targetZone, "zone", "", "RGB lighting zone (logo, backlight, side)")
  24. // The zone name is resolved against the connected board, which needs
  25. // enumeration but no HID handle, so an unknown name still fails before
  26. // anything is opened. openTarget does the resolving.
  27. cmd.PersistentPreRunE = func(*cobra.Command, []string) error {
  28. _, err := prepareTarget()
  29. return err
  30. }
  31. return cmd
  32. }
  33. var rootCmd = newRootCommand()
  34. func Execute() {
  35. if err := rootCmd.Execute(); err != nil {
  36. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  37. os.Exit(1)
  38. }
  39. }
  40. func main() {
  41. Execute()
  42. }
  43. var keyboardCmd = &cobra.Command{
  44. Use: "keyboard",
  45. Short: "Keyboard management commands",
  46. }
  47. // discoverAll is a seam for tests.
  48. var discoverAll = device.DiscoverAll
  49. // runKeyboardInfo writes the connected QMK keyboards as JSON. The 1-based
  50. // index that --device accepts travels in the "index" field, so the command
  51. // emits no human-readable banner alongside it.
  52. func runKeyboardInfo(out io.Writer) error {
  53. devices, err := discoverAll()
  54. if err != nil {
  55. return err
  56. }
  57. if devices == nil {
  58. devices = []device.Device{}
  59. }
  60. type info struct {
  61. Devices []device.Device `json:"devices"`
  62. Total int `json:"total"`
  63. }
  64. return encodeJSON(out, info{Devices: devices, Total: len(devices)})
  65. }
  66. var keyboardInfoCmd = &cobra.Command{
  67. Use: "info",
  68. Short: "Discover connected QMK keyboards",
  69. Long: "Scan for connected QMK keyboards and print device info as JSON.\n" +
  70. "Each device carries the 1-based index that --device accepts.",
  71. RunE: func(cmd *cobra.Command, args []string) error {
  72. return runKeyboardInfo(cmd.OutOrStdout())
  73. },
  74. }
  75. func init() {
  76. rootCmd.AddCommand(keyboardCmd)
  77. keyboardCmd.AddCommand(keyboardInfoCmd)
  78. rootCmd.AddCommand(NewEnableCmd())
  79. rootCmd.AddCommand(NewDisableCmd())
  80. rootCmd.AddCommand(NewInfoCmd())
  81. rootCmd.AddCommand(NewEffectCmd())
  82. rootCmd.AddCommand(NewBrightnessCmd())
  83. rootCmd.AddCommand(NewSpeedCmd())
  84. rootCmd.AddCommand(NewColorCmd())
  85. rootCmd.AddCommand(NewModeCmd())
  86. rootCmd.AddCommand(NewProfileSaveCmd())
  87. rootCmd.AddCommand(NewProfileLoadCmd())
  88. rootCmd.AddCommand(NewProfileListCmd())
  89. rootCmd.AddCommand(NewProfileDeleteCmd())
  90. rootCmd.Version = version
  91. rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n")
  92. }