From 90808c1ce0aa098a15e27d3a80ee5043435d69e3 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 25 Oct 2023 19:24:01 +0000 Subject: [PATCH] bug fixes, ready for external qa --- cmd/appendkeyfile/main.go | 9 -------- cmd/emailtouser/main.go | 2 +- cmd/help/main.go | 43 +++++++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/cmd/appendkeyfile/main.go b/cmd/appendkeyfile/main.go index 84c7db3..5016a64 100644 --- a/cmd/appendkeyfile/main.go +++ b/cmd/appendkeyfile/main.go @@ -24,20 +24,11 @@ func quit(msg string, code int) { } func main() { - username := os.Args[1] - if username == "" { - quit("expected username as argument", 1) - } - u, err := user.Current() if err != nil { quit(err.Error(), 2) } - if u.Username != username { - quit("that's my purse; I don't know you", 3) - } - sshPath := path.Join("/home", u.Username, ".ssh") keyfilePath := path.Join(sshPath, keyfileName) diff --git a/cmd/emailtouser/main.go b/cmd/emailtouser/main.go index f1b17ae..bb44718 100644 --- a/cmd/emailtouser/main.go +++ b/cmd/emailtouser/main.go @@ -27,7 +27,7 @@ func _main(args []string) error { return errors.New("email does not correspond to user") } - fmt.Println(user.Username) + fmt.Print(user.Username) return nil } diff --git a/cmd/help/main.go b/cmd/help/main.go index 7eead1c..ad81b82 100644 --- a/cmd/help/main.go +++ b/cmd/help/main.go @@ -74,7 +74,7 @@ func NewPrompter(tty *tty.TTY, cs colorScheme) *Prompter { func (p *Prompter) String(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)")) + fmt.Println(p.cs.Subtitle("(press enter to submit)")) s, err := p.tty.ReadString() if err != nil { return "", fmt.Errorf("couldn't collect input: %w", err) @@ -148,8 +148,6 @@ func _main(cs colorScheme) error { c, err := p.Select("What do you need help with?", options) defer func() { - fmt.Println() - fmt.Println(cs.Header("bye~")) }() switch c { @@ -174,7 +172,7 @@ func emailToUsername(email string) (string, error) { return "", fmt.Errorf("emailtouser failed with '%s': %w", stderrBuff.String(), err) } - return stdoutBuff.String(), nil + return strings.TrimSpace(stdoutBuff.String()), nil } func collectEmail(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { @@ -228,7 +226,7 @@ func collectEmail(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { fmt.Println(cs.Header("redeem an auth code and add a new public key")) - c, err := p.String("paste your auth code and hit enter to submit:") + c, err := p.String("paste your auth code:") if err != nil { l.Printf("failed to prompt: %s", err.Error()) fmt.Println(cs.Error("sorry, I couldn't read that.")) @@ -243,7 +241,7 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { } code := &AuthCode{ - Code: parts[0], + Code: c, Email: parts[1], } @@ -265,7 +263,10 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { return nil } - key, err := p.String("paste your new public key and hit enter to submit:") + fmt.Println() + fmt.Printf("hi, ~%s", username) + + key, err := p.String("paste your new public key:") if err != nil { l.Printf("failed to prompt: %s", err.Error()) fmt.Println(cs.Error("sorry, I couldn't read that.")) @@ -283,12 +284,17 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { return nil } - cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username) - cmd.Stdin = bytes.NewBufferString(key) + // TODO + // this works: sudo --user help sudo --user wren /town/bin/appendkeyfile < /tmp/foo.pub + // but it's failing with nothing on STDOUT and an exit code of 1 when invoked this way + cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile") + cmd.Stdin = bytes.NewBufferString(key + "\n") stdoutBuff := bytes.NewBuffer([]byte{}) cmd.Stdout = stdoutBuff + stderrBuff := bytes.NewBuffer([]byte{}) + cmd.Stderr = stderrBuff if err = cmd.Run(); err != nil { - l.Printf("appendkeyfile failed with '%s': %s", stdoutBuff.String(), err.Error()) + l.Printf("appendkeyfile failed with '%s', '%s': %s", stderrBuff.String(), stdoutBuff.String(), err.Error()) return errors.New("adding to keys file failed") } @@ -304,6 +310,10 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error { func main() { cs := newColorScheme() err := _main(cs) + defer func() { + fmt.Println() + fmt.Println(cs.Header("bye~")) + }() if err != nil { fmt.Println( cs.Error(fmt.Sprintf("sorry, something went wrong: %s", err.Error()))) @@ -313,11 +323,10 @@ func main() { } type AuthCode struct { - ID int64 - Code string - Email string - Used bool - Created time.Time + ID int64 + Code string + Email string + Used bool } func (c *AuthCode) Insert(db *sql.DB) error { @@ -347,7 +356,7 @@ func (c *AuthCode) Insert(db *sql.DB) error { func (c *AuthCode) Hydrate(db *sql.DB) error { stmt, err := db.Prepare(` - SELECT id, used, created + SELECT id, used FROM auth_codes WHERE code = ? AND email = ?`) if err != nil { @@ -355,7 +364,7 @@ func (c *AuthCode) Hydrate(db *sql.DB) error { } defer stmt.Close() - return stmt.QueryRow(c.Code).Scan(&c.ID, &c.Used, &c.Created) + return stmt.QueryRow(c.Code, c.Email).Scan(&c.ID, &c.Used) } func (c *AuthCode) MarkUsed(db *sql.DB) error {