-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs.go
177 lines (142 loc) · 3.57 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
"net/mail"
"strconv"
"syscall"
)
/*
Get text input
*/
func getTextInput(question string, required bool, value string) string {
var output string
fmt.Println(question)
changeValue := true
if value != "" {
changeValue = getConfirmInput("The current value is "+colorCyan+value+colorReset+" do you want to change it?", false)
output = value
if changeValue {
fmt.Println(question)
}
}
if changeValue {
fmt.Printf("> " + colorGreen)
fmt.Scanln(&output)
fmt.Println(colorReset)
}
if required == true && output == "" {
fmt.Println(colorRed + "An empty value is not allowed here" + colorReset)
output = getTextInput(question, required, "")
}
return output
}
/*
Get number input
*/
func getNumberInput(question string, required bool, value int) int {
var output int
var err error
var valueForInput string
if value == 0 {
valueForInput = ""
} else {
valueForInput = strconv.Itoa(value)
}
text := getTextInput(question, required, valueForInput)
output, err = strconv.Atoi(text)
if err != nil {
fmt.Println(colorRed + "Please enter a valid port" + colorReset)
output = getNumberInput(question, required, 0)
}
return output
}
/*
Ask for confirmation
*/
func getConfirmInput(question string, value bool) bool {
var output bool
fmt.Println(question)
changeValue := true
if value {
changeValue = getConfirmInput("Currently the option is active. Do you want to change it?", false)
output = value
if changeValue {
fmt.Println(question)
}
}
if changeValue {
output = askForConfirmation(true)
fmt.Println(colorReset)
}
return output
}
/*
Get email address and validate it
*/
func getEmailAddressInput(question string, required bool, value string) string {
var output string
fmt.Println(question)
changeValue := true
if value != "" {
changeValue = getConfirmInput("The current value is "+colorCyan+value+colorReset+" do you want to change it?", false)
output = value
if changeValue {
fmt.Println(question)
}
}
if changeValue {
fmt.Printf("> " + colorGreen)
fmt.Scanln(&output)
fmt.Println(colorReset)
}
if required == true && output == "" {
fmt.Println(colorRed + "An empty value is not allowed here" + colorReset)
output = getEmailAddressInput(question, required, "")
}
_, err := mail.ParseAddress(output)
if err != nil {
fmt.Println(colorRed + "The email address is not valid. Please try again." + colorReset)
output = getEmailAddressInput(question, required, "")
}
return output
}
/*
Get password input (hidden input)
*/
func getPasswordInput(question string, required bool, value string) string {
var output string
var passwd []byte
var err error
fmt.Println(question)
changeValue := true
if value != "" {
changeValue = getConfirmInput("The current value is "+colorCyan+value+colorReset+" do you want to change it?", false)
passwd = []byte(value)
if changeValue {
fmt.Println(question)
}
}
if changeValue {
fmt.Printf("> " + colorGreen)
passwd, err = terminal.ReadPassword(int(syscall.Stdin))
fmt.Println(colorReset)
}
if err != nil {
fmt.Println(colorRed + "An error occurred. Please try again." + colorReset)
output = getPasswordInput(question, required, "")
} else {
output = string(passwd)
}
if required == true && output == "" {
fmt.Println(colorRed + "An empty value is not allowed here" + colorReset)
output = getPasswordInput(question, required, "")
}
return output
}
/*
Print a section header
*/
func printSectionHeader(title string) {
fmt.Println("\n\n\n --- " + colorCyan + title + colorReset + " --- \n")
}