-
Notifications
You must be signed in to change notification settings - Fork 30
/
fake.go
101 lines (87 loc) · 2.81 KB
/
fake.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
package fake
import (
"strings"
"github.com/jenkins-x/jx-helpers/v3/pkg/input"
"github.com/pkg/errors"
)
// FakeInput provide a fake provider for testing
type FakeInput struct {
// Values the values to return indexed by the message
Values map[string]string
// OrderedValues are used if there is not a value indexed by message
OrderedValues []string
// Counter the number of fields that have been requested so far
Counter int
}
var _ input.Interface = &FakeInput{}
// PickPassword gets a password (via hidden input) from a user's free-form input
func (f *FakeInput) PickPassword(message string, help string) (string, error) {
value := f.getValue(message)
if value == "" {
return "", errors.Errorf("missing fake value for message: %s", message)
}
return value, nil
}
// PickValue picks a value
func (f *FakeInput) PickValue(message string, defaultValue string, required bool, help string) (string, error) {
value := f.getValue(message)
if value == "" {
value = defaultValue
}
return value, nil
}
// PickValidValue gets an answer to a prompt from a user's free-form input with a given validator
func (f *FakeInput) PickValidValue(message string, defaultValue string, validator func(val interface{}) error, help string) (string, error) {
return f.PickValue(message, defaultValue, false, help)
}
// PickNameWithDefault picks a value
func (f *FakeInput) PickNameWithDefault(names []string, message string, defaultValue string, help string) (string, error) {
value := f.getValue(message)
if value == "" {
value = defaultValue
}
return value, nil
}
func (f *FakeInput) SelectNamesWithFilter(names []string, message string, selectAll bool, filter string, help string) ([]string, error) {
return f.SelectNames(names, message, false, help)
}
func (f *FakeInput) SelectNames(names []string, message string, selectAll bool, help string) ([]string, error) {
value, err := f.PickValue(message, "", false, help)
if err != nil {
return nil, errors.Wrapf(err, "failed to ")
}
return []string{value}, nil
}
func (f *FakeInput) Confirm(message string, defaultValue bool, help string) (bool, error) {
value, err := f.PickValue(message, "", false, help)
if err != nil {
return false, errors.Wrapf(err, "failed to ")
}
if value == "" {
return defaultValue, nil
}
switch strings.ToLower(value) {
case "true", "y", "yes":
return true, nil
default:
return false, nil
}
}
// getValue returns the value using the message key or the ordered value if not available
func (f *FakeInput) getValue(message string) string {
if f.Values == nil {
f.Values = map[string]string{}
}
if f.OrderedValues == nil {
f.OrderedValues = []string{}
}
value := f.Values[message]
if value == "" {
// lets see if we have an ordered value
if f.Counter < len(f.OrderedValues) {
value = f.OrderedValues[f.Counter]
}
}
f.Counter++
return value
}