Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ func pollAsyncJob(r *Request, jobID string) (map[string]interface{}, error) {
return nil, errors.New("async API job query timed out")

case <-ticker.C:
queryResult, queryError := NewAPIRequest(r, "queryAsyncJobResult", []string{"jobid=" + jobID}, false)
args := []string{"jobid=" + jobID}
if r.Args != nil {
for _, arg := range r.Args {
if strings.HasPrefix(strings.ToLower(arg), "filter=") {
args = append(args, arg)
break
}
}
}
queryResult, queryError := NewAPIRequest(r, "queryAsyncJobResult", args, false)
if queryError != nil {
return queryResult, queryError
}
Expand Down
64 changes: 44 additions & 20 deletions cmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,27 @@ func printText(response map[string]interface{}) {
format := "text"
for k, v := range response {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice {
fmt.Printf("%v:\n", k)
for idx, item := range v.([]interface{}) {
if idx > 0 {
fmt.Println("================================================================================")
}
row, isMap := item.(map[string]interface{})
if isMap {
for field, value := range row {
fmt.Printf("%s = %v\n", field, jsonify(value, format))
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
items, ok := getItemsFromValue(v)
if ok {
fmt.Printf("%v:\n", k)
for idx, item := range items {
if idx > 0 {
fmt.Println("================================================================================")
}
row, isMap := item.(map[string]interface{})
if isMap {
for field, value := range row {
fmt.Printf("%s = %v\n", field, jsonify(value, format))
}
} else {
fmt.Printf("%v\n", item)
}
} else {
fmt.Printf("%v\n", item)
}
return
}
} else {
fmt.Printf("%v = %v\n", k, jsonify(v, format))
}
fmt.Printf("%v = %v\n", k, jsonify(v, format))
}
}

Expand All @@ -92,8 +95,8 @@ func printTable(response map[string]interface{}, filter []string) {
table := tablewriter.NewWriter(os.Stdout)
for k, v := range response {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice {
items, ok := v.([]interface{})
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
items, ok := getItemsFromValue(v)
if !ok {
continue
}
Expand Down Expand Up @@ -134,7 +137,7 @@ func printColumn(response map[string]interface{}, filter []string) {
for _, v := range response {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
items, ok := v.([]interface{})
items, ok := getItemsFromValue(v)
if !ok {
continue
}
Expand Down Expand Up @@ -173,7 +176,7 @@ func printCsv(response map[string]interface{}, filter []string) {
for _, v := range response {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
items, ok := v.([]interface{})
items, ok := getItemsFromValue(v)
if !ok {
continue
}
Expand Down Expand Up @@ -206,22 +209,43 @@ func printCsv(response map[string]interface{}, filter []string) {
enc.Flush()
}

func getItemsFromValue(v interface{}) ([]interface{}, bool) {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice {
sliceItems, ok := v.([]interface{})
if !ok {
return nil, false
}
return sliceItems, true
} else if valueType.Kind() == reflect.Map {
mapItem, ok := v.(map[string]interface{})
if !ok {
return nil, false
}
return []interface{}{mapItem}, true
}
return nil, false
}

func filterResponse(response map[string]interface{}, filter []string, outputType string) map[string]interface{} {
if filter == nil || len(filter) == 0 {
config.Debug("Filtering response with filter:", filter, "and output type:", outputType)
if len(filter) == 0 {
return response
}
filteredResponse := make(map[string]interface{})
for k, v := range response {
valueType := reflect.TypeOf(v)
if valueType.Kind() == reflect.Slice || valueType.Kind() == reflect.Map {
items, ok := v.([]interface{})
items, ok := getItemsFromValue(v)
if !ok {
config.Debug("Skipping non-slice/map value for key:", k)
continue
}
var filteredRows []interface{}
for _, item := range items {
row, ok := item.(map[string]interface{})
if !ok || len(row) < 1 {
config.Debug("Skipping non-map item for key:", k)
continue
}
filteredRow := make(map[string]interface{})
Expand Down