chore(auth): replace go-input with survey

This commit is contained in:
iyear 2022-11-29 10:36:00 +08:00
parent f862bec6a7
commit 3027d60f63
3 changed files with 22 additions and 20 deletions

View File

@ -3,7 +3,7 @@ package login
import (
"context"
"errors"
"github.com/dpastoor/go-input"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
@ -27,12 +27,13 @@ type termAuth struct {
}
func (a termAuth) Phone(_ context.Context) (string, error) {
phone, err := input.DefaultUI().Ask(color.BlueString("Enter your phone number:"), &input.Options{
Default: color.CyanString("+86 12345678900"),
Loop: true,
Required: true,
})
if err != nil {
phone := ""
prompt := &survey.Input{
Message: "Enter your phone number:",
Default: "+86 12345678900",
}
if err := survey.AskOne(prompt, &phone, survey.WithValidator(survey.Required)); err != nil {
return "", err
}
@ -41,23 +42,27 @@ func (a termAuth) Phone(_ context.Context) (string, error) {
}
func (a termAuth) Password(_ context.Context) (string, error) {
pwd, err := input.DefaultUI().Ask(color.BlueString("Enter 2FA Password:"), &input.Options{
Required: true,
Loop: true,
})
if err != nil {
pwd := ""
prompt := &survey.Input{
Message: "Enter 2FA Password:",
}
if err := survey.AskOne(prompt, &pwd, survey.WithValidator(survey.Required)); err != nil {
return "", err
}
return strings.TrimSpace(pwd), nil
}
func (a termAuth) Code(_ context.Context, _ *tg.AuthSentCode) (string, error) {
code, err := input.DefaultUI().Ask(color.BlueString("Enter Code:"), &input.Options{
Required: true,
Loop: true,
})
if err != nil {
code := ""
prompt := &survey.Input{
Message: "Enter Code:",
}
if err := survey.AskOne(prompt, &code, survey.WithValidator(survey.Required)); err != nil {
return "", err
}
return strings.TrimSpace(code), nil
}

1
go.mod
View File

@ -6,7 +6,6 @@ require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/bcicen/jstream v1.0.1
github.com/beevik/ntp v0.3.0
github.com/dpastoor/go-input v0.0.0-20170428152650-f4bfe573b917
github.com/fatih/color v1.13.0
github.com/gabriel-vasile/mimetype v1.4.1
github.com/go-playground/validator/v10 v10.11.0

2
go.sum
View File

@ -67,8 +67,6 @@ github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dpastoor/go-input v0.0.0-20170428152650-f4bfe573b917 h1:NM+xUZ6GjqE9K3jQBxv1LyVqS0WiE5boaFTn02p9Zsg=
github.com/dpastoor/go-input v0.0.0-20170428152650-f4bfe573b917/go.mod h1:ehZc4MjzEkKMHnwQ89CyJdeMs7EPCVlgvgINNql7b4g=
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=