From 81e91898539f5bf0c789a6a3549f57eab3630f06 Mon Sep 17 00:00:00 2001 From: Simon Aquino Date: Wed, 6 Aug 2014 22:41:31 +0000 Subject: [PATCH] Config show now outputs a clean data structure --- cmd/config_show.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/cmd/config_show.go b/cmd/config_show.go index 370fcd6a..dfafbf4f 100644 --- a/cmd/config_show.go +++ b/cmd/config_show.go @@ -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 {