This is an automated email from the ASF dual-hosted git repository.
rohit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cloudstack-cloudmonkey.git
The following commit(s) were added to refs/heads/master by this push:
new 9549b6e cmd: don't sort header when filter keys are specified
9549b6e is described below
commit 9549b6ebf972232aa5a151829366a9d4b2682073
Author: Rohit Yadav <[email protected]>
AuthorDate: Thu Jun 18 11:44:33 2020 +0530
cmd: don't sort header when filter keys are specified
Signed-off-by: Rohit Yadav <[email protected]>
---
cmd/api.go | 6 +++-
cmd/output.go | 100 ++++++++++++++++++++++++++++++++--------------------------
2 files changed, 60 insertions(+), 46 deletions(-)
diff --git a/cmd/api.go b/cmd/api.go
index 01ed2c3..022140a 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -88,7 +88,11 @@ func init() {
var filterKeys []string
for _, arg := range apiArgs {
if strings.HasPrefix(arg, "filter=") {
- filterKeys =
strings.Split(strings.Split(arg, "=")[1], ",")
+ for _, filterKey := range
strings.Split(strings.Split(arg, "=")[1], ",") {
+ if
len(strings.TrimSpace(filterKey)) > 0 {
+ filterKeys =
append(filterKeys, strings.TrimSpace(filterKey))
+ }
+ }
}
}
diff --git a/cmd/output.go b/cmd/output.go
index 91c434d..e3815e4 100644
--- a/cmd/output.go
+++ b/cmd/output.go
@@ -31,6 +31,9 @@ import (
)
func jsonify(value interface{}) string {
+ if value == nil {
+ return ""
+ }
if reflect.TypeOf(value).Kind() == reflect.Map ||
reflect.TypeOf(value).Kind() == reflect.Slice {
jsonStr, err := json.Marshal(value)
if err == nil {
@@ -47,7 +50,31 @@ func printJSON(response map[string]interface{}) {
enc.Encode(response)
}
-func printTable(response map[string]interface{}) {
+func printText(response map[string]interface{}) {
+ 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))
+ }
+ } else {
+ fmt.Printf("%v\n", item)
+ }
+ }
+ } else {
+ fmt.Printf("%v = %v\n", k, jsonify(v))
+ }
+ }
+}
+
+func printTable(response map[string]interface{}, filter []string) {
table := tablewriter.NewWriter(os.Stdout)
for k, v := range response {
valueType := reflect.TypeOf(v)
@@ -63,12 +90,15 @@ func printTable(response map[string]interface{}) {
if !ok || len(row) < 1 {
continue
}
-
if len(header) == 0 {
- for field := range row {
- header = append(header, field)
+ if len(filter) > 0 {
+ header = filter
+ } else {
+ for field := range row {
+ header = append(header,
field)
+ }
+ sort.Strings(header)
}
- sort.Strings(header)
table.SetHeader(header)
}
var rowArray []string
@@ -84,31 +114,7 @@ func printTable(response map[string]interface{}) {
table.Render()
}
-func printText(response map[string]interface{}) {
- 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))
- }
- } else {
- fmt.Printf("%v\n", item)
- }
- }
- } else {
- fmt.Printf("%v = %v\n", k, jsonify(v))
- }
- }
-}
-
-func printColumn(response map[string]interface{}) {
+func printColumn(response map[string]interface{}, filter []string) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ',
tabwriter.DiscardEmptyColumns)
for _, v := range response {
valueType := reflect.TypeOf(v)
@@ -125,10 +131,14 @@ func printColumn(response map[string]interface{}) {
}
if idx == 0 {
- for rk := range row {
- header = append(header,
strings.ToUpper(rk))
+ if len(filter) > 0 {
+ header = filter
+ } else {
+ for rk := range row {
+ header = append(header,
strings.ToUpper(rk))
+ }
+ sort.Strings(header)
}
- sort.Strings(header)
fmt.Fprintln(w, strings.Join(header,
"\t"))
}
var values []string
@@ -142,7 +152,7 @@ func printColumn(response map[string]interface{}) {
w.Flush()
}
-func printCsv(response map[string]interface{}) {
+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 {
@@ -158,10 +168,14 @@ func printCsv(response map[string]interface{}) {
}
if idx == 0 {
- for rk := range row {
- header = append(header, rk)
+ if len(filter) > 0 {
+ header = filter
+ } else {
+ for rk := range row {
+ header = append(header,
rk)
+ }
+ sort.Strings(header)
}
- sort.Strings(header)
fmt.Println(strings.Join(header, ","))
}
var values []string
@@ -170,7 +184,6 @@ func printCsv(response map[string]interface{}) {
}
fmt.Println(strings.Join(values, ","))
}
-
}
}
}
@@ -195,9 +208,6 @@ func filterResponse(response map[string]interface{}, filter
[]string, outputType
}
filteredRow := make(map[string]interface{})
for _, filterKey := range filter {
- if len(strings.TrimSpace(filterKey)) ==
0 {
- continue
- }
for field := range row {
if filterKey == field {
filteredRow[field] =
row[field]
@@ -226,14 +236,14 @@ func printResult(outputType string, response
map[string]interface{}, filter []st
switch outputType {
case config.JSON:
printJSON(response)
- case config.TABLE:
- printTable(response)
case config.TEXT:
printText(response)
case config.COLUMN:
- printColumn(response)
+ printColumn(response, filter)
case config.CSV:
- printCsv(response)
+ printCsv(response, filter)
+ case config.TABLE:
+ printTable(response, filter)
default:
fmt.Println("Invalid output type configured, please fix that!")
}