-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinputs.go
137 lines (114 loc) · 2.61 KB
/
inputs.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
package goclitools
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
"github.com/howeyc/gopass"
survey "gopkg.in/AlecAivazis/survey.v1"
)
var promptCache map[string]string
func Confirm(text string) bool {
return ConfirmWithDefault(text, true)
}
func ConfirmWithDefault(text string, defaultValue bool) bool {
options := "[y/N]"
if defaultValue {
options = "[Y/n]"
}
response := strings.ToLower(prompt(text+" "+options, nil, false))
if response != "" && response != "y" && response != "n" {
Log(fmt.Sprintf("Sorry, %s is not valid answer", response))
return ConfirmWithDefault(text, defaultValue)
} else if response == "" {
return defaultValue
}
return response != "n"
}
// Prompt ...
func Prompt(text string) string {
return prompt(text, nil, false)
}
// PromptSecret ...
func PromptSecret(text string) string {
return prompt(text, nil, true)
}
// PromptCached ...
func PromptCached(text, key string) string {
// return PromptSecret(name, configKey, false)
return prompt(text, &key, false)
}
// PromptSecretCached ...
func PromptSecretCached(text, key string) string {
return prompt(text, &key, true)
}
// PromptWithChoice ...
func PromptWithChoice(name string, choices []string) (int, error) {
result := ""
_choices := []string{}
for _, choice := range choices {
if choice == "" {
choice = "none"
}
_choices = append(_choices, choice)
}
prompt := &survey.Select{
Message: name + ":",
Options: _choices,
PageSize: 20,
}
if err := survey.AskOne(prompt, &result, nil); err != nil {
return -1, err
}
for index, choise := range _choices {
if choise == result {
return index, nil
}
}
return -1, errors.New("invalid choice selected")
}
// PromptWithMultiChoice ...
func PromptWithMultiChoice(name string, choices []string) ([]int, error) {
var result []string
prompt := &survey.MultiSelect{
Message: name + ":",
Options: choices,
PageSize: 20,
}
if err := survey.AskOne(prompt, &result, nil); err != nil {
return nil, err
}
indexes := []int{}
for index, choise := range choices {
for _, answer := range result {
if choise == answer {
indexes = append(indexes, index)
}
}
}
return indexes, nil
}
func prompt(text string, key *string, masked bool) string {
if key != nil {
if promptCache == nil {
promptCache = map[string]string{}
}
if val := promptCache[*key]; val != "" {
return val
}
}
fmt.Print(text + ": ")
if masked {
val, err := gopass.GetPasswdMasked()
if err != nil {
panic(err)
}
return string(val)
}
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return strings.Trim(scanner.Text(), " ")
}
return ""
}