/
helpers.go
168 lines (144 loc) · 3.45 KB
/
helpers.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
package cmd
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
)
const (
ansiEsc = "\u001B"
clearLine = "\r\033[K"
hideCursor = ansiEsc + "[?25l"
showCursor = ansiEsc + "[?25h"
)
type cmder interface {
getCommand() *cobra.Command
}
// commandError is an error used to signal different error situations in command handling.
type commandError struct {
s string
userError bool
serverError bool
silent bool
exitCode int
statusCode int
}
func (c commandError) Error() string {
details := ""
if c.statusCode > 0 {
details = fmt.Sprintf(" ::statusCode=%d", c.statusCode)
}
return c.s + details
}
func (c commandError) isUserError() bool {
return c.userError
}
func (c commandError) isServerError() bool {
return c.serverError
}
func (c commandError) isSilent() bool {
return c.silent
}
func newUserError(a ...interface{}) commandError {
return commandError{s: fmt.Sprintln(a...), userError: true, exitCode: 101}
}
func newUserErrorWithExitCode(exitCode int, a ...interface{}) commandError {
return commandError{s: fmt.Sprintln(a...), userError: true, exitCode: exitCode}
}
func newSystemError(a ...interface{}) commandError {
return commandError{s: fmt.Sprintln(a...), userError: false, exitCode: 100}
}
var httpStatusCodeToExitCode = map[int]int{
400: 40,
401: 1,
403: 3,
404: 4,
405: 5,
409: 9,
413: 13,
422: 22,
429: 29,
500: 50,
501: 51,
502: 52,
503: 53,
504: 54,
505: 55,
506: 56,
507: 57,
508: 58,
}
func newServerError(r *c8y.Response, err error) commandError {
message := ""
exitCode := 99
statusCode := 0
if r != nil {
if r.Response != nil {
statusCode = r.Response.StatusCode
if v, ok := httpStatusCodeToExitCode[statusCode]; ok {
exitCode = v
}
}
}
return commandError{
s: message,
userError: false,
serverError: true,
silent: true,
exitCode: exitCode,
statusCode: statusCode,
}
}
func newSystemErrorF(format string, a ...interface{}) commandError {
return commandError{s: fmt.Sprintf(format, a...), userError: false}
}
// Catch some of the obvious user errors from Cobra.
// We don't want to show the usage message for every error.
// The below may be to generic. Time will show.
var userErrorRegexp = regexp.MustCompile("argument|flag|shorthand")
func isUserError(err error) bool {
if cErr, ok := err.(commandError); ok && cErr.isUserError() {
return true
}
return userErrorRegexp.MatchString(err.Error())
}
func isServerError(err error) bool {
if cErr, ok := err.(commandError); ok && cErr.isServerError() {
return true
}
return false
}
func isSilentError(err error) bool {
if cErr, ok := err.(commandError); ok && cErr.isSilent() {
return true
}
return false
}
func newErrorSummary(message string, errorsCh <-chan error) error {
errorSummary := errors.New(message)
hasError := false
for err := range errorsCh {
if err != nil {
errorSummary = errors.WithStack(err)
hasError = true
}
}
if !hasError {
return nil
}
return errorSummary
}
// replacePathParameters replaces all of the path parameters in a given URI with the provided values
// Example:
// "alarm/alarms/{id}" => "alarm/alarms/1234" if given a parameter map of {"id": "1234"}
func replacePathParameters(uri string, parameters map[string]string) string {
if parameters == nil {
return uri
}
for key, value := range parameters {
uri = strings.ReplaceAll(uri, "{"+key+"}", value)
}
return uri
}