-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprompts.go
118 lines (109 loc) · 3.4 KB
/
prompts.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Tardy
//
// Created by Posse in NYC
// http://goposse.com
//
// Copyright (c) 2016 Posse Productions LLC.
// All rights reserved.
// See the LICENSE file for licensing details and requirements.
package tardy
import "strings"
// SimplePrompt - a simple prompt to get a value with no restrictions. allows for
// a default.
func SimplePrompt(message string, required Optionality, defaultValue string) Prompt {
return Prompt{
Message: message,
DefaultValue: defaultValue,
Required: required,
RetryIfNoMatch: true,
CaseSensitiveMatch: false,
}
}
// SimpleSecurePrompt - a simple prompt to get a value with no restrictions.
// entry is capture without local echo (typing not visible). allows for a default.
func SimpleSecurePrompt(message string, required Optionality, defaultValue string) Prompt {
return Prompt{
Message: message,
DefaultValue: defaultValue,
Required: required,
RetryIfNoMatch: true,
CaseSensitiveMatch: false,
SecureEntry: true,
}
}
// YesNoPrompt - prompts for a yes or no answer. the final value will be a
// boolean. allows for a default.
func YesNoPrompt(message string, hint string, required Optionality, defaultValue bool) Prompt {
return Prompt{
Message: message,
ValueHint: hint,
DefaultValue: defaultValue,
Required: required,
RetryIfNoMatch: true,
FailIfNoMatch: true,
CaseSensitiveMatch: false,
ValidationFunc: func(prompt *Prompt, value string) (string, Validity) {
_, validity := isValidYesOrNoValue(value)
return value, validity
},
ValueConverter: func(prompt *Prompt, value string) interface{} {
return isPositiveStringValue(value, false)
},
}
}
// SingleValuePrompt - prompts for an answer from within a subset of valid
// answers. allows for a default.
func SingleValuePrompt(message string, hint string, values []string, required Optionality, defaultValue string) Prompt {
return Prompt{
Message: message,
ValueHint: hint,
DefaultValue: defaultValue,
Required: required,
RetryIfNoMatch: true,
FailIfNoMatch: false,
CaseSensitiveMatch: false,
ValidationFunc: func(prompt *Prompt, value string) (string, Validity) {
useValue := value
originalValues := values
checkStrings := values
if prompt.CaseSensitiveMatch == false {
useValue = strings.ToLower(useValue)
checkStrings = mapStrings(checkStrings, func(s string) string {
return strings.ToLower(s)
})
}
for idx, v := range checkStrings {
if useValue == v {
return originalValues[idx], IsValid
}
}
validity := IsValid
if prompt.FailIfNoMatch {
validity = IsNotValid
}
return defaultValue, validity
},
}
}
// helpers
func isValidYesOrNoValue(value string) (bool, Validity) {
switch strings.ToLower(value) {
case "yes", "y", "yo", "si", "yup", "ya", "yep":
return true, IsValid
case "no", "n", "nope", "no way", "nuh uh", "nah":
return false, IsValid
}
return false, IsNotValid
}
// isPositiveStringValue - returns true if the string value matches the list of
// positive string values. empty or non-matched value will return the
// noMatchValue.
func isPositiveStringValue(value string, noMatchValue bool) bool {
if value == "" {
return noMatchValue
}
if boolVal, validity := isValidYesOrNoValue(value); validity == IsValid {
return boolVal
}
return noMatchValue
}