PromptPassword is a specialized text input that doesn't display the characters entered.
(prompt, name string, validators ...PromptValidator)
| 91 | |
| 92 | // PromptPassword is a specialized text input that doesn't display the characters entered. |
| 93 | func PromptPassword(prompt, name string, validators ...PromptValidator) (string, error) { |
| 94 | termState, err := terminal.GetState(int(syscall.Stdin)) |
| 95 | if err != nil { |
| 96 | return "", err |
| 97 | } |
| 98 | |
| 99 | cancel := interrupt.RegisterCleaner(func() error { |
| 100 | return terminal.Restore(int(syscall.Stdin), termState) |
| 101 | }) |
| 102 | defer cancel() |
| 103 | |
| 104 | loop: |
| 105 | for { |
| 106 | _, _ = fmt.Fprintf(os.Stderr, "%s: ", prompt) |
| 107 | |
| 108 | bytePassword, err := terminal.ReadPassword(int(syscall.Stdin)) |
| 109 | // new line for coherent formatting, ReadPassword clip the normal new line |
| 110 | // entered by the user |
| 111 | fmt.Println() |
| 112 | |
| 113 | if err != nil { |
| 114 | return "", err |
| 115 | } |
| 116 | |
| 117 | pass := string(bytePassword) |
| 118 | |
| 119 | for _, validator := range validators { |
| 120 | complaint, err := validator(name, pass) |
| 121 | if err != nil { |
| 122 | return "", err |
| 123 | } |
| 124 | if complaint != "" { |
| 125 | _, _ = fmt.Fprintln(os.Stderr, complaint) |
| 126 | continue loop |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return pass, nil |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // PromptChoice is a prompt giving possible choices |
| 135 | // Return the index starting at zero of the choice selected. |
no test coverage detected