/
stdin.go
191 lines (154 loc) · 4.08 KB
/
stdin.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package stdinUtils
import (
"bufio"
"fmt"
fileUtils "github.com/easysoft/zentaoatf/src/utils/file"
i118Utils "github.com/easysoft/zentaoatf/src/utils/i118"
langUtils "github.com/easysoft/zentaoatf/src/utils/lang"
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
"github.com/fatih/color"
"os"
"regexp"
"strconv"
"strings"
)
func InputForCheckout(productId *string, moduleId *string, suiteId *string, taskId *string,
independentFile *bool, scriptLang *string) {
var numb string
productCheckbox := ""
suiteCheckbox := ""
taskCheckbox := ""
if *productId != "" {
productCheckbox = "*"
numb = "1"
} else if *suiteId != "" {
suiteCheckbox = "*"
numb = "2"
} else if *taskId != "" {
taskCheckbox = "*"
numb = "3"
}
coType := GetInput("(1|2|3)", numb, "enter_co_type", productCheckbox, suiteCheckbox, taskCheckbox)
coType = strings.ToLower(coType)
if coType == "1" {
*productId = GetInput("\\d+", *productId,
i118Utils.Sprintf("pls_enter")+" "+i118Utils.Sprintf("product_id")+": "+*productId)
*moduleId = GetInput("\\d*", *moduleId,
i118Utils.Sprintf("pls_enter")+" "+i118Utils.Sprintf("module_id")+": "+*moduleId)
} else if coType == "2" {
*suiteId = GetInput("\\d+", *suiteId,
i118Utils.Sprintf("pls_enter")+" "+i118Utils.Sprintf("suite_id")+": "+*suiteId)
} else if coType == "3" {
*taskId = GetInput("\\d+", *taskId,
i118Utils.Sprintf("pls_enter")+" "+i118Utils.Sprintf("task_id")+": "+*taskId)
}
InputForBool(independentFile, false, "enter_co_independent")
numbs, names, labels := langUtils.GetSupportLanguageOptions(nil)
fmtParam := make([]string, 0)
dft := ""
for idx, label := range labels {
if names[idx] == *scriptLang {
dft = strconv.Itoa(idx + 1)
label += " *"
}
fmtParam = append(fmtParam, label)
}
langStr := GetInput("("+strings.Join(numbs, "|")+")", dft, "enter_co_language", strings.Join(fmtParam, "\n"))
langNumb, _ := strconv.Atoi(langStr)
*scriptLang = names[langNumb-1]
}
func InputForDir(dir *string, dft string, i118Key string) {
*dir = GetInput("is_dir", dft, "enter_dir", i118Utils.Sprintf(i118Key))
}
func InputForBool(in *bool, defaultVal bool, fmtStr string, fmtParam ...interface{}) {
str := GetInput("(yes|no|y|n|)", "", fmtStr, fmtParam...)
if str == "" {
*in = defaultVal
msg := ""
if *in {
msg = "yes"
} else {
msg = "no"
}
logUtils.PrintTo(msg)
return
}
if str == "y" && str != "yes" {
*in = true
} else {
*in = false
}
}
func GetInput(regx string, defaultVal string, fmtStr string, params ...interface{}) string {
var ret string
msg := i118Utils.Sprintf(fmtStr, params...)
for {
logUtils.PrintToWithColor("\n"+msg, color.FgCyan)
Scanf(&ret)
ret = strings.TrimSpace(ret)
if ret == "" && defaultVal != "" {
ret = defaultVal
logUtils.PrintTo(ret)
}
temp := strings.ToLower(ret)
if temp == "exit" {
color.Unset()
os.Exit(0)
}
if regx == "" {
return ret
}
var pass bool
var msg string
if regx == "is_dir" {
pass = fileUtils.IsDir(ret)
msg = "dir_not_exist"
} else {
pass, _ = regexp.MatchString("^"+regx+"$", temp)
msg = "invalid_input"
}
if pass {
return ret
} else {
ret = ""
logUtils.PrintToWithColor(i118Utils.Sprintf(msg), color.FgRed)
}
}
}
func GetInputForScriptInterpreter(defaultVal string, fmtStr string, params ...interface{}) string {
var ret string
msg := i118Utils.Sprintf(fmtStr, params...)
for {
logUtils.PrintToWithColor(msg, color.FgCyan)
Scanf(&ret)
ret = strings.TrimSpace(ret)
if ret == "" && defaultVal != "" {
ret = defaultVal
logUtils.PrintToWithColor(ret, -1)
}
if ret == "exit" {
color.Unset()
os.Exit(0)
}
if ret == "" { // ignore to set
return "-"
}
sep := string(os.PathSeparator)
if sep == `\` {
sep = `\\`
}
reg := fmt.Sprintf(".*%s+[^%s]+", sep, sep)
pass, _ := regexp.MatchString(reg, ret)
if pass {
return ret
} else {
ret = ""
logUtils.PrintToWithColor(i118Utils.Sprintf("invalid_input"), color.FgRed)
}
}
}
func Scanf(a *string) {
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
*a = string(data)
}