/
utils.go
359 lines (305 loc) · 11 KB
/
utils.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package util
import (
"bufio"
"fmt"
internalconstant "github.com/akamai/cli-test-center/internal/constant"
"github.com/akamai/cli-test-center/internal/model"
externalconstant "github.com/akamai/cli-test-center/user/constant"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/briandowns/spinner"
"github.com/clarketm/json"
"github.com/fatih/camelcase"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
)
func CamelToTitle(inp string) string {
inp = strings.Title(inp)
arr := camelcase.Split(inp)
out := strings.Join(arr, " ")
out = strings.Replace(out, "Url", "URL", -1)
return out
}
type Spinner struct {
_spinner *spinner.Spinner
enabled bool
}
func NewSpinner(message string, enabled bool) Spinner {
s := Spinner{enabled: enabled}
s._spinner = spinner.New([]string{". ", ".. ", "... ", ".... ", "..... ", "......"}, 300*time.Millisecond)
s._spinner.Writer = os.Stderr // Out-of-band info like progress info should go to stderr
s._spinner.Prefix = message
return s
}
func (s Spinner) Start() Spinner {
if s.enabled {
s._spinner.Start()
}
return s
}
func (s Spinner) StopWithSuccess() {
if s.enabled {
stopSpinner(s._spinner, color.GreenString("[OK]"))
}
}
func (s Spinner) StopWithFailure() {
if s.enabled {
stopSpinner(s._spinner, color.RedString("[FAIL]"))
}
}
func stopSpinner(s *spinner.Spinner, terminalMessage string) {
s.FinalMSG = s.Prefix + "...... " + terminalMessage
s.Stop()
_, _ = fmt.Fprintln(s.Writer)
}
// Check contains and irrespective of case.
func ContainsIgnoreCase(a string, b string) bool {
return strings.Contains(strings.ToLower(a), strings.ToLower(b))
}
// Check contains and irrespective of case.
func ContainsInArray(array []string, inputString string) bool {
var result = false
for _, x := range array {
if x == strings.ToLower(inputString) {
result = true
break
}
}
return result
}
func ClientProfileInCLIOutputFormat(clientProfile model.ClientProfile) string {
if clientProfile.IpVersion == internalconstant.Ipv6 {
return clientProfile.Client + externalconstant.PlusSign + "IPv6"
} else {
return clientProfile.Client + externalconstant.PlusSign + "IPv4"
}
}
func RequestHeaderInCLIOutputFormat(headerName, headerAction, headerValue string) string {
switch strings.ToUpper(headerAction) {
case internalconstant.Add:
return fmt.Sprintf("(%s): %s: %s", "Added", headerName, headerValue)
case internalconstant.Modify:
return fmt.Sprintf("(%s): %s: %s", "Modified", headerName, headerValue)
case internalconstant.Filter:
return fmt.Sprintf("(%s): %s: %s", "Filtered", headerName, "N/A")
}
return ""
}
func ConvertBooleanToYesOrNo(input bool) string {
if input {
return "Yes"
}
return "No"
}
func FormatTime(inputTime string) string {
layout := "2006-01-02T15:04:05+0000"
myDate, err := time.Parse(layout, inputTime)
if err != nil {
fmt.Println(err)
}
// convert this date to desired format when decided.
return myDate.Format("01/02/2006, 15:04 PM -07:00")
}
// Get all placeholders in string inside {{}}
func GetPlaceHoldersInString(errorMessage, regex string) []string {
r := regexp.MustCompile(regex)
matches := r.FindAllStringSubmatch(errorMessage, -1)
var placeHolders = make([]string, len(matches))
for i, v := range matches {
placeHolders[i] = v[1]
}
return placeHolders
}
func ReadStdin(cmd *cobra.Command) (bool, []byte) {
file := os.Stdin
fi, err := file.Stat()
if err != nil {
AbortWithExitCode(fmt.Sprintf(GetServiceMessage(cmd, internalconstant.MessageTypeDisplay, "", "standardInputErrorMsg"), err), internalconstant.ExitStatusCode1)
}
size := fi.Size()
isStandardInputAvailable := false
if (fi.Mode() & os.ModeCharDevice) == 0 {
isStandardInputAvailable = true
}
if size > 0 {
log.Debug("%v bytes available in Stdin\n", size)
jsonData, err := ioutil.ReadAll(bufio.NewReader(os.Stdin))
if err != nil {
AbortWithExitCode(fmt.Sprintf(GetServiceMessage(cmd, internalconstant.MessageTypeDisplay, "", "standardInputErrorMsg"), err), internalconstant.ExitStatusCode1)
}
return isStandardInputAvailable, jsonData
}
log.Debug("Stdin is empty")
return isStandardInputAvailable, nil
}
func ByteArrayToStruct(cmd *cobra.Command, byt []byte, payloadObject interface{}) {
if err := json.Unmarshal(byt, payloadObject); err != nil {
log.Debug(err)
AbortWithUsageAndMessageAndCode(cmd, GetErrorMessageForFlag(cmd, internalconstant.Invalid, internalconstant.Json), internalconstant.ExitStatusCode3)
}
}
// CheckIfBothJsonAndFlagAreSetForCommand method returns true if json input is set, otherwise false
func CheckIfBothJsonAndFlagAreSetForCommand(cmd *cobra.Command, jsonData []byte, isStandardInputAvailable bool) bool {
checkIfSubCommandFlagsAreNonEmpty := checkIfAnySubCommandFlagIsSet(cmd)
if checkIfSubCommandFlagsAreNonEmpty && jsonData != nil {
AbortWithUsageAndMessageAndCode(cmd, GetGlobalErrorMessage("invalidCommandInput"), internalconstant.ExitStatusCode2)
} else if !checkIfSubCommandFlagsAreNonEmpty && !isStandardInputAvailable {
// If command is provided without JSON input and flags, we simply through flag missing error and abort with usage
AbortWithUsageAndMessageAndCode(cmd, GetErrorMessageForFlag(cmd, internalconstant.Missing, "flagOrJsonImport"), internalconstant.ExitStatusCode2)
} else if !checkIfSubCommandFlagsAreNonEmpty && jsonData == nil {
AbortWithUsageAndMessageAndCode(cmd, GetGlobalErrorMessage("invalidJsonInput"), internalconstant.ExitStatusCode3)
}
return !checkIfSubCommandFlagsAreNonEmpty
}
/*
Returns true/false if any flag has been set/modified for the child level sub-command. e.g. akamai test-center ts
generate-default, here flags for generate-default will be checked and global flags will be not be included in check.
*/
func checkIfAnySubCommandFlagIsSet(cmd *cobra.Command) bool {
isSubCommandFlagSet := false
// set boolean to true if any non-inherited flag is passed as a command line flag
cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
if cmd.Flags().Changed(flag.Name) {
isSubCommandFlagSet = true
}
})
return isSubCommandFlagSet
}
// LegacyArgs is used to invalidate unknown subcommands took reference from cobra.args legacyArgs() library
func LegacyArgs(cmd *cobra.Command, args []string) error {
// we will not show error when no subcommand is given for parent command, but we will show help section.
if len(args) <= 0 {
return nil
}
// no subcommand, always take args
if cmd.HasSubCommands() {
return fmt.Errorf(GetErrorMessageForSubArgument(cmd, internalconstant.Invalid, internalconstant.SubCommandWrongArgumentPassed), args[0], cmd.Name(), FindSuggestions(cmd, args[0]))
}
// root command with subcommands, do subcommand checking.
if !cmd.HasParent() && len(args) > 0 {
return fmt.Errorf(GetErrorMessageForSubArgument(cmd, internalconstant.Invalid, internalconstant.SubCommandWrongArgumentPassed), args[0], cmd.Name(), FindSuggestions(cmd, args[0]))
}
return nil
}
// FindSuggestions returns a list possible subcommands referenced cobra.command findSuggestions() library
func FindSuggestions(cmd *cobra.Command, arg string) string {
if cmd.DisableSuggestions {
return internalconstant.Empty
}
if cmd.SuggestionsMinimumDistance <= 0 {
cmd.SuggestionsMinimumDistance = 2
}
suggestionsString := internalconstant.Empty
if suggestions := cmd.SuggestionsFor(arg); len(suggestions) > 0 {
suggestionsString += "\n\nDid you mean this?\n"
for _, s := range suggestions {
suggestionsString += fmt.Sprintf("\t%v\n", s)
}
}
return suggestionsString
}
// NoArgsCheck returns an error if any args are included.
func NoArgsCheck(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf(GetErrorMessageForSubArgument(cmd, internalconstant.Invalid, internalconstant.SubCommandWithArgumentNotAllowed), args[0], cmd.Name())
}
return nil
}
// GetHttpExitCode get equivalent exit status code for corresponding http status code
func GetHttpExitCode(responseCode string) int {
log.Debugf("Response Status Code [%q] ", responseCode)
statusCode, err := strconv.Atoi(responseCode)
if err != nil {
statusCode = internalconstant.ExitStatusCode1
}
if statusCode >= 400 && statusCode <= 550 { // Difference used for 4xx and 5xx errors
statusCode = statusCode - internalconstant.BaseSubtractor
} else if statusCode >= 100 && statusCode <= 399 { // 1xx 2xx & 3xx errors are treated as success
statusCode = internalconstant.ExitStatusCode0
} else {
statusCode = internalconstant.ExitStatusCode1
}
log.Debugf("Exit Status Code [%v] ", statusCode)
return statusCode
}
func GetStatusKeyAndColour(status string) (string, color.Color) {
if status == internalconstant.CompletedEnum {
c := color.New(color.FgGreen)
return internalconstant.CompletedKey, *c
} else if status == internalconstant.CompletedWithUnexpectedResultsEnum {
c := color.New(color.FgYellow)
return internalconstant.CompletedWithUnexpectedResultsKey, *c
} else if status == internalconstant.InProgressEnum {
return internalconstant.InProgressKey, *color.New()
} else {
c := color.New(color.FgRed)
return internalconstant.FailedKey, *c
}
}
func GetColourForEnum(enum string, bold bool) color.Color {
c := color.New()
if bold {
c.Add(color.Bold)
}
if enum == internalconstant.GetRequestMethod {
c.Add(color.FgBlue)
} else if enum == internalconstant.HeadRequestMethod {
c.Add(color.FgYellow)
} else if enum == internalconstant.PostRequestMethod {
c.Add(color.FgGreen)
}
return *c
}
func GetResolvedOrUnResolvedHeaders(header model.RequestHeader) string {
var headerName = header.HeaderName
var headerAction = header.HeaderAction
var headerValue = header.HeaderValue
if header.HeaderNameResolved != internalconstant.Empty {
headerName = header.HeaderNameResolved
}
if header.HeaderValueResolved != internalconstant.Empty {
headerValue = header.HeaderValueResolved
}
return RequestHeaderInCLIOutputFormat(headerName, headerAction, headerValue)
}
func GetResolvedOrUnResolvedCondition(condition model.Condition) string {
var cond = condition.ConditionExpression
if condition.ConditionExpressionResolved != internalconstant.Empty {
cond = condition.ConditionExpressionResolved
}
return cond
}
func GetResolvedOrUnResolvedRequestBody(testRequest model.TestRequest) string {
var requestBody = testRequest.RequestBody
if testRequest.RequestBodyResolved != internalconstant.Empty {
requestBody = testRequest.RequestBodyResolved
}
return requestBody
}
func GetResolvedOrUnResolvedRequestURL(testRequest model.TestRequest) string {
var url = testRequest.TestRequestUrl
if testRequest.TestRequestUrlResolved != internalconstant.Empty {
url = testRequest.TestRequestUrlResolved
}
return url
}
func SetVariablesInCLIOutputFormat(variable model.DynamicVariable) string {
return variable.VariableName + externalconstant.Equals + variable.VariableValue
}
func GetConvertedInteger(number string) int {
num, _ := strconv.Atoi(number)
return num
}
func GetTotalTestCasesCount(testCases []model.TestCase) int {
count := len(testCases)
for _, testcase := range testCases {
count += len(testcase.DerivedTestCases)
}
return count
}