github.com/mmatczuk/gohan@v0.0.0-20170206152520-30e45d9bdb69/cli/client/output.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package client
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/cloudwan/gohan/schema"
    24  	"github.com/olekukonko/tablewriter"
    25  )
    26  
    27  var errorKey = "error"
    28  
    29  func (gohanClientCLI *GohanClientCLI) formatOutput(s *schema.Schema, rawResult interface{}) string {
    30  	if rawResult == nil {
    31  		return ""
    32  	}
    33  	switch gohanClientCLI.opts.outputFormat {
    34  	case outputFormatTable:
    35  		return gohanClientCLI.formatOutputTable(s, rawResult)
    36  	default:
    37  		result, _ := json.MarshalIndent(rawResult, "", "\t")
    38  		return fmt.Sprintf("%s", result)
    39  	}
    40  }
    41  
    42  func (gohanClientCLI *GohanClientCLI) formatOutputTable(s *schema.Schema, rawResult interface{}) string {
    43  	buffer := bytes.NewBufferString("")
    44  	for k, v := range rawResult.(map[string]interface{}) {
    45  		if k == errorKey {
    46  			return fmt.Sprintf("%v", v)
    47  		}
    48  		switch v.(type) {
    49  		case []interface{}:
    50  			gohanClientCLI.createResourcesTable(s, buffer, v.([]interface{}))
    51  		case map[string]interface{}:
    52  			gohanClientCLI.createSingleResourceTable(s, buffer, v.(map[string]interface{}))
    53  		default:
    54  			return fmt.Sprintf("%v", v)
    55  		}
    56  	}
    57  	return buffer.String()
    58  }
    59  
    60  func (gohanClientCLI *GohanClientCLI) createResourcesTable(s *schema.Schema, buffer *bytes.Buffer, resources []interface{}) {
    61  	table := tablewriter.NewWriter(buffer)
    62  	if len(resources) == 0 {
    63  		return
    64  	}
    65  	table.SetHeader(s.Titles())
    66  	for _, rawResource := range resources {
    67  		resourceSlice := []string{}
    68  		resource := rawResource.(map[string]interface{})
    69  		for _, property := range s.Properties {
    70  			v := ""
    71  			if val, ok := resource[property.ID]; ok && val != nil {
    72  				switch property.Type {
    73  				case "string":
    74  					v = fmt.Sprint(val)
    75  					if property.RelationProperty != "" {
    76  						relatedResource := resource[property.RelationProperty].(map[string]interface{})
    77  						v = relatedResource["name"].(string)
    78  					}
    79  				default:
    80  					v = fmt.Sprint(val)
    81  				}
    82  			}
    83  			resourceSlice = append(resourceSlice, v)
    84  		}
    85  		table.Append(resourceSlice)
    86  	}
    87  	table.Render()
    88  }
    89  
    90  func (gohanClientCLI *GohanClientCLI) createSingleResourceTable(s *schema.Schema, buffer *bytes.Buffer, resource map[string]interface{}) {
    91  	table := tablewriter.NewWriter(buffer)
    92  	table.SetHeader([]string{"Property", "Value"})
    93  	for _, property := range s.Properties {
    94  		table.Append([]string{property.Title, fmt.Sprint(resource[property.ID])})
    95  	}
    96  	table.Render()
    97  }