2020-07-29 03:04:14 +00:00
|
|
|
package promptutil
|
|
|
|
|
|
|
|
import (
|
2020-07-29 04:55:26 +00:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
2020-07-29 03:04:14 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/logrusorgru/aurora"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var au = aurora.NewAurora(true)
|
|
|
|
|
|
|
|
// ValidatePrompt requests the user for text and expects it to fulfil la provided validation function.
|
|
|
|
func ValidatePrompt(promptText string, validateFunc func(string) error) (string, error) {
|
|
|
|
var responseValid bool
|
|
|
|
var response string
|
|
|
|
for !responseValid {
|
|
|
|
fmt.Printf("%s:\n", au.Bold(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 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-07-29 03:04:14 +00:00
|
|
|
bytePassword, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
|
|
|
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
|
|
|
|
}
|