main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "netdome.biz/paul/impact-80/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 VIA-compatible keyboard lighting",
  19. SilenceErrors: true,
  20. SilenceUsage: true,
  21. }
  22. cmd.PersistentFlags().StringVar(&targetDevice, "device", "", "HID device path to use")
  23. cmd.PersistentFlags().StringVar(&targetZone, "zone", "", "RGB lighting zone (logo, backlight, side)")
  24. cmd.PersistentPreRunE = func(*cobra.Command, []string) error {
  25. _, err := selectedZones()
  26. return err
  27. }
  28. return cmd
  29. }
  30. var rootCmd = newRootCommand()
  31. func Execute() {
  32. if err := rootCmd.Execute(); err != nil {
  33. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  34. os.Exit(1)
  35. }
  36. }
  37. func main() {
  38. Execute()
  39. }
  40. var keyboardCmd = &cobra.Command{
  41. Use: "keyboard",
  42. Short: "Keyboard management commands",
  43. }
  44. var keyboardInfoCmd = &cobra.Command{
  45. Use: "info",
  46. Short: "Discover connected VIA-compatible keyboards",
  47. Long: "Scan for connected VIA-compatible keyboards and print device info as JSON.",
  48. Run: func(cmd *cobra.Command, args []string) {
  49. keyboards, err := device.LoadKeyboards()
  50. if err != nil {
  51. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  52. os.Exit(1)
  53. }
  54. devices, err := device.Discover(keyboards)
  55. if err != nil {
  56. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  57. os.Exit(1)
  58. }
  59. if len(devices) == 0 {
  60. fmt.Println(`{"devices":[]}`)
  61. return
  62. }
  63. type Info struct {
  64. Devices []device.Device `json:"devices"`
  65. Total int `json:"total"`
  66. }
  67. out := Info{Devices: devices, Total: len(devices)}
  68. data, _ := json.MarshalIndent(out, "", " ")
  69. fmt.Println(string(data))
  70. },
  71. }
  72. var keyboardListCmd = &cobra.Command{
  73. Use: "list",
  74. Short: "List supported keyboards",
  75. Long: "Print all keyboards from keyboards.json as JSON.",
  76. Run: func(cmd *cobra.Command, args []string) {
  77. keyboards, err := device.LoadKeyboards()
  78. if err != nil {
  79. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  80. os.Exit(1)
  81. }
  82. if len(keyboards) == 0 {
  83. fmt.Println(`{"keyboards":[]}`)
  84. return
  85. }
  86. type Info struct {
  87. Keyboards []device.Keyboard `json:"keyboards"`
  88. Total int `json:"total"`
  89. }
  90. out := Info{Keyboards: keyboards, Total: len(keyboards)}
  91. data, _ := json.MarshalIndent(out, "", " ")
  92. fmt.Println(string(data))
  93. },
  94. }
  95. func init() {
  96. rootCmd.AddCommand(keyboardCmd)
  97. keyboardCmd.AddCommand(keyboardInfoCmd)
  98. keyboardCmd.AddCommand(keyboardListCmd)
  99. rootCmd.AddCommand(NewEnableCmd())
  100. rootCmd.AddCommand(NewDisableCmd())
  101. rootCmd.AddCommand(NewInfoCmd())
  102. rootCmd.AddCommand(NewEffectCmd())
  103. rootCmd.AddCommand(NewBrightnessCmd())
  104. rootCmd.AddCommand(NewSpeedCmd())
  105. rootCmd.AddCommand(NewColorCmd())
  106. rootCmd.AddCommand(NewModeCmd())
  107. rootCmd.AddCommand(NewProfileSaveCmd())
  108. rootCmd.AddCommand(NewProfileLoadCmd())
  109. rootCmd.AddCommand(NewProfileListCmd())
  110. rootCmd.AddCommand(NewProfileDeleteCmd())
  111. rootCmd.Version = version
  112. rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n")
  113. }