main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 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. // discoverAll is a seam for tests.
  45. var discoverAll = device.DiscoverAll
  46. // runKeyboardInfo writes the connected QMK keyboards as JSON. The 1-based
  47. // index that --device accepts travels in the "index" field, so the command
  48. // emits no human-readable banner alongside it.
  49. func runKeyboardInfo(out io.Writer) error {
  50. devices, err := discoverAll()
  51. if err != nil {
  52. return err
  53. }
  54. if devices == nil {
  55. devices = []device.Device{}
  56. }
  57. type info struct {
  58. Devices []device.Device `json:"devices"`
  59. Total int `json:"total"`
  60. }
  61. return encodeJSON(out, info{Devices: devices, Total: len(devices)})
  62. }
  63. var keyboardInfoCmd = &cobra.Command{
  64. Use: "info",
  65. Short: "Discover connected QMK keyboards",
  66. Long: "Scan for connected QMK keyboards and print device info as JSON.\n" +
  67. "Each device carries the 1-based index that --device accepts.",
  68. RunE: func(cmd *cobra.Command, args []string) error {
  69. return runKeyboardInfo(cmd.OutOrStdout())
  70. },
  71. }
  72. func init() {
  73. rootCmd.AddCommand(keyboardCmd)
  74. keyboardCmd.AddCommand(keyboardInfoCmd)
  75. rootCmd.AddCommand(NewEnableCmd())
  76. rootCmd.AddCommand(NewDisableCmd())
  77. rootCmd.AddCommand(NewInfoCmd())
  78. rootCmd.AddCommand(NewEffectCmd())
  79. rootCmd.AddCommand(NewBrightnessCmd())
  80. rootCmd.AddCommand(NewSpeedCmd())
  81. rootCmd.AddCommand(NewColorCmd())
  82. rootCmd.AddCommand(NewModeCmd())
  83. rootCmd.AddCommand(NewProfileSaveCmd())
  84. rootCmd.AddCommand(NewProfileLoadCmd())
  85. rootCmd.AddCommand(NewProfileListCmd())
  86. rootCmd.AddCommand(NewProfileDeleteCmd())
  87. rootCmd.Version = version
  88. rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n")
  89. }