-
Notifications
You must be signed in to change notification settings - Fork 1
/
esutil.go
308 lines (265 loc) · 7.89 KB
/
esutil.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
package esutil
import (
"bytes"
"context"
"encoding/json"
"fmt"
elastic7 "github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/geoffmore/esctl/pkg/opts"
"gopkg.in/yaml.v2"
"io/ioutil"
"reflect"
)
// TODO - rename esutil to util or figure out a better name for this package
// TODO - split this until its own types file
type esRequest interface {
Do(context.Context, esapi.Transport) (*esapi.Response, error)
}
// TODO - remove this in the future if not needed
/*
// Deserialize a response into a generic interface
func Des(esRes *esapi.Response) (r map[string]interface{}, err error) {
//statusCode := esRes.StatusCode
//header := esRes.Header
if err = json.NewDecoder(esRes.Body).Decode(&r); err != nil {
return r, err
}
defer esRes.Body.Close()
return r, err
}
*/
// TODO - remove this in the future if not needed
/*
// Display a generic interface as yaml
func MapToYamlish(r map[string]interface{}, s int) {
var nestedMap map[string]interface{}
spaces := s
if false {
fmt.Printf("%v", spaces)
}
for k, v := range r {
switch v.(type) {
default:
// Did not find a map as a value
fmt.Printf("%s%v: '%v'\n", strings.Repeat(" ", 2*(spaces)), k, v)
case map[string]interface{}:
// Found a map as a value
fmt.Printf("%v:\n", k)
// Explicitly create a map from that value, recurse
nestedMap = v.(map[string]interface{})
MapToYamlish(nestedMap, spaces+1)
// I need a way to use multiple interface types and iterate over them
// Maybe I add logic to figure out how to determine the type of the map
// interface thing?
//case map[int]interface{}:
// fmt.Printf("%v:\n", k)
// nestedMap = v.(map[int]interface{})
// mapToYamlish(nestedMap, spaces+1)
}
}
}
*/
// TODO - remove this in the future if not needed
/*
// Convert a generic interface of type map[string]interface{} to yaml bytes
func MapToYamlBytes(r map[string]interface{}) (b []byte, err error) {
return b, err
}
*/
// TODO - remove this in the future if not needed
// TODO - rename RequestNew -> Request afterwards
/*
// Generic function used to execute requests and print results
func Request(r esRequest, c *elastic7.Client) error {
res, err := r.Do(context.Background(), c.Transport)
if err != nil {
return err
}
// Check if status code is successful
if !(res.StatusCode >= 200 && res.StatusCode < 300) {
return fmt.Errorf("Status Code '%v' not in range 200-299. Exiting...\n", res.StatusCode)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
defer res.Body.Close()
fmt.Printf("%+v\n", string(b))
return nil
}
*/
// TODO - remove this in the future if not needed
/*
// Check if the 'Format' field exists for a struct beneath a provided interface
func FormatExists(i interface{}) bool {
return reflect.ValueOf(i).FieldByName("Format").IsValid()
}
*/
// Convert bytes into a desired output format
func ParseBytes(b []byte, fmtExists bool, outputFmt string) (err error) {
if outputFmt == "yaml" && !fmtExists {
var iface map[string]interface{}
err := json.Unmarshal(b, &iface)
if err != nil {
return err
}
b, err = yaml.Marshal(iface)
if err != nil {
return err
}
}
fmt.Println(string(b))
return nil
}
func GetResponse(r esRequest, c *elastic7.Client) (*esapi.Response, error) {
res, err := r.Do(context.Background(), c.Transport)
return res, err
}
// Generic function used to execute requests and return bytes
func RequestNew(r esRequest, c *elastic7.Client) ([]byte, error) {
var b []byte
res, err := r.Do(context.Background(), c.Transport)
if err != nil {
return b, err
}
// Check if status code is successful
if !(res.StatusCode >= 200 && res.StatusCode < 300) {
return b, fmt.Errorf("Status Code '%v' not in range 200-299. Exiting...\n", res.StatusCode)
}
b, err = ioutil.ReadAll(res.Body)
if err != nil {
return b, err
}
// https://tour.golang.org/moretypes/25
// https://stackoverflow.com/questions/57740428/handling-errors-in-defer
defer func() {
err = res.Body.Close()
}()
if err != nil {
return b, err
}
return b, nil
}
// Attempt to change the 'Format' field of a struct if it exists.
// Return whether the field exists and could be set
func SetFormat(s reflect.Value, outputFmt string) bool {
// https://blog.golang.org/laws-of-reflection
var matches bool
var fieldExists bool = s.FieldByName("Format").IsValid()
var canSet bool = s.CanSet()
if s.Kind() == reflect.Struct {
if fieldExists {
// Attempt to change output format
switch outputFmt {
case "json":
matches = true
case "yaml":
matches = true
}
if matches && canSet {
s.FieldByName("Format").SetString(outputFmt)
}
}
}
return fieldExists && canSet
}
// https://golangcode.com/check-if-element-exists-in-slice/
// Find takes a slice and looks for an element in it. If found it will
// return it's key, otherwise it will return -1 and a bool of false.
func Find(slice []string, val string) (int, bool) {
for i, item := range slice {
if item == val {
return i, true
}
}
return -1, false
}
// Take json. Return a reader
func JSONToReader(intf interface{}) (*bytes.Reader, error) {
b, err := json.Marshal(intf)
return bytes.NewReader(b), err
}
func FilenameToReader(name string) (*bytes.Reader, error) {
var r *bytes.Reader
// Handle stdin
if name == "-" {
return r, fmt.Errorf("Reading from stdin is not yet supported")
} else {
// Open the file and read its contents
b, err := ioutil.ReadFile(name)
if err != nil {
return r, err
}
// Create a *io.Reader from bytes
r = bytes.NewReader(b)
}
return r, nil
}
// Attempt to change the value of an arbitrary boolean type field
// Return whether the field exists and could be set
func SetBool(s reflect.Value, field string, a bool) bool {
var fieldExists bool = s.FieldByName(field).IsValid()
var canSet bool = s.CanSet()
if s.Kind() == reflect.Struct {
if fieldExists && canSet {
// Attempt to change value
s.FieldByName(field).SetBool(a)
}
}
return fieldExists && canSet
}
// Attempt to change the value of an arbitrary string type field
// Return whether the field exists and could be set
func SetString(s reflect.Value, field string, a string) bool {
var fieldExists bool = s.FieldByName(field).IsValid()
var canSet bool = s.CanSet()
if s.Kind() == reflect.Struct {
if fieldExists && canSet {
// Attempt to change value
s.FieldByName(field).SetString(a)
}
}
return fieldExists && canSet
}
// Attempt to change the value of an arbitrary int64 type field
// Return whether the field exists and could be set
func SetInt(s reflect.Value, field string, a int64) bool {
var fieldExists bool = s.FieldByName(field).IsValid()
var canSet bool = s.CanSet()
if s.Kind() == reflect.Struct {
if fieldExists && canSet {
// Attempt to change value
s.FieldByName(field).SetInt(a)
}
}
return fieldExists && canSet
}
// Attempt to set all fields contained in opts.CommandOptions according to the
// map opts.CmdsToFieldNames
func SetAllCmdOpts(v reflect.Value, c *opts.CommandOptions) map[string]bool {
cmdOpts := opts.CmdsToFieldNames
// https://stackoverflow.com/questions/18926304
cv := reflect.ValueOf(c).Elem()
var changedFields map[string]bool = make(map[string]bool)
for cmdFieldName, structFieldName := range cmdOpts {
val := v.FieldByName(structFieldName)
if val.IsValid() {
// Type lookup is necessary here for the switch
switch t := val.Type().String(); t {
case "string":
changedFields[structFieldName] = SetString(v, structFieldName, cv.FieldByName(cmdFieldName).String())
//case "int":
// reflect's SetInt() expects int64
case "int64":
changedFields[structFieldName] = SetInt(v, structFieldName, cv.FieldByName(cmdFieldName).Int())
case "bool":
changedFields[structFieldName] = SetBool(v, structFieldName, cv.FieldByName(cmdFieldName).Bool())
}
} else {
// Handle the case where the field doesn't exist in the struct
changedFields[structFieldName] = false
}
}
return changedFields
}