From 8efb7903b2d4c6630d22458c827e8b5a1175a0e0 Mon Sep 17 00:00:00 2001 From: Simon Aquino Date: Wed, 6 Aug 2014 00:19:55 +0000 Subject: [PATCH] Added map to to_string and tabs to config_show --- cmd/config_show.go | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cmd/config_show.go b/cmd/config_show.go index 92ca096a..370fcd6a 100644 --- a/cmd/config_show.go +++ b/cmd/config_show.go @@ -12,12 +12,13 @@ func aptlyConfigShow(cmd *commander.Command, args []string) error { config := context.Config() - fmt.Println(to_string(reflect.ValueOf(config).Elem())) - return err + fmt.Println(to_string(reflect.ValueOf(config).Elem(), 0)) + + return nil } -func to_string(v reflect.Value) string { +func to_string(v reflect.Value, tabs int) string { switch v.Kind() { @@ -28,19 +29,26 @@ func to_string(v reflect.Value) 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))) + str_slice = append(str_slice, to_string(v.Index(i), tabs)) } 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, typ.Field(i).Name+": "+to_string(v.Field(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, 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, make_tabs(tabs)+"}") return strings.Join(str_slice, "\n") - //case reflect.Map: - // var str_slice []string - case reflect.String: return v.String() } @@ -48,6 +56,14 @@ func to_string(v reflect.Value) string { } +func make_tabs(tabs int) string { + str := "" + for i := 0; i < tabs; i++ { + str += "\t" + } + return str +} + func makeCmdConfigShow() *commander.Command { cmd := &commander.Command{ Run: aptlyConfigShow,