root.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Package cmd defines the command-line interface commands
  2. package cmd
  3. import (
  4. "fmt"
  5. "log/slog"
  6. "github.com/ayn2op/discordo/internal/config"
  7. "github.com/ayn2op/discordo/internal/consts"
  8. "github.com/ayn2op/discordo/internal/logger"
  9. "github.com/ayn2op/tview"
  10. "github.com/diamondburned/arikawa/v3/utils/ws"
  11. "github.com/diamondburned/ningen/v3"
  12. "github.com/gdamore/tcell/v2"
  13. "github.com/spf13/cobra"
  14. "github.com/zalando/go-keyring"
  15. )
  16. var (
  17. discordState *ningen.State
  18. app *application
  19. rootCmd = &cobra.Command{
  20. Use: consts.Name,
  21. RunE: func(cmd *cobra.Command, _ []string) error {
  22. flags := cmd.Flags()
  23. logLevel, _ := flags.GetString("log-level")
  24. var level slog.Level
  25. switch logLevel {
  26. case "debug":
  27. ws.EnableRawEvents = true
  28. level = slog.LevelDebug
  29. case "info":
  30. level = slog.LevelInfo
  31. case "warn":
  32. level = slog.LevelWarn
  33. case "error":
  34. level = slog.LevelError
  35. }
  36. if err := logger.Load(level); err != nil {
  37. return fmt.Errorf("failed to load logger: %w", err)
  38. }
  39. configPath, _ := flags.GetString("config")
  40. cfg, err := config.Load(configPath)
  41. if err != nil {
  42. return fmt.Errorf("failed to load config: %w", err)
  43. }
  44. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
  45. token, _ := flags.GetString("token")
  46. if token == "" {
  47. token, err = keyring.Get(consts.Name, "token")
  48. if err != nil {
  49. slog.Info("failed to retrieve token from keyring", "err", err)
  50. }
  51. }
  52. app = newApplication(cfg)
  53. return app.run(token)
  54. },
  55. }
  56. Execute = rootCmd.Execute
  57. )
  58. func init() {
  59. flags := rootCmd.Flags()
  60. flags.StringP("token", "t", "", "authentication token")
  61. flags.StringP("config", "c", config.DefaultPath(), "path of the configuration file")
  62. flags.String("log-level", "info", "log level")
  63. }