refactor: callback instead of chan
This commit is contained in:
parent
edff245ff8
commit
bda8419b4b
2 changed files with 41 additions and 38 deletions
|
|
@ -1,10 +1,8 @@
|
|||
package ui
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/ayn2op/discordo/internal/config"
|
||||
"github.com/ayn2op/discordo/internal/constants"
|
||||
"github.com/diamondburned/arikawa/v3/api"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
|
|
@ -12,27 +10,24 @@ import (
|
|||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
Value string
|
||||
Error error
|
||||
}
|
||||
type DoneFn func(token string, err error)
|
||||
|
||||
type LoginForm struct {
|
||||
*tview.Form
|
||||
Token chan Token
|
||||
done DoneFn
|
||||
}
|
||||
|
||||
func NewLoginForm(cfg *config.Config) *LoginForm {
|
||||
func NewLoginForm(done DoneFn) *LoginForm {
|
||||
lf := &LoginForm{
|
||||
Form: tview.NewForm(),
|
||||
Token: make(chan Token, 1),
|
||||
Form: tview.NewForm(),
|
||||
done: done,
|
||||
}
|
||||
|
||||
lf.AddInputField("Email", "", 0, nil, nil)
|
||||
lf.AddPasswordField("Password", "", 0, 0, nil)
|
||||
lf.AddPasswordField("Code (optional)", "", 0, 0, nil)
|
||||
lf.AddCheckbox("Remember Me", true, nil)
|
||||
lf.AddButton("Login", lf.onLoginButtonSelected)
|
||||
lf.AddButton("Login", lf.login)
|
||||
|
||||
lf.SetTitle("Login")
|
||||
lf.SetTitleColor(tcell.GetColor(cfg.Theme.TitleColor))
|
||||
|
|
@ -46,7 +41,7 @@ func NewLoginForm(cfg *config.Config) *LoginForm {
|
|||
return lf
|
||||
}
|
||||
|
||||
func (lf *LoginForm) onLoginButtonSelected() {
|
||||
func (lf *LoginForm) login() {
|
||||
email := lf.GetFormItem(0).(*tview.InputField).GetText()
|
||||
password := lf.GetFormItem(1).(*tview.InputField).GetText()
|
||||
if email == "" || password == "" {
|
||||
|
|
@ -58,7 +53,10 @@ func (lf *LoginForm) onLoginButtonSelected() {
|
|||
// Log in using the provided email and password.
|
||||
lr, err := apiClient.Login(email, password)
|
||||
if err != nil {
|
||||
lf.Token <- Token{Error: err}
|
||||
if lf.done != nil {
|
||||
lf.done("", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -66,19 +64,28 @@ func (lf *LoginForm) onLoginButtonSelected() {
|
|||
if lr.MFA && lr.Token == "" {
|
||||
code := lf.GetFormItem(2).(*tview.InputField).GetText()
|
||||
if code == "" {
|
||||
lf.Token <- Token{Error: errors.New("code required")}
|
||||
if lf.done != nil {
|
||||
lf.done("", errors.New("code required"))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
lr, err = apiClient.TOTP(code, lr.Ticket)
|
||||
if err != nil {
|
||||
lf.Token <- Token{Error: err}
|
||||
if lf.done != nil {
|
||||
lf.done("", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if lr.Token == "" {
|
||||
lf.Token <- Token{Error: errors.New("missing token")}
|
||||
if lf.done != nil {
|
||||
lf.done("", errors.New("missing token"))
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -86,10 +93,14 @@ func (lf *LoginForm) onLoginButtonSelected() {
|
|||
if rememberMe {
|
||||
go func() {
|
||||
if err := keyring.Set(constants.Name, "token", lr.Token); err != nil {
|
||||
log.Println(err)
|
||||
if lf.done != nil {
|
||||
lf.done("", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
lf.Token <- Token{Value: lr.Token}
|
||||
if lf.done != nil {
|
||||
lf.done(lr.Token, nil)
|
||||
}
|
||||
}
|
||||
30
cmd/run.go
30
cmd/run.go
|
|
@ -5,7 +5,6 @@ import (
|
|||
|
||||
"github.com/ayn2op/discordo/internal/config"
|
||||
"github.com/ayn2op/discordo/internal/logger"
|
||||
"github.com/ayn2op/discordo/internal/ui"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
|
|
@ -28,32 +27,25 @@ func Run(token string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// mainFlex must be initialized before opening a new state.
|
||||
mainFlex = newMainFlex()
|
||||
if token == "" {
|
||||
lf := ui.NewLoginForm(cfg)
|
||||
|
||||
go func() {
|
||||
// mainFlex must be initialized before opening a new state.
|
||||
mainFlex = newMainFlex()
|
||||
|
||||
token := <-lf.Token
|
||||
if token.Error != nil {
|
||||
app.Stop()
|
||||
log.Fatal(token.Error)
|
||||
}
|
||||
|
||||
if err := openState(token.Value); err != nil {
|
||||
lf := NewLoginForm(func(token string, err error) {
|
||||
if err != nil {
|
||||
app.Stop()
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
app.QueueUpdateDraw(func() {
|
||||
app.SetRoot(mainFlex, true)
|
||||
})
|
||||
}()
|
||||
if err := openState(token); err != nil {
|
||||
app.Stop()
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
app.SetRoot(mainFlex, true)
|
||||
})
|
||||
|
||||
app.SetRoot(lf, true)
|
||||
} else {
|
||||
mainFlex = newMainFlex()
|
||||
if err := openState(token); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue