-
Notifications
You must be signed in to change notification settings - Fork 4
/
command.go
622 lines (454 loc) · 16.1 KB
/
command.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/Jeffail/gabs/v2"
"github.com/getkin/kin-openapi/openapi3"
)
type Parameter struct {
varType string
orgName string
description string
required bool
enum []any
location string
value *string
}
// Rename flags with odd names that causes issues in some shells
func renameFlag(name string) string {
name = strings.ReplaceAll(name, "[", "-")
name = strings.ReplaceAll(name, "]", "")
return name
}
func parseCommand(format string, verbose bool) {
doc := loadAPI()
command := flag.Arg(0)
for uri, path := range doc.Paths.Map() {
for method, action := range path.Operations() {
if strings.ReplaceAll(action.OperationID, "_", "-") != command {
continue
}
//Found command, setup flags
subFlag := flag.NewFlagSet(command, flag.ExitOnError)
//Disable unwanted built-in flag features
subFlag.Usage = func() {}
query := url.Values{}
contentType := ""
//Will hold all valid flag values
flags := make(map[string]Parameter)
//Collect all valid URI flags for this command
for _, parameter := range action.Parameters {
//Ignore some flags
if parameter.Value.Name == "page[number]" ||
parameter.Value.Name == "page[size]" ||
parameter.Value.Name == "fields" ||
parameter.Value.Name == "Prefer" {
continue
}
if parameter.Value.Schema.Value.Type == "string" ||
parameter.Value.Schema.Value.Type == "boolean" ||
parameter.Value.Schema.Value.Type == "integer" ||
parameter.Value.Schema.Value.Type == "array" {
flags[renameFlag(parameter.Value.Name)] = Parameter{
location: parameter.Value.In,
varType: "string",
orgName: parameter.Value.Name,
required: parameter.Value.Required,
value: new(string),
}
subFlag.StringVar(flags[renameFlag(parameter.Value.Name)].value, renameFlag(parameter.Value.Name), "", parameter.Value.Description)
continue
}
//TODO: If code reaches here, means support for new field-type is needed!
fmt.Println("IGNORE UNSUPPORTED FIELD. PLEASE LET THE MAINTAINER KNOW!", parameter.Value.Name, parameter.Value.Schema.Value.Type)
}
var requiredFlags map[string]bool
if method == "POST" || method == "PATCH" || method == "DELETE" {
if action.RequestBody != nil {
for contentType = range action.RequestBody.Value.Content {
}
//If no schema is defined for the body, no need to look for futher fields
if action.RequestBody.Value.Content[contentType].Schema != nil {
//Recursively collect all required fields
requiredFlags = collectRequired(action.RequestBody.Value.Content[contentType].Schema.Value)
var collectAttributes func(*openapi3.Schema, string, string)
//Function to support nested objects
collectAttributes = func(nested *openapi3.Schema, prefix string, inheritType string) {
//Collect all availble attributes for this command
for name, attribute := range nested.Properties {
//Ignore read-only attributes in body
if attribute.Value.ReadOnly {
continue
}
flagName := prefix + name
//Ignore ID-field that is redundant
if flagName == "data-id" && inheritType == "" {
continue
}
//Nested object, needs to drill down deeper
if attribute.Value.Type == "object" {
collectAttributes(attribute.Value, flagName+"-", "")
continue
}
//Arrays might include objects that needs to be drilled down deeper
if attribute.Value.Type == "array" && attribute.Value.Items.Value.Type == "object" {
collectAttributes(attribute.Value.Items.Value, flagName+"-", "array")
continue
}
required := false
if requiredFlags[flagName] {
required = true
}
//If flag is required and only one value is available, no need to offer it to the user
if required && attribute.Value.Enum != nil && len(attribute.Value.Enum) == 1 {
continue
}
flagName = shortenName(flagName)
flags[flagName] = Parameter{
location: "body",
required: required,
enum: attribute.Value.Enum,
value: new(string),
}
subFlag.StringVar(flags[flagName].value, flagName, "", attribute.Value.Description)
}
}
collectAttributes(action.RequestBody.Value.Content[contentType].Schema.Value, "", "")
}
}
}
//Find command possition in args
pos := 1
for index, arg := range os.Args {
if arg == command {
pos = index
break
}
}
//Validate all flags
subFlag.Parse(os.Args[pos+1:])
//If command has -account flag and no value set, use default account-ID
if flag, ok := flags["account"]; ok && *flag.value == "" {
*flag.value = ScalrAccount
}
//If command has -account-id flag and no value set, use default account-ID
if flag, ok := flags["account-id"]; ok && *flag.value == "" {
*flag.value = ScalrAccount
}
var missing []string
var missingBody []string
//Sort flag values to correct locations
for name, parameter := range flags {
//Ignore empty flags..
if *parameter.value == "" {
//..Unless required
if parameter.required {
if parameter.location == "query" || parameter.location == "path" {
missing = append(missing, name)
} else {
missingBody = append(missingBody, name)
}
}
continue
}
switch parameter.location {
case "query":
//This flag value should be sent as a query parameter
query.Add(parameter.orgName, *parameter.value)
case "path":
//This flag value goes in the URI
uri = strings.Replace(uri, "{"+parameter.orgName+"}", *parameter.value, 1)
}
}
var body string
if method == "POST" || method == "PATCH" || method == "DELETE" {
//If stdin contains data, use that as Body
stat, _ := os.Stdin.Stat()
if stat.Mode()&os.ModeNamedPipe != 0 ||
(stat.Mode()&os.ModeCharDevice == 0) && stat.Size() > 0 {
if len(missing) > 0 {
fmt.Printf("Missing required flag(s): %s\n", missing)
os.Exit(1)
}
var stdin []byte
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stdin = append(stdin, scanner.Bytes()...)
}
err := scanner.Err()
checkErr(err)
body = string(stdin)
}
if len(body) == 0 {
// FIXME: Disable required attributes for PATCH requests as the specs are incorrect
if method != "PATCH" {
if len(missingBody) > 0 || len(missing) > 0 {
fmt.Printf("Missing required flag(s): %s\n", append(missing, missingBody...))
os.Exit(1)
}
} else {
if len(missing) > 0 {
fmt.Printf("Missing required flag(s): %s\n", missing)
os.Exit(1)
}
}
if action.RequestBody != nil {
raw := gabs.New()
var collectAttributes func(*openapi3.Schema, string)
//Function to support nested objects
collectAttributes = func(nested *openapi3.Schema, prefix string) {
//Collect all availble attributes for this command
for name, attribute := range nested.Properties {
//Ignore read-only attributes in body
if attribute.Value.ReadOnly {
continue
}
path := prefix + name
//Nested object, needs to drill down deeper
if attribute.Value.Type == "object" {
collectAttributes(attribute.Value, path+".")
continue
}
//Special case for arrays of objects used in relationships
if attribute.Value.Type == "array" && attribute.Value.Items.Value.Type == "object" {
path = path + ".id"
attribute.Value.Type = "relationship"
}
flagName := strings.ReplaceAll(path, ".", "-")
required := false
if requiredFlags[flagName] {
required = true
}
//Special case to auto-add type in relationships if ID is set
if strings.HasPrefix(flagName, "data-relationships-") && name == "type" {
id := strings.Replace(shortenName(flagName), "-data-type", "-id", 1)
if *flags[id].value != "" {
required = true
} else {
required = false
}
}
//If required and only one value is available, use it
if required && attribute.Value.Enum != nil && len(attribute.Value.Enum) == 1 {
raw.SetP(attribute.Value.Enum[0], path)
continue
}
flagName = shortenName(flagName)
if _, ok := flags[flagName]; !ok {
continue
}
value := *flags[flagName].value
//Skip attribute if not set
if value == "" {
continue
}
theType := attribute.Value.Type
//If no type is specified, look deeper
if theType == "" && attribute.Value.AnyOf != nil {
for _, item := range attribute.Value.AnyOf {
if item.Value.Type != "" {
theType = item.Value.Type
break
}
}
}
switch theType {
case "relationship":
//Special case for arrays in relationships
for _, item := range strings.Split(value, ",") {
sub := gabs.New()
sub.Set(item, "id")
sub.Set(attribute.Value.Items.Value.Properties["type"].Value.Enum[0], "type")
raw.ArrayAppendP(sub.Data(), strings.TrimSuffix(path, ".id"))
}
case "boolean":
val, _ := strconv.ParseBool(value)
raw.SetP(val, path)
case "string":
raw.SetP(value, path)
case "integer":
val, _ := strconv.Atoi(value)
raw.SetP(val, path)
case "array":
raw.SetP(strings.Split(value, ","), path)
default:
//TODO: If code reaches here, means we need to add support for more field types!
fmt.Println("IGNORE UNSUPPORTED FIELD", name, attribute.Value.Type)
}
}
}
collectAttributes(action.RequestBody.Value.Content[contentType].Schema.Value, "")
body = raw.StringIndent("", " ")
}
}
} else {
if len(missing) > 0 {
fmt.Printf("Missing required flag(s): %s\n", missing)
os.Exit(1)
}
}
//Make request to the API
callAPI(method, uri, query, body, contentType, verbose, format)
return
}
}
//Command not found
fmt.Printf("\nCommand '%s' not found. Use -help to list available commands.\n\n", command)
os.Exit(1)
}
// Helper function to shorter flag-names for convenience
func shortenName(flagName string) string {
//If this is an attribute, strip prefix to shorten flag-names
flagName = strings.TrimPrefix(flagName, "data-attributes-")
//If this is a relationship, strip prefix and -data- to shorten flag-names
if strings.HasPrefix(flagName, "data-relationships-") {
flagName = strings.TrimPrefix(flagName, "data-relationships-")
flagName = strings.Replace(flagName, "-data-id", "-id", 1)
}
flagName = strings.TrimPrefix(flagName, "data-")
return flagName
}
// Make a request to the Scalr API
func callAPI(method string, uri string, query url.Values, body string, contentType string, verbose bool, format string) {
output := gabs.New()
output.Array()
query.Add("page[size]", "100")
for page := 1; true; page++ {
query.Set("page[number]", strconv.Itoa(page))
if verbose {
fmt.Println(method, "https://"+ScalrHostname+BasePath+uri+"?"+query.Encode())
if contentType != "" {
fmt.Println("Content-Type = " + contentType)
fmt.Println(body)
}
}
req, err := http.NewRequest(method, "https://"+ScalrHostname+BasePath+uri+"?"+query.Encode(), strings.NewReader(body))
checkErr(err)
req.Header.Set("User-Agent", "scalr-cli/"+versionCLI)
req.Header.Add("Authorization", "Bearer "+ScalrToken)
req.Header.Add("Prefer", "profile=preview")
if contentType != "" {
req.Header.Add("Content-Type", contentType)
}
res, err := http.DefaultClient.Do(req)
checkErr(err)
resBody, err := io.ReadAll(res.Body)
checkErr(err)
if verbose {
//Show raw server response
fmt.Println(string(resBody))
}
if res.StatusCode >= 300 {
showError(resBody)
}
//Empty response, quit early
if len(resBody) == 0 {
return
}
//If not a JSON:API response, just rend it raw
if res.Header.Get("content-type") != "application/vnd.api+json" {
fmt.Println(string(resBody))
return
}
//Check if paging is needed
response, err := gabs.ParseJSON(resBody)
checkErr(err)
//If data is empty, just send empty array
if len(response.Search("data").Children()) == 0 && len(response.Search("data").ChildrenMap()) == 0 {
break
}
arrayResponse := response.Exists("data", "0")
newItems := parseData(response)
//If this is a single item response, return it instead of an array
if !arrayResponse {
output = newItems.Search("0")
break
}
for _, data := range newItems.Children() {
output.ArrayAppend(data)
}
if response.Path("meta.pagination.next-page").Data() == nil {
break
}
}
//TODO: Add different outputs, such as YAML, CSV and TABLE
//formatJSON(resBody)
fmt.Println(output.StringIndent("", " "))
}
// Parse error response and show it to user
func showError(resBody []byte) {
jsonParsed, err := gabs.ParseJSON(resBody)
if err != nil {
fmt.Println("Server did not return a valid JSON response")
} else {
fmt.Println(jsonParsed.StringIndent("", " "))
}
os.Exit(1)
}
// Data JSON:API data to make it easier to work with
func parseData(response *gabs.Container) *gabs.Container {
output := gabs.New()
output.Array()
//Convert non-array to array if needed
if !response.Exists("data", "0") {
item := response.Path("data").Data()
response.Array("data")
response.ArrayAppend(item, "data")
}
included := gabs.New()
for _, include := range response.Path("included").Children() {
included.Set(include.Data(), include.Path("type").Data().(string)+"-"+include.Path("id").Data().(string))
}
for _, value := range response.Path("data").Children() {
sub := gabs.New()
sub.Set(value.Search("attributes").Data())
sub.SetP(value.Search("id"), "id")
sub.SetP(value.Search("type"), "type")
for name, relationship := range value.Search("relationships").ChildrenMap() {
if relationship.Data() == nil {
continue
}
//Function to support relationship arrays
//TODO: Should probably move this outside of the loop for performance reason, but will make code less readable
var connectRelationship = func(rel *gabs.Container) *gabs.Container {
relId := rel.Path("type").Data().(string) + "-" + rel.Path("id").Data().(string)
if included.Exists(relId) {
addition := gabs.New()
//Include attributes
addition.Set(included.Search(relId, "attributes").Data())
//Include ID and type
addition.Set(rel.Path("id"), "id")
addition.Set(rel.Path("type"), "type")
//Include sub-relationship IDs
//TODO: Does this need support for arrays in sub-relationships? Probably...
for subName, subRelationship := range included.Search(relId, "relationships").ChildrenMap() {
if subRelationship.Data() == nil {
continue
}
addition.Set(subRelationship.Path("data.id"), subName+"-id")
}
return addition
}
return rel
}
if !relationship.ExistsP("data.id") {
//Assume this is an array
sub.ArrayP(name)
for _, value := range relationship.Path("data").Children() {
sub.ArrayAppend(connectRelationship(value), name)
}
continue
}
sub.SetP(connectRelationship(relationship.Path("data")), name)
}
output.ArrayAppend(sub)
}
return output
}