main.go 2.0 KB

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