main.go 1.8 KB

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