bug fixes, ready for external qa

vilmibm 2023-10-25 19:24:01 +00:00
parent 17d39483fb
commit 90808c1ce0
3 changed files with 27 additions and 27 deletions

View File

@ -24,20 +24,11 @@ func quit(msg string, code int) {
} }
func main() { func main() {
username := os.Args[1]
if username == "" {
quit("expected username as argument", 1)
}
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
quit(err.Error(), 2) 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") sshPath := path.Join("/home", u.Username, ".ssh")
keyfilePath := path.Join(sshPath, keyfileName) keyfilePath := path.Join(sshPath, keyfileName)

View File

@ -27,7 +27,7 @@ func _main(args []string) error {
return errors.New("email does not correspond to user") return errors.New("email does not correspond to user")
} }
fmt.Println(user.Username) fmt.Print(user.Username)
return nil return nil
} }

View File

@ -74,7 +74,7 @@ func NewPrompter(tty *tty.TTY, cs colorScheme) *Prompter {
func (p *Prompter) String(prompt string) (string, error) { func (p *Prompter) String(prompt string) (string, error) {
fmt.Println("") fmt.Println("")
fmt.Println(p.cs.Prompt(prompt)) 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() s, err := p.tty.ReadString()
if err != nil { if err != nil {
return "", fmt.Errorf("couldn't collect input: %w", err) 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) c, err := p.Select("What do you need help with?", options)
defer func() { defer func() {
fmt.Println()
fmt.Println(cs.Header("bye~"))
}() }()
switch c { switch c {
@ -174,7 +172,7 @@ func emailToUsername(email string) (string, error) {
return "", fmt.Errorf("emailtouser failed with '%s': %w", stderrBuff.String(), err) 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 { 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 { 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")) 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 { if err != nil {
l.Printf("failed to prompt: %s", err.Error()) l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that.")) 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 := &AuthCode{
Code: parts[0], Code: c,
Email: parts[1], Email: parts[1],
} }
@ -265,7 +263,10 @@ func redeemCode(l *log.Logger, db *sql.DB, cs colorScheme, p *Prompter) error {
return nil 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 { if err != nil {
l.Printf("failed to prompt: %s", err.Error()) l.Printf("failed to prompt: %s", err.Error())
fmt.Println(cs.Error("sorry, I couldn't read that.")) 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 return nil
} }
cmd := exec.Command("sudo", "--user", username, "/town/bin/appendkeyfile", username) // TODO
cmd.Stdin = bytes.NewBufferString(key) // 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{}) stdoutBuff := bytes.NewBuffer([]byte{})
cmd.Stdout = stdoutBuff cmd.Stdout = stdoutBuff
stderrBuff := bytes.NewBuffer([]byte{})
cmd.Stderr = stderrBuff
if err = cmd.Run(); err != nil { 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") 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() { func main() {
cs := newColorScheme() cs := newColorScheme()
err := _main(cs) err := _main(cs)
defer func() {
fmt.Println()
fmt.Println(cs.Header("bye~"))
}()
if err != nil { if err != nil {
fmt.Println( fmt.Println(
cs.Error(fmt.Sprintf("sorry, something went wrong: %s", err.Error()))) cs.Error(fmt.Sprintf("sorry, something went wrong: %s", err.Error())))
@ -313,11 +323,10 @@ func main() {
} }
type AuthCode struct { type AuthCode struct {
ID int64 ID int64
Code string Code string
Email string Email string
Used bool Used bool
Created time.Time
} }
func (c *AuthCode) Insert(db *sql.DB) error { 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 { func (c *AuthCode) Hydrate(db *sql.DB) error {
stmt, err := db.Prepare(` stmt, err := db.Prepare(`
SELECT id, used, created SELECT id, used
FROM auth_codes FROM auth_codes
WHERE code = ? AND email = ?`) WHERE code = ? AND email = ?`)
if err != nil { if err != nil {
@ -355,7 +364,7 @@ func (c *AuthCode) Hydrate(db *sql.DB) error {
} }
defer stmt.Close() 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 { func (c *AuthCode) MarkUsed(db *sql.DB) error {