/
menu.go
157 lines (125 loc) · 3.27 KB
/
menu.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ofa
import (
"fmt"
"os"
"sort"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
)
func interactiveMenuSelector(label string, entries map[string]*string, defaultValue *string) (*string, error) {
items := make([]string, 0, len(entries))
var defaultSelect *string = nil
for k, v := range entries {
items = append(items, k)
if defaultValue != nil && v != nil && *v == *defaultValue {
defaultSelect = toSP(k)
}
}
result, err := menuSelector(label, items, defaultSelect)
if err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
return entries[*result], nil
}
func awsRoleMenuSelector(label string, roles []samlAwsRole, indent int) (*samlAwsRole, error) {
m := make(map[string]*samlAwsRole, len(roles))
items := make([]string, len(roles))
for i, v := range roles {
menuKey := fmt.Sprintf("%s (%s)", padWidth(v.Role(), indent), v.DisplayName())
m[menuKey] = &roles[i]
items[i] = menuKey
}
result, err := menuSelector(label, items, nil)
if err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
return m[*result], nil
}
func menuSelector(label string, items []string, defaultValue *string) (*string, error) {
if !*globalInteractive {
if defaultValue != nil {
return defaultValue, nil
}
return nil, fmt.Errorf("Selection required but not interactive")
}
sort.Strings(items)
cursorPos := 0
for i, v := range items {
if defaultValue != nil && *defaultValue == v {
cursorPos = i
}
}
prompt := promptui.Select{
Size: 8,
Label: label,
Items: items,
CursorPos: cursorPos,
HideHelp: true,
Stdout: &bellSkipper{},
}
index, _, err := prompt.Run()
if err != nil {
checkForKeyboard(err)
return nil, err
}
return &items[index], nil
}
func textInput(label string, defaultValue *string, hidden bool, validate promptui.ValidateFunc) (*string, error) {
if !*globalInteractive {
if defaultValue != nil {
return defaultValue, nil
}
return nil, fmt.Errorf("Input required but not interactive")
}
prompt := promptui.Prompt{
Label: label,
Validate: validate,
Stdout: &bellSkipper{},
}
if hidden {
prompt.Mask = '*'
}
if defaultValue != nil {
prompt.Default = *defaultValue
}
value, err := prompt.Run()
if err != nil {
checkForKeyboard(err)
return nil, err
}
return &value, nil
}
func checkForKeyboard(err error) {
if err == promptui.ErrInterrupt {
log.Fatal("Terminated by ^C")
} else if err == promptui.ErrEOF {
log.Fatal("Terminated by ^D")
}
}
// bellSkipper implements an io.WriteCloser that skips the terminal bell
// character (ASCII code 7), and writes the rest to os.Stderr. It is used to
// replace readline.Stdout, that is the package used by promptui to display the
// prompts.
//
// This is a workaround for the bell issue documented in
// https://github.com/manifoldco/promptui/issues/49.
type bellSkipper struct{}
// Write implements an io.WriterCloser over os.Stderr, but it skips the terminal
// bell character.
func (bs *bellSkipper) Write(b []byte) (int, error) {
const charBell = 7 // c.f. readline.CharBell
if len(b) == 1 && b[0] == charBell {
return 0, nil
}
return os.Stderr.Write(b)
}
// Close implements an io.WriterCloser over os.Stderr.
func (bs *bellSkipper) Close() error {
return os.Stderr.Close()
}