main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 channel: backlight, rgblight, rgb_matrix, audio or led_matrix, or a name from keyboards.json")
  24. return cmd
  25. }
  26. var rootCmd = newRootCommand()
  27. func Execute() {
  28. if err := rootCmd.Execute(); err != nil {
  29. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  30. os.Exit(1)
  31. }
  32. }
  33. func main() {
  34. Execute()
  35. }
  36. var keyboardCmd = &cobra.Command{
  37. Use: "keyboard",
  38. Short: "Keyboard management commands",
  39. }
  40. // discoverAll is a seam for tests.
  41. var discoverAll = device.DiscoverAll
  42. // runKeyboardInfo writes the connected QMK keyboards as JSON. The 1-based
  43. // index that --device accepts travels in the "index" field, so the command
  44. // emits no human-readable banner alongside it.
  45. func runKeyboardInfo(out io.Writer) error {
  46. devices, err := discoverAll()
  47. if err != nil {
  48. return err
  49. }
  50. if devices == nil {
  51. devices = []device.Device{}
  52. }
  53. type info struct {
  54. Devices []device.Device `json:"devices"`
  55. Total int `json:"total"`
  56. }
  57. return encodeJSON(out, info{Devices: devices, Total: len(devices)})
  58. }
  59. var keyboardInfoCmd = &cobra.Command{
  60. Use: "info",
  61. Short: "Discover connected QMK keyboards",
  62. Long: "Scan for connected QMK keyboards and print device info as JSON.\n" +
  63. "Each device carries the 1-based index that --device accepts.",
  64. RunE: func(cmd *cobra.Command, args []string) error {
  65. return runKeyboardInfo(cmd.OutOrStdout())
  66. },
  67. }
  68. func init() {
  69. rootCmd.AddCommand(keyboardCmd)
  70. keyboardCmd.AddCommand(keyboardInfoCmd)
  71. rootCmd.AddCommand(NewEnableCmd())
  72. rootCmd.AddCommand(NewDisableCmd())
  73. rootCmd.AddCommand(NewInfoCmd())
  74. rootCmd.AddCommand(NewEffectCmd())
  75. rootCmd.AddCommand(NewBrightnessCmd())
  76. rootCmd.AddCommand(NewSpeedCmd())
  77. rootCmd.AddCommand(NewColorCmd())
  78. rootCmd.AddCommand(NewModeCmd())
  79. rootCmd.AddCommand(NewProfileSaveCmd())
  80. rootCmd.AddCommand(NewProfileLoadCmd())
  81. rootCmd.AddCommand(NewProfileListCmd())
  82. rootCmd.AddCommand(NewProfileDeleteCmd())
  83. rootCmd.Version = version
  84. rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n")
  85. }