diff --git a/cmd/config_show.go b/cmd/config_show.go index dfafbf4f..8ddff565 100644 --- a/cmd/config_show.go +++ b/cmd/config_show.go @@ -1,10 +1,8 @@ package cmd import ( + "encoding/json" "fmt" - "reflect" - "strconv" - "strings" "github.com/smira/commander" ) @@ -12,55 +10,19 @@ import ( func aptlyConfigShow(cmd *commander.Command, args []string) error { config := context.Config() + pretty_json, err := json.MarshalIndent(config, "", " ") - config_to_string := toString(reflect.ValueOf(config).Elem()) - - if config_to_string == "" { - return fmt.Errorf("Error processing configuration") + if err != nil { + return fmt.Errorf("unable to parse the config file: %s", err) } + config_to_string := string(pretty_json) + fmt.Println(config_to_string) return nil } -func toString(v reflect.Value) string { - - switch v.Kind() { - - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return strconv.FormatInt(v.Int(), 10) - case reflect.Bool: - return strconv.FormatBool(v.Bool()) - case reflect.Slice: - var str_slice []string - for i := 0; i < v.Len(); i++ { - 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()) - for i := 0; i < typ.NumField(); i++ { - str_slice = append(str_slice, typ.Field(i).Name+": "+toString(v.Field(i))) - } - 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, "- "+toString(key)+":\n"+toString(v.MapIndex(key))) - } - return strings.Join(str_slice, "\n") - case reflect.String: - return v.String() - default: - return "" - } - - return "" -} - func makeCmdConfigShow() *commander.Command { cmd := &commander.Command{ Run: aptlyConfigShow,