town/cmd/help/main.go

203 lines
4.5 KiB
Go
Raw Normal View History

2023-06-07 07:12:32 +00:00
package main
2023-10-18 00:48:37 +00:00
import (
2023-10-24 05:15:04 +00:00
"database/sql"
2023-10-18 00:48:37 +00:00
"fmt"
"os"
"strconv"
2023-10-24 05:15:04 +00:00
"strings"
2023-10-18 00:48:37 +00:00
2023-10-24 06:22:21 +00:00
"git.tilde.town/tildetown/town/codes"
2023-10-24 05:15:04 +00:00
"git.tilde.town/tildetown/town/towndb"
2023-10-21 04:58:30 +00:00
"github.com/charmbracelet/lipgloss"
2023-10-24 05:15:04 +00:00
_ "github.com/mattn/go-sqlite3"
2023-10-18 00:48:37 +00:00
"github.com/mattn/go-tty"
)
2023-10-21 04:58:30 +00:00
// TODO consider local-only help command for renaming and adding emails to account
type colorScheme struct {
Header func(string) string
Subtitle func(string) string
Prompt func(string) string
Email func(string) string
Option func(string) string
}
func newColorScheme() colorScheme {
s2r := func(s lipgloss.Style) func(string) string {
return s.Render
}
c := func(s string) lipgloss.Color {
return lipgloss.Color(s)
}
s := lipgloss.NewStyle
return colorScheme{
Header: s2r(s().Bold(true).Foreground(c("#E0B0FF"))),
Subtitle: s2r(s().Italic(true).Foreground(c("gray"))),
Email: s2r(s().Bold(true).Underline(true)),
Prompt: s2r(s().Bold(true).Foreground(c("#00752d"))),
Option: s2r(s().Bold(true).Foreground(c("#38747a"))),
}
}
func stringPrompt(cs colorScheme, tty *tty.TTY, prompt string) (string, error) {
fmt.Println("")
fmt.Println(cs.Prompt(prompt))
fmt.Println(cs.Subtitle("(type your answer below and press enter to submit)"))
s, err := tty.ReadString()
if err != nil {
return "", fmt.Errorf("couldn't collect input: %w", err)
}
return s, nil
}
func numberPrompt(cs colorScheme, tty *tty.TTY, prompt string, opts []string) (int, error) {
2023-10-18 00:48:37 +00:00
fmt.Println()
2023-10-21 04:58:30 +00:00
fmt.Println(cs.Prompt(prompt))
fmt.Println(cs.Subtitle("(pick an option using the corresponding number)"))
2023-10-18 00:48:37 +00:00
chosen := -1
for chosen < 0 {
fmt.Println()
for ix, o := range opts {
2023-10-21 04:58:30 +00:00
fmt.Printf("%s: %s\n", cs.Option(fmt.Sprintf("%d", ix+1)), o)
2023-10-18 00:48:37 +00:00
}
r, err := tty.ReadRune()
if err != nil {
return -1, fmt.Errorf("could not collect answer for '%s': %w", prompt, err)
}
c, err := strconv.Atoi(string(r))
if err != nil {
fmt.Println()
fmt.Printf("I could not understand '%s'. Try again, please.\n", string(r))
continue
}
if c > len(opts) || c == 0 {
fmt.Println()
fmt.Printf("%s is not an option. Try again, please.\n", string(r))
continue
}
chosen = c - 1
}
2023-10-21 04:58:30 +00:00
fmt.Println("")
2023-10-18 00:48:37 +00:00
return chosen, nil
}
func _main() error {
2023-10-24 05:15:04 +00:00
db, err := towndb.ConnectDB()
if err != nil {
return fmt.Errorf("could not connect to database. please let root@tilde.town know about this.")
}
2023-10-21 04:58:30 +00:00
cs := newColorScheme()
fmt.Println(cs.Header("Hi, you have reached the tilde town help desk."))
2023-10-18 00:48:37 +00:00
fmt.Println()
2023-10-21 04:58:30 +00:00
fmt.Println("Please check out the options below.")
fmt.Printf("If none of them apply to your case, you can email %s. \n", cs.Email("root@tilde.town"))
2023-10-18 00:48:37 +00:00
tty, err := tty.Open()
if err != nil {
return fmt.Errorf("could not open tty: %w", err)
}
defer tty.Close()
options := []string{
2023-10-21 04:58:30 +00:00
"I need to request that a new SSH key be added to my account.",
"I have a code from my e-mail to redeem for a new SSH key",
"I just want out of here",
2023-10-18 00:48:37 +00:00
}
2023-10-21 04:58:30 +00:00
c, err := numberPrompt(cs, tty, "What do you need help with?", options)
2023-10-18 00:48:37 +00:00
2023-10-24 05:15:04 +00:00
defer func() {
fmt.Println()
fmt.Println(cs.Header("bye~"))
}()
2023-10-21 04:58:30 +00:00
switch c {
case 0:
2023-10-24 05:15:04 +00:00
return collectEmail(db, cs, tty)
2023-10-21 04:58:30 +00:00
case 1:
return redeemCode(tty)
case 2:
2023-10-18 00:48:37 +00:00
return nil
}
2023-10-21 04:58:30 +00:00
return nil
}
2023-10-24 05:15:04 +00:00
func collectEmail(db *sql.DB, cs colorScheme, tty *tty.TTY) error {
fmt.Println(cs.Header("We can send a authorization code to an email associated with your town account."))
2023-10-21 04:58:30 +00:00
email, err := stringPrompt(cs, tty, "email to send reset code to?")
if err != nil {
return err
}
2023-10-24 05:15:04 +00:00
fmt.Println()
fmt.Println(cs.Header("thanks!"))
fmt.Println()
fmt.Printf("If %s is associated with a town account we'll email an authorization code.\n", cs.Email(email))
mustHave := []string{"@", "."}
found := 0
for _, s := range mustHave {
if strings.Contains(email, s) {
found++
}
}
if found != len(mustHave) {
// TODO log
return nil
}
user, err := towndb.UserForEmail(db, email)
if err != nil {
// TODO log
return err
}
if user == nil {
// TODO log
return nil
}
2023-10-24 06:22:21 +00:00
code := codes.NewCode(email)
fmt.Println(code)
ac := &towndb.AuthCode{
Code: code,
Email: email,
}
err = ac.Insert(db)
if err != nil {
// TODO log
return err
}
2023-10-24 05:15:04 +00:00
2023-10-21 04:58:30 +00:00
// TODO send email
return nil
}
2023-10-18 00:48:37 +00:00
2023-10-21 04:58:30 +00:00
func redeemCode(tty *tty.TTY) error {
// TODO deserialize
// TODO verify code
// TODO accept key
// TODO verify key
// TODO add key to .authorized_keys2
// TODO mark used
2023-10-18 00:48:37 +00:00
return nil
}
2023-06-07 07:12:32 +00:00
func main() {
2023-10-18 00:48:37 +00:00
err := _main()
if err != nil {
fmt.Printf("sorry, something went wrong: %s\n", err.Error())
fmt.Println("Please let an admin know by emailing a copy of this error to root@tilde.town")
os.Exit(1)
}
2023-06-07 07:12:32 +00:00
}