Config show now outputs a clean data structure

This commit is contained in:
Simon Aquino
2014-08-06 22:41:31 +00:00
parent 8efb7903b2
commit 81e9189853

View File

@@ -5,20 +5,26 @@ import (
"reflect"
"strconv"
"strings"
"github.com/smira/commander"
)
import "github.com/smira/commander"
func aptlyConfigShow(cmd *commander.Command, args []string) error {
config := context.Config()
fmt.Println(to_string(reflect.ValueOf(config).Elem(), 0))
config_to_string := toString(reflect.ValueOf(config).Elem())
if config_to_string == "" {
return fmt.Errorf("Error processing configuration")
}
fmt.Println(config_to_string)
return nil
}
func to_string(v reflect.Value, tabs int) string {
func toString(v reflect.Value) string {
switch v.Kind() {
@@ -29,39 +35,30 @@ func to_string(v reflect.Value, tabs int) string {
case reflect.Slice:
var str_slice []string
for i := 0; i < v.Len(); i++ {
str_slice = append(str_slice, to_string(v.Index(i), tabs))
str_slice = append(str_slice, toString(v.Index(i)))
}
return strings.Join(str_slice, ", ")
case reflect.Struct:
var str_slice []string
typ := reflect.TypeOf(v.Interface())
//str_slice = append(str_slice, make_tabs(tabs)+"{")
for i := 0; i < typ.NumField(); i++ {
str_slice = append(str_slice, make_tabs(tabs)+typ.Field(i).Name+": "+to_string(v.Field(i), tabs+1))
str_slice = append(str_slice, typ.Field(i).Name+": "+toString(v.Field(i)))
}
//str_slice = append(str_slice, make_tabs(tabs)+"}")
return strings.Join(str_slice, "\n")
case reflect.Map:
var str_slice []string
str_slice = append(str_slice, "")
for _, key := range v.MapKeys() {
str_slice = append(str_slice, make_tabs(tabs)+"- "+to_string(key, tabs)+":\n"+to_string(v.MapIndex(key), tabs+1))
str_slice = append(str_slice, "- "+toString(key)+":\n"+toString(v.MapIndex(key)))
}
//str_slice = append(str_slice, make_tabs(tabs)+"}")
return strings.Join(str_slice, "\n")
case reflect.String:
return v.String()
default:
return ""
}
return ""
}
func make_tabs(tabs int) string {
str := ""
for i := 0; i < tabs; i++ {
str += "\t"
}
return str
}
func makeCmdConfigShow() *commander.Command {