run.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package run
  2. import (
  3. "log"
  4. "os"
  5. "path/filepath"
  6. "github.com/ayn2op/discordo/config"
  7. "github.com/ayn2op/discordo/ui"
  8. "github.com/rivo/tview"
  9. )
  10. var (
  11. discordState *State
  12. app = tview.NewApplication()
  13. mainFlex *MainFlex
  14. )
  15. type Cmd struct {
  16. Token string `default:"${token}" help:"The authentication token." short:"t"`
  17. Config string `default:"${configPath}" help:"The path of the configuration file." short:"l" type:"path"`
  18. Log string `default:"${logPath}" help:"The path of the log file." short:"c" type:"path"`
  19. }
  20. func (r *Cmd) Run() error {
  21. err := os.MkdirAll(filepath.Dir(r.Config), os.ModePerm)
  22. if err != nil {
  23. return err
  24. }
  25. err = config.Load(r.Config)
  26. if err != nil {
  27. return err
  28. }
  29. err = os.MkdirAll(filepath.Dir(r.Log), os.ModePerm)
  30. if err != nil {
  31. return err
  32. }
  33. f, err := os.OpenFile(r.Log, os.O_CREATE|os.O_WRONLY, os.ModePerm)
  34. if err != nil {
  35. return err
  36. }
  37. log.SetOutput(f)
  38. log.SetFlags(log.LstdFlags | log.Llongfile)
  39. if r.Token == "" {
  40. lf := ui.NewLoginForm()
  41. go func() {
  42. mainFlex = newMainFlex()
  43. if err := <-lf.Error; err != nil {
  44. app.Stop()
  45. log.Fatal(err)
  46. }
  47. if err := openState(<-lf.Token); err != nil {
  48. app.Stop()
  49. log.Fatal(err)
  50. }
  51. app.QueueUpdateDraw(func() {
  52. app.SetRoot(mainFlex, true)
  53. })
  54. }()
  55. app.SetRoot(lf, true)
  56. } else {
  57. mainFlex = newMainFlex()
  58. if err := openState(r.Token); err != nil {
  59. app.Stop()
  60. }
  61. app.SetRoot(mainFlex, true)
  62. }
  63. app.EnableMouse(config.Current.Mouse)
  64. return app.Run()
  65. }