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:
parent
25790fb5d7
commit
4778a631ba
1 changed files with 16 additions and 4 deletions
|
@ -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 {
|
||||
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 {
|
||||
if errors.Is(err, errAborted) {
|
||||
fmt.Println("Aborted.")
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "Error getting password confirmation:", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
if password != confirm {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue