WIP username input

trunk
vilmibm 2023-02-28 19:30:07 +00:00
parent 128b53edbe
commit 5a41d99ff9
2 changed files with 57 additions and 12 deletions

View File

@ -1,13 +1,12 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"strings" "strings"
//"github.com/gdamore/tcell/v2" "git.tilde.town/tildetown/town/invites"
//"github.com/rivo/tview"
"github.com/AlecAivazis/survey/v2" "github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
@ -31,18 +30,43 @@ func surveyIconSet(icons *survey.IconSet) {
icons.Question.Format = "magenta:b" icons.Question.Format = "magenta:b"
} }
func promptInvite() (invite string, err error) { func promptCode() (code string, err error) {
invitePrompt := &survey.Input{ codePrompt := &survey.Input{
Message: "invite code?", Message: "invite code?",
} }
err = survey.AskOne(invitePrompt, &invite, err = survey.AskOne(codePrompt, &code,
survey.WithValidator(survey.Required), survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet)) survey.WithIcons(surveyIconSet))
invite = strings.TrimSpace(invite) code = strings.TrimSpace(code)
return return
} }
func promptUsername() (un string, err error) {
unPrompt := &survey.Input{
Message: "desired username?",
}
err = survey.AskOne(unPrompt, &un,
survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet),
survey.WithValidator(func(val interface{}) error {
un := val.(string)
// TODO check for exising username
fmt.Println(un)
return nil
}))
return "", nil
}
func _main() error { func _main() error {
inviteDB, err := invites.ConnectDB()
if err != nil {
return err
}
data := &newUserData{}
s := lipgloss.NewStyle(). s := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{ Foreground(lipgloss.AdaptiveColor{
Light: "#7D19BD", Light: "#7D19BD",
@ -52,13 +76,29 @@ func _main() error {
s = s.SetString(welcomeArt) s = s.SetString(welcomeArt)
fmt.Println(s) fmt.Println(s)
invite, err := promptInvite() code, err := promptCode()
if err != nil {
return err
}
invite, err := invites.Get(inviteDB, code)
if err != nil {
return fmt.Errorf("could not look up invite code: %w", err)
}
if invite.Used {
return errors.New("that invite code has already been used.")
}
s = s.SetString("thanks!! just gotta collect some information now and then your account will be ready.")
fmt.Println(s)
data.Username, err = promptUsername()
if err != nil { if err != nil {
return err return err
} }
fmt.Println("got " + invite)
// TODO check if token is valid
// TODO collect username // TODO collect username
// TODO collect email // TODO collect email
// TODO collect public key // TODO collect public key
@ -69,6 +109,7 @@ func _main() error {
} }
func main() { func main() {
// TODO friendlier error handling
err := _main() err := _main()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)

View File

@ -86,7 +86,7 @@ func Get(db *sql.DB, code string) (*Invite, error) {
inv := &Invite{ inv := &Invite{
Code: code, Code: code,
} }
var created int64 var created string
var used int var used int
stmt, err := db.Prepare(` stmt, err := db.Prepare(`
SELECT id, created, email, used SELECT id, created, email, used
@ -111,7 +111,11 @@ func Get(db *sql.DB, code string) (*Invite, error) {
return nil, err return nil, err
} }
inv.Created = time.Unix(created, 0) inv.Created, err = time.Parse("2006-01-02T15:04", created)
if err != nil {
return inv, err
}
inv.Used = used > 0 inv.Used = used > 0
return inv, nil return inv, nil