1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 02:36:24 -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
import (
"errors"
"flag"
"fmt"
"os"
@ -14,6 +15,10 @@ import (
"golang.org/x/term"
)
var (
errAborted = fmt.Errorf("aborted")
)
func getPassword() (string, error) {
var password string
fd := int(os.Stdin.Fd())
@ -41,8 +46,7 @@ func getPassword() (string, error) {
}
} else if input[0] == 3 {
term.Restore(fd, state)
fmt.Println("\nAborted.")
os.Exit(1)
return "", errAborted
} else {
password += string(input[0])
fmt.Print("*")
@ -124,7 +128,11 @@ func parseCommandLine() {
fmt.Print("Enter password for new staff account: ")
password, err = getPassword()
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)
}
if password == "" {
@ -134,7 +142,11 @@ func parseCommandLine() {
fmt.Print("Confirm password: ")
confirm, err := getPassword()
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)
}
if password != confirm {