1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 15:06:23 -07:00

Improve password handling by returning an error on abortion and checking in the calling function

This commit is contained in:
Eggbertx 2025-05-08 17:38:22 -07:00
parent 25790fb5d7
commit 4778a631ba

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"errors"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -14,6 +15,10 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
var (
errAborted = fmt.Errorf("aborted")
)
func getPassword() (string, error) { func getPassword() (string, error) {
var password string var password string
fd := int(os.Stdin.Fd()) fd := int(os.Stdin.Fd())
@ -41,8 +46,7 @@ func getPassword() (string, error) {
} }
} else if input[0] == 3 { } else if input[0] == 3 {
term.Restore(fd, state) term.Restore(fd, state)
fmt.Println("\nAborted.") return "", errAborted
os.Exit(1)
} else { } else {
password += string(input[0]) password += string(input[0])
fmt.Print("*") fmt.Print("*")
@ -124,7 +128,11 @@ func parseCommandLine() {
fmt.Print("Enter password for new staff account: ") fmt.Print("Enter password for new staff account: ")
password, err = getPassword() password, err = getPassword()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error getting password:", err) if errors.Is(err, errAborted) {
fmt.Println("Aborted.")
} else {
fmt.Fprintln(os.Stderr, "Error getting password:", err)
}
os.Exit(1) os.Exit(1)
} }
if password == "" { if password == "" {
@ -134,7 +142,11 @@ func parseCommandLine() {
fmt.Print("Confirm password: ") fmt.Print("Confirm password: ")
confirm, err := getPassword() confirm, err := getPassword()
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error getting password confirmation:", err) if errors.Is(err, errAborted) {
fmt.Println("Aborted.")
} else {
fmt.Fprintln(os.Stderr, "Error getting password confirmation:", err)
}
os.Exit(1) os.Exit(1)
} }
if password != confirm { if password != confirm {