chore: refactor password code for StdIO (#139)
This commit is contained in:
@ -67,7 +67,7 @@ func (t *terminalStdio) GetStringInput(prompt, defaultValue string) (input strin
|
||||
return
|
||||
}
|
||||
|
||||
// GetSecret prompts the user for a password.
|
||||
// GetSecret prompts the user for a secret.
|
||||
func (t *terminalStdio) GetSecret(prompt string, minLen int) (password string, err error) {
|
||||
question := survey.Password{Message: prompt}
|
||||
opts := []survey.AskOpt{survey.WithStdio(t.Stdin, t.Stdout, t.Stderr)}
|
||||
@ -79,6 +79,24 @@ func (t *terminalStdio) GetSecret(prompt string, minLen int) (password string, e
|
||||
return
|
||||
}
|
||||
|
||||
// GetPassword prompts the user for a secret twice, and inputs must match.
|
||||
// Uses stdio.MinPasswordLen as the minimum input length
|
||||
func (t *terminalStdio) GetPassword(prompt string) (string, error) {
|
||||
pass1, err := t.GetSecret(prompt, MinPasswordLen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Don't bother with the length check the 2nd time, since we check equality to pass1.
|
||||
pass2, err := t.GetSecret(prompt+" again", 0)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if pass1 == pass2 {
|
||||
return pass1, nil
|
||||
}
|
||||
return "", t.Error("Passwords do not match")
|
||||
}
|
||||
|
||||
// GetConfirm asks the user for a y/n answer to a prompt.
|
||||
func (t *terminalStdio) GetConfirm(prompt string) (answer bool) {
|
||||
question := survey.Confirm{
|
||||
|
@ -2,6 +2,8 @@ package stdio
|
||||
|
||||
import "io"
|
||||
|
||||
const MinPasswordLen = 8
|
||||
|
||||
type StdIO interface {
|
||||
io.Writer
|
||||
WriteErr(p []byte) (n int, err error)
|
||||
@ -9,5 +11,6 @@ type StdIO interface {
|
||||
Error(message string) error
|
||||
GetStringInput(prompt, defaultValue string) (string, error)
|
||||
GetSecret(prompt string, minLen int) (string, error)
|
||||
GetPassword(prompt string) (string, error)
|
||||
GetConfirm(prompt string) bool
|
||||
}
|
||||
|
Reference in New Issue
Block a user