You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ro...@apache.org on 2018/04/25 14:57:13 UTC

[cloudstack-cloudmonkey] 05/06: api: wip refactor output code, using output type, response and filter opts

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

commit 56221379a199e463c84856443cf32a27d4d35687
Author: Rohit Yadav <ro...@apache.org>
AuthorDate: Sun Apr 22 20:59:33 2018 +0530

    api: wip refactor output code, using output type, response and filter opts
    
    Signed-off-by: Rohit Yadav <ro...@apache.org>
---
 cmd/api.go | 105 ++++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 62 insertions(+), 43 deletions(-)

diff --git a/cmd/api.go b/cmd/api.go
index 7bb6f43..c13a2c8 100644
--- a/cmd/api.go
+++ b/cmd/api.go
@@ -21,13 +21,13 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"strings"
-
-	"cloudmonkey/config"
 	"github.com/olekukonko/tablewriter"
 	"os"
 	"reflect"
 	"sort"
+	"strings"
+
+	"cloudmonkey/config"
 )
 
 var apiCommand *Command
@@ -59,6 +59,59 @@ func printText(itemMap map[string]interface{}) {
 	}
 }
 
+func printResult(outputType string, response map[string]interface{}, filter []string) {
+	switch outputType {
+	case config.TABLE:
+		table := tablewriter.NewWriter(os.Stdout)
+		for k, v := range response {
+			valueType := reflect.TypeOf(v)
+			if valueType.Kind() == reflect.Slice {
+				items, ok := v.([]interface{})
+				if !ok {
+					continue
+				}
+				fmt.Printf("%s:\n", k)
+				var header []string
+				for _, item := range items {
+					row, ok := item.(map[string]interface{})
+					if !ok || len(row) < 1 {
+						continue
+					}
+
+					if len(header) == 0 {
+						for field, _ := range row {
+							if filter != nil && len(filter) > 0 {
+								for _, filterKey := range filter {
+									if filterKey == field {
+										header = append(header, field)
+									}
+								}
+								continue
+							}
+							header = append(header, field)
+						}
+						sort.Strings(header)
+						table.SetHeader(header)
+					}
+					var rowArray []string
+					for _, field := range header {
+						rowArray = append(rowArray, fmt.Sprintf("%v", row[field]))
+					}
+					table.Append(rowArray)
+				}
+			} else {
+				fmt.Printf("%s = %v\n", k, v)
+			}
+		}
+		table.Render()
+	case config.TEXT:
+		printText(response)
+	default:
+		jsonOutput, _ := json.MarshalIndent(response, "", "  ")
+		fmt.Println(string(jsonOutput))
+	}
+}
+
 func init() {
 	apiCommand = &Command{
 		Name: "api",
@@ -110,49 +163,15 @@ func init() {
 				return err
 			}
 
-			switch r.Config.Core.Output {
-			case config.TABLE:
-				table := tablewriter.NewWriter(os.Stdout)
-				for k, v := range response {
-					valueType := reflect.TypeOf(v)
-					if valueType.Kind() == reflect.Slice {
-						items, ok := v.([]interface{})
-						if !ok {
-							continue
-						}
-						fmt.Printf("%s:\n", k)
-						var header []string
-						for _, item := range items {
-							row, ok := item.(map[string]interface{})
-							if !ok || len(row) < 1 {
-								continue
-							}
-
-							if len(header) == 0 {
-								for field, _ := range row {
-									header = append(header, field)
-								}
-								sort.Strings(header)
-								table.SetHeader(header)
-							}
-							var rowArray []string
-							for _, field := range header {
-								rowArray = append(rowArray, fmt.Sprintf("%v", row[field]))
-							}
-							table.Append(rowArray)
-						}
-					} else {
-						fmt.Printf("%s = %v\n", k, v)
-					}
+			var filterKeys []string
+			for _, arg := range apiArgs {
+				if strings.HasPrefix(arg, "filter=") {
+					filterKeys = strings.Split(strings.Split(arg, "=")[1], ",")
 				}
-				table.Render()
-			case config.TEXT:
-				printText(response)
-			default:
-				jsonOutput, _ := json.MarshalIndent(response, "", "  ")
-				fmt.Println(string(jsonOutput))
 			}
 
+			printResult(r.Config.Core.Output, response, filterKeys)
+
 			return nil
 		},
 	}

-- 
To stop receiving notification emails like this one, please contact
rohit@apache.org.