town/cmd/help/main.go

79 lines
1.7 KiB
Go

package main
import (
"fmt"
"os"
"strconv"
"github.com/mattn/go-tty"
)
func numberPrompt(tty *tty.TTY, prompt string, opts []string) (int, error) {
fmt.Println()
fmt.Println(prompt)
fmt.Println("(pick an option using the corresponding number)")
chosen := -1
for chosen < 0 {
fmt.Println()
for ix, o := range opts {
fmt.Printf("%d: %s\n", ix+1, o)
}
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
}
return chosen, nil
}
func _main() error {
fmt.Println("Hi, you have reached the tilde town help desk.")
fmt.Println()
fmt.Println("Please check out the options below. If none of them apply to you, you can send an e-mail to root@tilde.town .")
tty, err := tty.Open()
if err != nil {
return fmt.Errorf("could not open tty: %w", err)
}
defer tty.Close()
options := []string{
"I lost access to my tilde.town account and want to upload a new SSH key",
"I have a code from my e-mail to redeem",
"I just want outta here",
}
c, err := numberPrompt(tty, "What do you need help with?", options)
if c == 2 {
fmt.Println()
fmt.Println("bye~")
return nil
}
// TODO collect email
return nil
}
func main() {
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)
}
}