Browse Source

fix(cli): handle terminal credential input errors

Frédéric Guillot 3 weeks ago
parent
commit
9cd7629615
1 changed files with 18 additions and 4 deletions
  1. 18 4
      internal/cli/ask_credentials.go

+ 18 - 4
internal/cli/ask_credentials.go

@@ -23,13 +23,27 @@ func askCredentials() (string, string) {
 	fmt.Print("Enter Username: ")
 
 	reader := bufio.NewReader(os.Stdin)
-	username, _ := reader.ReadString('\n')
+	username, err := reader.ReadString('\n')
+	if err != nil {
+		printErrorAndExit(fmt.Errorf("unable to read username: %w", err))
+	}
 
 	fmt.Print("Enter Password: ")
 
-	state, _ := term.GetState(fd)
-	defer term.Restore(fd, state)
-	bytePassword, _ := term.ReadPassword(fd)
+	state, err := term.GetState(fd)
+	if err != nil {
+		printErrorAndExit(fmt.Errorf("unable to get terminal state: %w", err))
+	}
+	defer func() {
+		if restoreErr := term.Restore(fd, state); restoreErr != nil {
+			printErrorAndExit(fmt.Errorf("unable to restore terminal state: %w", restoreErr))
+		}
+	}()
+
+	bytePassword, err := term.ReadPassword(fd)
+	if err != nil {
+		printErrorAndExit(fmt.Errorf("unable to read password: %w", err))
+	}
 
 	fmt.Print("\n")
 	return strings.TrimSpace(username), strings.TrimSpace(string(bytePassword))