ask_credentials.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package cli // import "miniflux.app/v2/internal/cli"
  4. import (
  5. "bufio"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "strings"
  10. "golang.org/x/term"
  11. )
  12. func askCredentials() (string, string) {
  13. fd := int(os.Stdin.Fd())
  14. if !term.IsTerminal(fd) {
  15. printErrorAndExit(errors.New("this is not an interactive terminal, exiting"))
  16. }
  17. fmt.Print("Enter Username: ")
  18. reader := bufio.NewReader(os.Stdin)
  19. username, err := reader.ReadString('\n')
  20. if err != nil {
  21. printErrorAndExit(fmt.Errorf("unable to read username: %w", err))
  22. }
  23. fmt.Print("Enter Password: ")
  24. state, err := term.GetState(fd)
  25. if err != nil {
  26. printErrorAndExit(fmt.Errorf("unable to get terminal state: %w", err))
  27. }
  28. defer func() {
  29. if restoreErr := term.Restore(fd, state); restoreErr != nil {
  30. printErrorAndExit(fmt.Errorf("unable to restore terminal state: %w", restoreErr))
  31. }
  32. }()
  33. bytePassword, err := term.ReadPassword(fd)
  34. if err != nil {
  35. printErrorAndExit(fmt.Errorf("unable to read password: %w", err))
  36. }
  37. fmt.Print("\n")
  38. return strings.TrimSpace(username), strings.TrimSpace(string(bytePassword))
  39. }