Compare commits

..

No commits in common. "5c2142f6e794b2d12fdfd31dfaecd9fc3a36b45b" and "6950ba7109df1a6344c88cb8e1105cd1a5a565a6" have entirely different histories.

2 changed files with 17 additions and 84 deletions

View File

@ -16,8 +16,6 @@ import (
// TODO consider local-only help command for renaming and adding emails to account
// TODO put colorscheme, prompting stuff into own packages for use in the other commands. would be good to get off of survey.
type colorScheme struct {
Header func(string) string
Subtitle func(string) string
@ -43,23 +41,11 @@ func newColorScheme() colorScheme {
}
}
type Prompter struct {
cs colorScheme
tty *tty.TTY
}
func NewPrompter(tty *tty.TTY, cs colorScheme) *Prompter {
return &Prompter{
cs: cs,
tty: tty,
}
}
func (p *Prompter) String(prompt string) (string, error) {
func stringPrompt(cs colorScheme, tty *tty.TTY, prompt string) (string, error) {
fmt.Println("")
fmt.Println(p.cs.Prompt(prompt))
fmt.Println(p.cs.Subtitle("(type your answer below and press enter to submit)"))
s, err := p.tty.ReadString()
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)
}
@ -67,18 +53,18 @@ func (p *Prompter) String(prompt string) (string, error) {
return s, nil
}
func (p *Prompter) Select(prompt string, opts []string) (int, error) {
func numberPrompt(cs colorScheme, tty *tty.TTY, prompt string, opts []string) (int, error) {
fmt.Println()
fmt.Println(p.cs.Prompt(prompt))
fmt.Println(p.cs.Subtitle("(pick an option using the corresponding number)"))
fmt.Println(cs.Prompt(prompt))
fmt.Println(cs.Subtitle("(pick an option using the corresponding number)"))
chosen := -1
for chosen < 0 {
fmt.Println()
for ix, o := range opts {
fmt.Printf("%s: %s\n", p.cs.Option(fmt.Sprintf("%d", ix+1)), o)
fmt.Printf("%s: %s\n", cs.Option(fmt.Sprintf("%d", ix+1)), o)
}
r, err := p.tty.ReadRune()
r, err := tty.ReadRune()
if err != nil {
return -1, fmt.Errorf("could not collect answer for '%s': %w", prompt, err)
}
@ -118,14 +104,12 @@ func _main() error {
}
defer tty.Close()
p := NewPrompter(tty, cs)
options := []string{
"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",
}
c, err := p.Select("What do you need help with?", options)
c, err := numberPrompt(cs, tty, "What do you need help with?", options)
defer func() {
fmt.Println()
@ -134,9 +118,9 @@ func _main() error {
switch c {
case 0:
return collectEmail(db, cs, p)
return collectEmail(db, cs, tty)
case 1:
return redeemCode(db, cs, p)
return redeemCode(tty)
case 2:
return nil
}
@ -144,9 +128,9 @@ func _main() error {
return nil
}
func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
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."))
email, err := p.String("email to send reset code to?")
email, err := stringPrompt(cs, tty, "email to send reset code to?")
if err != nil {
return err
}
@ -201,25 +185,8 @@ func collectEmail(db *sql.DB, cs colorScheme, p *Prompter) error {
return nil
}
func redeemCode(db *sql.DB, cs colorScheme, p *Prompter) error {
fmt.Println(cs.Header("redeem an auth code and add a new public key"))
fmt.Println()
c, err := p.String("paste your auth code:")
if err != nil {
// TODO log
return err
}
parts, err := codes.Decode(c)
if err != nil {
// TODO log
return err
}
code := parts[0]
email := parts[1]
fmt.Println(code, email)
func redeemCode(tty *tty.TTY) error {
// TODO deserialize
// TODO verify code
// TODO accept key
// TODO verify key

View File

@ -153,7 +153,6 @@ func UserForEmail(db *sql.DB, address string) (*TownUser, error) {
if err != nil {
return nil, err
}
defer stmt.Close()
row := stmt.QueryRow(address)
u := &TownUser{}
if err = row.Scan(&u.ID, &u.Username); err != nil {
@ -184,41 +183,8 @@ type AuthCode struct {
}
func (c *AuthCode) Insert(db *sql.DB) error {
stmt, err := db.Prepare(`
INSERT INTO auth_codes (code, email)
VALUES ?, ?`)
if err != nil {
return err
}
defer stmt.Close()
result, err := stmt.Exec(c.Code, c.Email)
if err != nil {
return err
}
liid, err := result.LastInsertId()
if err != nil {
return err
}
c.ID = liid
// TODO
return nil
}
func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(`
SELECT id, email, used, created
FROM auth_codes
WHERE code = ?`)
if err != nil {
return err
}
defer stmt.Close()
return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Email, &c.Used, &c.Created)
}
// TODO other auth code as needed