/
prompt_displayer.go
54 lines (47 loc) · 1.33 KB
/
prompt_displayer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package prompt_displayer
import (
"github.com/kurtosis-tech/stacktrace"
"github.com/manifoldco/promptui"
"github.com/sirupsen/logrus"
"net/mail"
)
const (
maxLabelLength = 110
)
func DisplayConfirmationPromptAndGetBooleanResult(label string, defaultValue string) (string, error) {
if len(label) > maxLabelLength {
return "", stacktrace.NewError("Label '%v' is longer than the maximum allowed characters, '%v'", label, maxLabelLength)
}
prompt := promptui.Prompt{
Label: label,
Default: defaultValue,
AllowEdit: false,
Validate: validateConfirmationInput,
Mask: 0,
HideEntered: false,
Templates: nil,
IsConfirm: false,
IsVimMode: false,
Pointer: nil,
Stdin: nil,
Stdout: nil,
}
userInput, err := prompt.Run()
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred displaying the prompt")
}
logrus.Debugf("User input: '%v'", userInput)
return userInput, nil
}
// ====================================================================================================
//
// Private Helper Functions
//
// ====================================================================================================
func validateConfirmationInput(input string) error {
if input == "" {
return nil
}
_, parsingErr := mail.ParseAddress(input)
return parsingErr
}