2020-07-29 03:04:14 +00:00
|
|
|
package promptutil
|
|
|
|
|
|
|
|
import (
|
2020-07-29 04:55:26 +00:00
|
|
|
"bufio"
|
2020-07-29 03:04:14 +00:00
|
|
|
"fmt"
|
2020-08-20 17:53:09 +00:00
|
|
|
"io"
|
2020-08-11 23:15:06 +00:00
|
|
|
"io/ioutil"
|
2020-07-29 03:04:14 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
2020-08-11 23:15:06 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/fileutil"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-07-29 03:04:14 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var au = aurora.NewAurora(true)
|
|
|
|
|
2020-08-11 23:15:06 +00:00
|
|
|
// PasswordConfirm defines an enum type that can determine whether or not
|
|
|
|
// a prompt should confirm a password input.
|
|
|
|
type PasswordConfirm int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NoConfirmPass enum to indicate to the prompt that confirming the password is not needed.
|
|
|
|
NoConfirmPass PasswordConfirm = iota
|
|
|
|
// ConfirmPass enum to indicate to the prompt to confirm the password entered.
|
|
|
|
ConfirmPass
|
|
|
|
)
|
|
|
|
|
2020-09-15 14:52:07 +00:00
|
|
|
// PasswordReaderFunc takes in a *file and returns a password using the terminal package
|
|
|
|
func passwordReaderFunc(file *os.File) ([]byte, error) {
|
|
|
|
pass, err := terminal.ReadPassword(int(file.Fd()))
|
|
|
|
return pass, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordReader has passwordReaderFunc as the default but can be changed for testing purposes.
|
|
|
|
var PasswordReader func(file *os.File) ([]byte, error) = passwordReaderFunc
|
|
|
|
|
2020-08-20 19:14:03 +00:00
|
|
|
// ValidatePrompt requests the user for text and expects the user to fulfill the provided validation function.
|
2020-08-20 17:53:09 +00:00
|
|
|
func ValidatePrompt(r io.Reader, promptText string, validateFunc func(string) error) (string, error) {
|
2020-07-29 03:04:14 +00:00
|
|
|
var responseValid bool
|
|
|
|
var response string
|
|
|
|
for !responseValid {
|
|
|
|
fmt.Printf("%s:\n", au.Bold(promptText))
|
2020-08-20 17:53:09 +00:00
|
|
|
scanner := bufio.NewScanner(r)
|
2020-07-29 04:55:26 +00:00
|
|
|
if ok := scanner.Scan(); ok {
|
|
|
|
item := scanner.Text()
|
|
|
|
response = strings.TrimRight(item, "\r\n")
|
|
|
|
if err := validateFunc(response); err != nil {
|
|
|
|
fmt.Printf("Entry not valid: %s\n", au.BrightRed(err))
|
|
|
|
} else {
|
|
|
|
responseValid = true
|
|
|
|
}
|
2020-07-29 03:04:14 +00:00
|
|
|
} else {
|
2020-07-29 04:55:26 +00:00
|
|
|
return "", errors.New("could not scan text input")
|
2020-07-29 03:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultPrompt prompts the user for any text and performs no validation. If nothing is entered it returns the default.
|
|
|
|
func DefaultPrompt(promptText string, defaultValue string) (string, error) {
|
|
|
|
var response string
|
2020-07-29 08:33:28 +00:00
|
|
|
if defaultValue != "" {
|
|
|
|
fmt.Printf("%s %s:\n", promptText, fmt.Sprintf("(%s: %s)", au.BrightGreen("default"), defaultValue))
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%s\n", promptText)
|
|
|
|
}
|
2020-07-29 04:55:26 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
if ok := scanner.Scan(); ok {
|
|
|
|
item := scanner.Text()
|
|
|
|
response = strings.TrimRight(item, "\r\n")
|
|
|
|
if response == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
return response, nil
|
2020-07-29 03:04:14 +00:00
|
|
|
}
|
2020-07-29 04:55:26 +00:00
|
|
|
return "", errors.New("could not scan text input")
|
2020-07-29 03:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultAndValidatePrompt prompts the user for any text and expects it to fulfill a validation function. If nothing is entered
|
|
|
|
// the default value is returned.
|
|
|
|
func DefaultAndValidatePrompt(promptText string, defaultValue string, validateFunc func(string) error) (string, error) {
|
|
|
|
var responseValid bool
|
|
|
|
var response string
|
|
|
|
for !responseValid {
|
|
|
|
fmt.Printf("%s %s:\n", promptText, fmt.Sprintf("(%s: %s)", au.BrightGreen("default"), defaultValue))
|
2020-07-29 04:55:26 +00:00
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
|
|
if ok := scanner.Scan(); ok {
|
|
|
|
item := scanner.Text()
|
|
|
|
response = strings.TrimRight(item, "\r\n")
|
|
|
|
if err := validateFunc(response); err != nil {
|
|
|
|
fmt.Printf("Entry not valid: %s\n", au.BrightRed(err))
|
|
|
|
} else {
|
|
|
|
responseValid = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "", errors.New("could not scan text input")
|
2020-07-29 03:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PasswordPrompt prompts the user for a password, that repeatedly requests the password until it qualifies the
|
|
|
|
// passed in validation function.
|
|
|
|
func PasswordPrompt(promptText string, validateFunc func(string) error) (string, error) {
|
|
|
|
var responseValid bool
|
|
|
|
var response string
|
|
|
|
for !responseValid {
|
2020-08-10 22:35:30 +00:00
|
|
|
fmt.Printf("%s: ", au.Bold(promptText))
|
2020-09-15 14:52:07 +00:00
|
|
|
bytePassword, err := PasswordReader(os.Stdin)
|
2020-07-29 03:04:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
response = strings.TrimRight(string(bytePassword), "\r\n")
|
|
|
|
if err := validateFunc(response); err != nil {
|
|
|
|
fmt.Printf("\nEntry not valid: %s\n", au.BrightRed(err))
|
|
|
|
} else {
|
2020-08-10 22:35:30 +00:00
|
|
|
fmt.Println("")
|
2020-07-29 03:04:14 +00:00
|
|
|
responseValid = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
2020-08-11 23:15:06 +00:00
|
|
|
|
|
|
|
// InputPassword with a custom validator along capabilities of confirming
|
|
|
|
// the password and reading it from disk if a specified flag is set.
|
|
|
|
func InputPassword(
|
|
|
|
cliCtx *cli.Context,
|
|
|
|
passwordFileFlag *cli.StringFlag,
|
|
|
|
promptText string,
|
|
|
|
confirmText string,
|
2020-09-17 01:34:42 +00:00
|
|
|
shouldConfirmPassword bool,
|
2020-08-11 23:15:06 +00:00
|
|
|
passwordValidator func(input string) error,
|
|
|
|
) (string, error) {
|
|
|
|
if cliCtx.IsSet(passwordFileFlag.Name) {
|
|
|
|
passwordFilePathInput := cliCtx.String(passwordFileFlag.Name)
|
|
|
|
passwordFilePath, err := fileutil.ExpandPath(passwordFilePathInput)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not determine absolute path of password file")
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(passwordFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "could not read password file")
|
|
|
|
}
|
|
|
|
enteredPassword := strings.TrimRight(string(data), "\r\n")
|
|
|
|
if err := passwordValidator(enteredPassword); err != nil {
|
|
|
|
return "", errors.Wrap(err, "password did not pass validation")
|
|
|
|
}
|
|
|
|
return enteredPassword, nil
|
|
|
|
}
|
|
|
|
var hasValidPassword bool
|
|
|
|
var password string
|
|
|
|
var err error
|
|
|
|
for !hasValidPassword {
|
|
|
|
password, err = PasswordPrompt(promptText, passwordValidator)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read password: %v", err)
|
|
|
|
}
|
2020-09-17 01:34:42 +00:00
|
|
|
if shouldConfirmPassword {
|
2020-08-11 23:15:06 +00:00
|
|
|
passwordConfirmation, err := PasswordPrompt(confirmText, passwordValidator)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not read password confirmation: %v", err)
|
|
|
|
}
|
|
|
|
if password != passwordConfirmation {
|
|
|
|
log.Error("Passwords do not match")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasValidPassword = true
|
|
|
|
} else {
|
|
|
|
return password, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return password, nil
|
|
|
|
}
|