From 24927f9a29f480c6ce212dc07c8c1ba47aefef61 Mon Sep 17 00:00:00 2001 From: Simon Aquino Date: Mon, 13 Oct 2014 18:29:45 +0100 Subject: [PATCH] config_show should output pretty json This commit changes the output of aptly config show to be pretty json rather than YAML. --- cmd/config_show.go | 50 ++++++---------------------------------------- 1 file changed, 6 insertions(+), 44 deletions(-) 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,