mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
1f21e196b6
* Merge variable declaration and assignment * Use result of type assertion to simplify cases * Replace call to bytes.Compare with bytes.Equal * Drop unnecessary use of the blank identifier * Replace x.Sub(time.Now()) with time.Until(x) * Function literal can be simplified * Use a single append to concatenate two slices * Replace time.Now().Sub(x) with time.Since(x) * Omit comparison with boolean constant * Omit redundant nil check on slices * Nested if can be replaced with else-if * Function call can be replaced with helper function * Omit redundant control flow * Use plain channel send or receive * Simplify returning boolean expression * Merge branch 'origin-master' into fix-antipatterns * Merge branch 'master' into fix-antipatterns
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
package cmd
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
var log = logrus.WithField("prefix", "node")
|
|
|
|
// ConfirmAction uses the passed in actionText as the confirmation text displayed in the terminal.
|
|
// The user must enter Y or N to indicate whether they confirm the action detailed in the warning text.
|
|
// Returns a boolean representing the user's answer.
|
|
func ConfirmAction(actionText string, deniedText string) (bool, error) {
|
|
var confirmed bool
|
|
reader := bufio.NewReader(os.Stdin)
|
|
log.Warn(actionText)
|
|
|
|
for {
|
|
fmt.Print(">> ")
|
|
|
|
line, _, err := reader.ReadLine()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
trimmedLine := strings.TrimSpace(string(line))
|
|
lineInput := strings.ToUpper(trimmedLine)
|
|
if lineInput != "Y" && lineInput != "N" {
|
|
log.Errorf("Invalid option of %s chosen, please only enter Y/N", line)
|
|
continue
|
|
}
|
|
if lineInput == "Y" {
|
|
confirmed = true
|
|
break
|
|
}
|
|
log.Warn(deniedText)
|
|
break
|
|
}
|
|
|
|
return confirmed, nil
|
|
}
|
|
|
|
// EnterPassword queries the user for their password through the terminal, in order to make sure it is
|
|
// not passed in a visible way to the terminal.
|
|
func EnterPassword(confirmPassword bool, pr PasswordReader) (string, error) {
|
|
var passphrase string
|
|
log.Info("Enter a password:")
|
|
bytePassword, err := pr.ReadPassword()
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "could not read account password")
|
|
}
|
|
text := bytePassword
|
|
passphrase = strings.ReplaceAll(text, "\n", "")
|
|
if confirmPassword {
|
|
log.Info("Please re-enter your password:")
|
|
bytePassword, err := pr.ReadPassword()
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "could not read account password")
|
|
}
|
|
text := bytePassword
|
|
confirmedPass := strings.ReplaceAll(text, "\n", "")
|
|
if passphrase != confirmedPass {
|
|
log.Info("Passwords did not match, please try again")
|
|
return EnterPassword(true, pr)
|
|
}
|
|
}
|
|
return passphrase, nil
|
|
}
|