config_show should output pretty json

This commit changes the output of aptly config show to be pretty json
rather than YAML.
This commit is contained in:
Simon Aquino
2014-10-13 18:29:45 +01:00
parent ecbb9ad20c
commit 24927f9a29
+6 -44
View File
@@ -1,10 +1,8 @@
package cmd package cmd
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect"
"strconv"
"strings"
"github.com/smira/commander" "github.com/smira/commander"
) )
@@ -12,55 +10,19 @@ import (
func aptlyConfigShow(cmd *commander.Command, args []string) error { func aptlyConfigShow(cmd *commander.Command, args []string) error {
config := context.Config() config := context.Config()
pretty_json, err := json.MarshalIndent(config, "", " ")
config_to_string := toString(reflect.ValueOf(config).Elem()) if err != nil {
return fmt.Errorf("unable to parse the config file: %s", err)
if config_to_string == "" {
return fmt.Errorf("Error processing configuration")
} }
config_to_string := string(pretty_json)
fmt.Println(config_to_string) fmt.Println(config_to_string)
return nil 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 { func makeCmdConfigShow() *commander.Command {
cmd := &commander.Command{ cmd := &commander.Command{
Run: aptlyConfigShow, Run: aptlyConfigShow,