main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "netdome.biz/paul/impact-80/cmd/qmk-rgb/rgb"
  8. "netdome.biz/paul/impact-80/internal/device"
  9. )
  10. // version is set at build time via -ldflags.
  11. var version = "dev"
  12. func newRootCommand() *cobra.Command {
  13. return &cobra.Command{
  14. Use: "qmk-rgb",
  15. Short: "QMK VIA RGB CLI — control VIA-compatible keyboard lighting",
  16. SilenceErrors: true,
  17. SilenceUsage: true,
  18. }
  19. }
  20. var rootCmd = newRootCommand()
  21. func Execute() {
  22. if err := rootCmd.Execute(); err != nil {
  23. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  24. os.Exit(1)
  25. }
  26. }
  27. func main() {
  28. Execute()
  29. }
  30. var keyboardCmd = &cobra.Command{
  31. Use: "keyboard",
  32. Short: "Keyboard management commands",
  33. }
  34. var keyboardInfoCmd = &cobra.Command{
  35. Use: "info",
  36. Short: "Discover connected VIA-compatible keyboards",
  37. Long: "Scan for connected VIA-compatible keyboards and print device info as JSON.",
  38. Run: func(cmd *cobra.Command, args []string) {
  39. keyboards, err := device.LoadKeyboards()
  40. if err != nil {
  41. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  42. os.Exit(1)
  43. }
  44. devices, err := device.Discover(keyboards)
  45. if err != nil {
  46. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  47. os.Exit(1)
  48. }
  49. if len(devices) == 0 {
  50. fmt.Println(`{"devices":[]}`)
  51. return
  52. }
  53. type Info struct {
  54. Devices []device.Device `json:"devices"`
  55. Total int `json:"total"`
  56. }
  57. out := Info{Devices: devices, Total: len(devices)}
  58. data, _ := json.MarshalIndent(out, "", " ")
  59. fmt.Println(string(data))
  60. },
  61. }
  62. var keyboardListCmd = &cobra.Command{
  63. Use: "list",
  64. Short: "List supported keyboards",
  65. Long: "Print all keyboards from keyboards.json as JSON.",
  66. Run: func(cmd *cobra.Command, args []string) {
  67. keyboards, err := device.LoadKeyboards()
  68. if err != nil {
  69. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  70. os.Exit(1)
  71. }
  72. if len(keyboards) == 0 {
  73. fmt.Println(`{"keyboards":[]}`)
  74. return
  75. }
  76. type Info struct {
  77. Keyboards []device.Keyboard `json:"keyboards"`
  78. Total int `json:"total"`
  79. }
  80. out := Info{Keyboards: keyboards, Total: len(keyboards)}
  81. data, _ := json.MarshalIndent(out, "", " ")
  82. fmt.Println(string(data))
  83. },
  84. }
  85. func init() {
  86. rootCmd.AddCommand(keyboardCmd)
  87. keyboardCmd.AddCommand(keyboardInfoCmd)
  88. keyboardCmd.AddCommand(keyboardListCmd)
  89. rgbCmd := rgb.Init()
  90. rgbCmd.AddCommand(
  91. rgb.NewEffectCmd(),
  92. rgb.NewBrightnessCmd(),
  93. rgb.NewSpeedCmd(),
  94. rgb.NewColorCmd(),
  95. rgb.NewModeCmd(),
  96. rgb.NewEnableCmd(),
  97. rgb.NewDisableCmd(),
  98. rgb.NewInfoCmd(),
  99. rgb.NewProfileSaveCmd(),
  100. rgb.NewProfileLoadCmd(),
  101. rgb.NewProfileListCmd(),
  102. rgb.NewProfileDeleteCmd(),
  103. )
  104. rootCmd.AddCommand(rgbCmd)
  105. rootCmd.Version = version
  106. rootCmd.SetVersionTemplate(" qmk-rgb {{.Version}}\n")
  107. }