main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 the name this keyboard's definition gives it")
  24. cmd.PersistentFlags().StringVar(&definitionFlag, "definition", "", "VIA definition file to read effect names from, instead of the one in the data directory")
  25. cmd.PersistentFlags().BoolVar(&jsonOutput, "json", false, "Print JSON instead of text")
  26. return cmd
  27. }
  28. var rootCmd = newRootCommand()
  29. func Execute() {
  30. if err := rootCmd.Execute(); err != nil {
  31. fmt.Fprintf(os.Stderr, "Error: %v\n", err)
  32. os.Exit(1)
  33. }
  34. }
  35. func main() {
  36. Execute()
  37. }
  38. var keyboardCmd = &cobra.Command{
  39. Use: "keyboard",
  40. Short: "Keyboard management commands",
  41. }
  42. // discoverAll is a seam for tests.
  43. var discoverAll = device.DiscoverAll
  44. // runKeyboardInfo writes the connected QMK keyboards, as text unless --json
  45. // asks otherwise. The 1-based index that --device accepts is in the "index"
  46. // field, so the machine shape emits no banner alongside it. Whether a keyboard
  47. // has effect names is reported per keyboard, because it differs: one board may
  48. // have a definition and the next may not.
  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. lines := describeDevices(devices)
  58. if !jsonOutput {
  59. return printKeyboardInfoText(out, lines)
  60. }
  61. type info struct {
  62. Devices []keyboardLine `json:"devices"`
  63. Total int `json:"total"`
  64. }
  65. return encodeJSON(out, info{Devices: lines, Total: len(lines)})
  66. }
  67. var keyboardInfoCmd = &cobra.Command{
  68. Use: "info",
  69. Short: "Discover connected QMK keyboards",
  70. Long: "Scan for connected QMK keyboards and print device info as JSON.\n" +
  71. "Each device carries the 1-based index that --device accepts.",
  72. Args: cobra.NoArgs,
  73. RunE: func(cmd *cobra.Command, args []string) error {
  74. return runKeyboardInfo(cmd.OutOrStdout())
  75. },
  76. }
  77. func init() {
  78. rootCmd.AddCommand(keyboardCmd)
  79. keyboardCmd.AddCommand(keyboardInfoCmd)
  80. rootCmd.AddCommand(NewEnableCmd())
  81. rootCmd.AddCommand(NewDisableCmd())
  82. rootCmd.AddCommand(NewInfoCmd())
  83. rootCmd.AddCommand(NewEffectCmd())
  84. rootCmd.AddCommand(NewBrightnessCmd())
  85. rootCmd.AddCommand(NewSpeedCmd())
  86. rootCmd.AddCommand(NewColorCmd())
  87. rootCmd.AddCommand(NewModeCmd())
  88. rootCmd.AddCommand(NewProfileSaveCmd())
  89. rootCmd.AddCommand(NewProfileLoadCmd())
  90. rootCmd.AddCommand(NewProfileListCmd())
  91. rootCmd.AddCommand(NewProfileDeleteCmd())
  92. rootCmd.AddCommand(NewDefinitionCmd())
  93. rootCmd.Version = version
  94. rootCmd.SetVersionTemplate(" qmk-rgb-tool {{.Version}}\n")
  95. }