diff --git a/cmd/welcome/main.go b/cmd/welcome/main.go index 6f10f0e..2bf426d 100644 --- a/cmd/welcome/main.go +++ b/cmd/welcome/main.go @@ -1,13 +1,12 @@ package main import ( + "errors" "fmt" "os" "strings" - //"github.com/gdamore/tcell/v2" - //"github.com/rivo/tview" - + "git.tilde.town/tildetown/town/invites" "github.com/AlecAivazis/survey/v2" "github.com/charmbracelet/lipgloss" @@ -31,18 +30,43 @@ func surveyIconSet(icons *survey.IconSet) { icons.Question.Format = "magenta:b" } -func promptInvite() (invite string, err error) { - invitePrompt := &survey.Input{ +func promptCode() (code string, err error) { + codePrompt := &survey.Input{ Message: "invite code?", } - err = survey.AskOne(invitePrompt, &invite, + err = survey.AskOne(codePrompt, &code, survey.WithValidator(survey.Required), survey.WithIcons(surveyIconSet)) - invite = strings.TrimSpace(invite) + code = strings.TrimSpace(code) 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 { + inviteDB, err := invites.ConnectDB() + if err != nil { + return err + } + + data := &newUserData{} + s := lipgloss.NewStyle(). Foreground(lipgloss.AdaptiveColor{ Light: "#7D19BD", @@ -52,13 +76,29 @@ func _main() error { s = s.SetString(welcomeArt) 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 { return err } - fmt.Println("got " + invite) - // TODO check if token is valid // TODO collect username // TODO collect email // TODO collect public key @@ -69,6 +109,7 @@ func _main() error { } func main() { + // TODO friendlier error handling err := _main() if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/invites/invites.go b/invites/invites.go index 5ca0b63..f665ab7 100644 --- a/invites/invites.go +++ b/invites/invites.go @@ -86,7 +86,7 @@ func Get(db *sql.DB, code string) (*Invite, error) { inv := &Invite{ Code: code, } - var created int64 + var created string var used int stmt, err := db.Prepare(` SELECT id, created, email, used @@ -111,7 +111,11 @@ func Get(db *sql.DB, code string) (*Invite, error) { 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 return inv, nil