main.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/wobkey/rgb/cmd/wobkey/rgb"
  8. "github.com/wobkey/rgb/internal/device"
  9. )
  10. var rootCmd = &cobra.Command{
  11. Use: "wobkey",
  12. Short: "Wobkey RGB CLI — control VIA-compatible keyboard lighting",
  13. }
  14. func Execute() {
  15. if err := rootCmd.Execute(); err != nil {
  16. fmt.Fprintln(os.Stderr, err)
  17. os.Exit(1)
  18. }
  19. }
  20. func main() {
  21. Execute()
  22. }
  23. var keyboardCmd = &cobra.Command{
  24. Use: "keyboard",
  25. Short: "Keyboard management commands",
  26. }
  27. var keyboardInfoCmd = &cobra.Command{
  28. Use: "info",
  29. Short: "Discover connected VIA-compatible keyboards",
  30. Long: "Scan for connected VIA-compatible keyboards and print device info as JSON.",
  31. Run: func(cmd *cobra.Command, args []string) {
  32. keyboards, err := device.LoadKeyboards()
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  35. os.Exit(1)
  36. }
  37. devices, err := device.Discover(keyboards)
  38. if err != nil {
  39. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  40. os.Exit(1)
  41. }
  42. if len(devices) == 0 {
  43. fmt.Println(`{"devices":[]}`)
  44. return
  45. }
  46. type Info struct {
  47. Devices []device.Device `json:"devices"`
  48. Total int `json:"total"`
  49. }
  50. out := Info{Devices: devices, Total: len(devices)}
  51. data, _ := json.MarshalIndent(out, "", " ")
  52. fmt.Println(string(data))
  53. },
  54. }
  55. func init() {
  56. rootCmd.AddCommand(keyboardCmd)
  57. keyboardCmd.AddCommand(keyboardInfoCmd)
  58. rgbCmd := rgb.Init()
  59. rgbCmd.AddCommand(
  60. rgb.NewEffectCmd(),
  61. rgb.NewBrightnessCmd(),
  62. rgb.NewSpeedCmd(),
  63. rgb.NewColorCmd(),
  64. rgb.NewModeCmd(),
  65. rgb.NewEnableCmd(),
  66. rgb.NewDisableCmd(),
  67. rgb.NewInfoCmd(),
  68. )
  69. rootCmd.AddCommand(rgbCmd)
  70. }