PromptDefault is a simple text input with a default value.
(prompt, name, preValue string, validators ...PromptValidator)
| 55 | |
| 56 | // PromptDefault is a simple text input with a default value. |
| 57 | func PromptDefault(prompt, name, preValue string, validators ...PromptValidator) (string, error) { |
| 58 | loop: |
| 59 | for { |
| 60 | if preValue != "" { |
| 61 | _, _ = fmt.Fprintf(os.Stderr, "%s [%s]: ", prompt, preValue) |
| 62 | } else { |
| 63 | _, _ = fmt.Fprintf(os.Stderr, "%s: ", prompt) |
| 64 | } |
| 65 | |
| 66 | line, err := bufio.NewReader(os.Stdin).ReadString('\n') |
| 67 | if err != nil { |
| 68 | return "", err |
| 69 | } |
| 70 | |
| 71 | line = strings.TrimSpace(line) |
| 72 | |
| 73 | if preValue != "" && line == "" { |
| 74 | line = preValue |
| 75 | } |
| 76 | |
| 77 | for _, validator := range validators { |
| 78 | complaint, err := validator(name, line) |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | if complaint != "" { |
| 83 | _, _ = fmt.Fprintln(os.Stderr, complaint) |
| 84 | continue loop |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return line, nil |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // PromptPassword is a specialized text input that doesn't display the characters entered. |
| 93 | func PromptPassword(prompt, name string, validators ...PromptValidator) (string, error) { |
no test coverage detected