main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "github.com/ayntgl/discordo/config"
  8. "github.com/ayntgl/discordo/ui"
  9. "github.com/rivo/tview"
  10. "github.com/zalando/go-keyring"
  11. )
  12. const (
  13. name = "discordo"
  14. )
  15. var (
  16. token string
  17. configPath string
  18. )
  19. func init() {
  20. flag.StringVar(&token, "token", "", "The client authentication token.")
  21. // If the token is provided via a command-line flag, store it in the default keyring.
  22. if token != "" {
  23. go keyring.Set(name, "token", token)
  24. }
  25. if token == "" {
  26. token, _ = keyring.Get(name, "token")
  27. }
  28. configDirPath, err := os.UserConfigDir()
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. flag.StringVar(&configPath, "config", filepath.Join(configDirPath, name), "The path to the configuration directory.")
  33. }
  34. func main() {
  35. flag.Parse()
  36. cfg := config.New(configPath)
  37. err := cfg.Load()
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. c := ui.NewCore(cfg)
  42. if token != "" {
  43. err = c.Run(token)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. c.DrawMainFlex()
  48. c.Application.SetRoot(c.MainFlex, true)
  49. c.Application.SetFocus(c.GuildsTree)
  50. } else {
  51. loginForm := tview.NewForm()
  52. loginForm.AddPasswordField("Token", "", 0, 0, nil)
  53. loginForm.SetButtonsAlign(tview.AlignCenter)
  54. loginForm.SetTitle("Login")
  55. loginForm.SetTitleAlign(tview.AlignLeft)
  56. loginForm.SetBorder(true)
  57. loginForm.SetBorderPadding(0, 0, 1, 1)
  58. loginForm.AddButton("Login", func() {
  59. tkn := loginForm.GetFormItem(0).(*tview.InputField).GetText()
  60. if tkn == "" {
  61. return
  62. }
  63. err := c.Run(tkn)
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. go keyring.Set(name, "token", tkn)
  68. c.DrawMainFlex()
  69. c.Application.SetRoot(c.MainFlex, true)
  70. c.Application.SetFocus(c.GuildsTree)
  71. })
  72. c.Application.SetRoot(loginForm, true)
  73. }
  74. tview.Borders.TopLeftFocus = tview.Borders.TopLeft
  75. tview.Borders.TopRightFocus = tview.Borders.TopRight
  76. tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
  77. tview.Borders.BottomRightFocus = tview.Borders.BottomRight
  78. tview.Borders.HorizontalFocus = tview.Borders.Horizontal
  79. tview.Borders.VerticalFocus = tview.Borders.Vertical
  80. tview.Borders.TopLeft = 0
  81. tview.Borders.TopRight = 0
  82. tview.Borders.BottomLeft = 0
  83. tview.Borders.BottomRight = 0
  84. tview.Borders.Horizontal = 0
  85. tview.Borders.Vertical = 0
  86. err = c.Application.Run()
  87. if err != nil {
  88. log.Fatal(err)
  89. }
  90. }