displayStructFields now recurse into nested structs
This commit is contained in:
parent
3fdca54cc4
commit
06900bfa9c
54
cmd/list.go
54
cmd/list.go
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"strings"
|
||||
"io/ioutil"
|
||||
"gocage/jail"
|
||||
"encoding/json"
|
||||
@ -14,6 +15,46 @@ import (
|
||||
|
||||
var gJails []Jail
|
||||
|
||||
|
||||
func getStructField(parentStruct interface{}, fieldName string) (reflect.Value, string) {
|
||||
v := reflect.ValueOf(parentStruct)
|
||||
|
||||
if v.Kind() == reflect.Ptr {
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
if false {
|
||||
for i := 0 ; i < v.NumField(); i++ {
|
||||
f := v.Field(i)
|
||||
if f.Kind() == reflect.String {
|
||||
fmt.Printf("%v=%v\n", v.Type().Field(i).Name, f.Interface())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(fieldName, ".") {
|
||||
fs := strings.Split(fieldName, ".")
|
||||
f := v.FieldByName(fs[0])
|
||||
if f.Kind() == reflect.Struct {
|
||||
return getStructField(f.Interface(), strings.Join(fs[1:], "."))
|
||||
} else {
|
||||
log.Fatalln(fmt.Sprintf("%s is not a struct: %s\n", fs[0], f.Kind().String()))
|
||||
}
|
||||
}
|
||||
|
||||
if false {
|
||||
if v.FieldByName(fieldName).IsValid() {
|
||||
log.Println(fmt.Sprintf("%s value: %s", fieldName, v.FieldByName(fieldName).Interface()))
|
||||
} else {
|
||||
log.Println(fmt.Sprintf("%s is invalid", fieldName))
|
||||
}
|
||||
}
|
||||
|
||||
return v, fieldName
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Pretty display of jails field
|
||||
Fields to show are given in a string array parameter
|
||||
Ex. : displayJails(["Name", "JID", "RootPath"])
|
||||
@ -46,14 +87,17 @@ func displayStructFields(jails []Jail, valsToDisplay []string) {
|
||||
|
||||
line := make([]Field, len(valsToDisplay))
|
||||
for i, f := range valsToDisplay {
|
||||
a := reflect.ValueOf(&tj).Elem()
|
||||
a, f := getStructField(tj, f)
|
||||
|
||||
field := Field {
|
||||
Name: f,
|
||||
}
|
||||
if a.Elem().FieldByName(f).IsValid() {
|
||||
if a.FieldByName(f).IsValid() {
|
||||
// For now this just contains this item length, will adjust later
|
||||
field.MaxLen = len(fmt.Sprintf("%v", a.Elem().FieldByName(f).Interface()))
|
||||
field.Value = fmt.Sprintf("%v", a.Elem().FieldByName(f).Interface())
|
||||
field.MaxLen = len(fmt.Sprintf("%v", a.FieldByName(f).Interface()))
|
||||
field.Value = fmt.Sprintf("%v", a.FieldByName(f).Interface())
|
||||
} else {
|
||||
fmt.Printf("Invalid field name: %s\n", f)
|
||||
}
|
||||
line[i] = field
|
||||
|
||||
@ -145,7 +189,7 @@ func listJails(args []string) {
|
||||
listJailsFromDatastore(d)
|
||||
}
|
||||
|
||||
displayStructFields(gJails, []string{"JID", "Name", "Release", "RootPath"})
|
||||
displayStructFields(gJails, []string{"JID", "Name", "Config.Release", "Config.Ip4_addr", "RootPath"})
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user