town/cmd/welcome/main.go

127 lines
2.4 KiB
Go
Raw Normal View History

package main
2023-02-26 08:57:46 +00:00
import (
2023-02-28 19:30:07 +00:00
"errors"
2023-02-26 08:57:46 +00:00
"fmt"
"os"
2023-02-28 05:50:55 +00:00
"strings"
2023-02-26 08:57:46 +00:00
2023-02-28 19:30:07 +00:00
"git.tilde.town/tildetown/town/invites"
2023-02-28 23:18:11 +00:00
"git.tilde.town/tildetown/town/stats"
2023-02-28 05:50:55 +00:00
"github.com/AlecAivazis/survey/v2"
"github.com/charmbracelet/lipgloss"
2023-02-26 08:57:46 +00:00
_ "embed"
)
//go:embed welcome.txt
var welcomeArt string
// TODO move magic key machine to static page
type newUserData struct {
Username string
DisplayName string
Email string
PubKey string
}
2023-02-28 05:50:55 +00:00
func surveyIconSet(icons *survey.IconSet) {
icons.Question.Text = "~"
icons.Question.Format = "magenta:b"
}
2023-02-26 08:57:46 +00:00
2023-02-28 19:30:07 +00:00
func promptCode() (code string, err error) {
codePrompt := &survey.Input{
2023-02-28 05:50:55 +00:00
Message: "invite code?",
}
2023-02-28 19:30:07 +00:00
err = survey.AskOne(codePrompt, &code,
2023-02-28 05:50:55 +00:00
survey.WithValidator(survey.Required),
survey.WithIcons(surveyIconSet))
2023-02-28 19:30:07 +00:00
code = strings.TrimSpace(code)
2023-02-28 05:50:55 +00:00
return
}
2023-02-26 08:57:46 +00:00
2023-02-28 23:18:11 +00:00
func promptUsername(townData stats.TildeData) (un string, err error) {
2023-02-28 19:30:07 +00:00
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)
2023-02-28 23:18:11 +00:00
for _, v := range townData.Users {
if v.Username == un {
return errors.New("that username is already in use, sorry")
}
}
2023-02-28 19:30:07 +00:00
return nil
}))
return "", nil
}
2023-02-28 05:50:55 +00:00
func _main() error {
2023-02-28 23:18:11 +00:00
townData, err := stats.Stats()
if err != nil {
return err
}
2023-02-28 19:30:07 +00:00
inviteDB, err := invites.ConnectDB()
if err != nil {
return err
}
data := &newUserData{}
2023-02-28 05:50:55 +00:00
s := lipgloss.NewStyle().
Foreground(lipgloss.AdaptiveColor{
Light: "#7D19BD",
Dark: "#E0B0FF",
})
2023-02-26 08:57:46 +00:00
2023-02-28 05:50:55 +00:00
s = s.SetString(welcomeArt)
fmt.Println(s)
2023-02-26 08:57:46 +00:00
2023-02-28 19:30:07 +00:00
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)
2023-02-28 23:18:11 +00:00
data.Username, err = promptUsername(townData)
2023-02-28 05:50:55 +00:00
if err != nil {
return err
}
2023-02-26 08:57:46 +00:00
2023-02-28 05:50:55 +00:00
// TODO collect username
// TODO collect email
// TODO collect public key
// TODO have enough to make account; can now do that
// TODO assuming account creation succeeded, mark invite as used
2023-02-26 08:57:46 +00:00
2023-02-28 05:50:55 +00:00
return nil
2023-02-26 08:57:46 +00:00
}
func main() {
2023-02-28 19:30:07 +00:00
// TODO friendlier error handling
2023-02-26 08:57:46 +00:00
err := _main()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}