root.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Package cmd defines the command-line interface commands
  2. package cmd
  3. import (
  4. "flag"
  5. "fmt"
  6. "log/slog"
  7. "github.com/ayn2op/discordo/internal/config"
  8. "github.com/ayn2op/discordo/internal/consts"
  9. "github.com/ayn2op/discordo/internal/logger"
  10. "github.com/ayn2op/tview"
  11. "github.com/diamondburned/arikawa/v3/utils/ws"
  12. "github.com/diamondburned/ningen/v3"
  13. "github.com/zalando/go-keyring"
  14. )
  15. var (
  16. discordState *ningen.State
  17. app *application
  18. )
  19. func Run() error {
  20. tokenFlag := flag.String("token", "", "authentication token")
  21. configPath := flag.String("config-path", config.DefaultPath(), "path of the configuration file")
  22. logPath := flag.String("log-path", logger.DefaultPath(), "path of the log file")
  23. logLevel := flag.String("log-level", "info", "log level")
  24. flag.Parse()
  25. var level slog.Level
  26. switch *logLevel {
  27. case "debug":
  28. ws.EnableRawEvents = true
  29. level = slog.LevelDebug
  30. case "info":
  31. level = slog.LevelInfo
  32. case "warn":
  33. level = slog.LevelWarn
  34. case "error":
  35. level = slog.LevelError
  36. }
  37. if err := logger.Load(*logPath, level); err != nil {
  38. return fmt.Errorf("failed to load logger: %w", err)
  39. }
  40. cfg, err := config.Load(*configPath)
  41. if err != nil {
  42. return fmt.Errorf("failed to load config: %w", err)
  43. }
  44. token := *tokenFlag
  45. if token == "" {
  46. token, err = keyring.Get(consts.Name, "token")
  47. if err != nil {
  48. slog.Info("failed to retrieve token from keyring", "err", err)
  49. }
  50. }
  51. tview.Styles = tview.Theme{}
  52. app = newApplication(cfg)
  53. return app.run(token)
  54. }