root.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package cmd
  2. import (
  3. "log/slog"
  4. "github.com/ayn2op/discordo/internal/config"
  5. "github.com/ayn2op/discordo/internal/consts"
  6. "github.com/ayn2op/discordo/internal/logger"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/rivo/tview"
  9. "github.com/spf13/cobra"
  10. "github.com/zalando/go-keyring"
  11. )
  12. var (
  13. discordState *State
  14. app *App
  15. )
  16. var (
  17. rootCmd = &cobra.Command{
  18. RunE: func(cmd *cobra.Command, args []string) error {
  19. if err := logger.Load(); err != nil {
  20. return err
  21. }
  22. token, _ := cmd.Flags().GetString("token")
  23. if token == "" {
  24. var err error
  25. token, err = keyring.Get(consts.Name, "token")
  26. if err != nil {
  27. slog.Info("failed to retrieve token from keyring", "err", err)
  28. }
  29. }
  30. cfg, err := config.Load()
  31. if err != nil {
  32. return err
  33. }
  34. tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
  35. tview.Borders.Horizontal = cfg.Theme.Border.Preset.Horizontal
  36. tview.Borders.Vertical = cfg.Theme.Border.Preset.Vertical
  37. tview.Borders.TopLeft = cfg.Theme.Border.Preset.TopLeft
  38. tview.Borders.TopRight = cfg.Theme.Border.Preset.TopRight
  39. tview.Borders.BottomLeft = cfg.Theme.Border.Preset.BottomLeft
  40. tview.Borders.BottomRight = cfg.Theme.Border.Preset.BottomRight
  41. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  42. tview.Borders.VerticalFocus = tview.Borders.Vertical
  43. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  44. tview.Borders.TopRightFocus = tview.Borders.TopRight
  45. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  46. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  47. app = newApp(cfg)
  48. return app.run(token)
  49. },
  50. }
  51. Execute = rootCmd.Execute
  52. )
  53. func init() {
  54. rootCmd.Flags().StringP("token", "t", "", "the authentication token")
  55. }