Recursive handling for command line flags

Every struct in a NodeConf with `lopt:"foo" set is now added as a
command-line flag with the RecursiveCreateFlags call. For maps a struct
with the key UNDEF is added so that it can be parsed out.

As the flags for the command-line need variables which hold the values,
for every map an element map[UNDEF] is added.  When now calling the
internal add, these map element can be filtered out and replace by the
given name. (e.g., --netname)

* rewrote node/profile add for recursive functions
* rewrote node/profile set for recursive functions
* rewrote node/profile list for recursive functions

Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
Christian Goll
2023-08-11 10:31:29 -06:00
committed by Jonathon Anderson
parent c55c5a2ac4
commit 45539a0d1f
29 changed files with 1346 additions and 714 deletions

View File

@@ -2,7 +2,6 @@ package apinode
import (
"fmt"
"reflect"
"sort"
"strings"
@@ -77,100 +76,14 @@ func NodeList(nodeGet *wwapiv1.GetNodeList) (nodeList wwapiv1.NodeList, err erro
n.ContainerName.Print(),
n.SystemOverlay.Print()+"/"+n.RuntimeOverlay.Print()))
}
} else if nodeGet.Type == wwapiv1.GetNodeList_All {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", "NODE", "FIELD", "PROFILE", "VALUE"))
} else if nodeGet.Type == wwapiv1.GetNodeList_All || nodeGet.Type == wwapiv1.GetNodeList_FullAll {
for _, n := range node.FilterByName(nodes, nodeGet.Nodes) {
nType := reflect.TypeOf(n)
nVal := reflect.ValueOf(n)
nConfType := reflect.TypeOf(node.NodeConf{})
for i := 0; i < nType.NumField(); i++ {
var fieldName, fieldSource, fieldVal string
nConfField, ok := nConfType.FieldByName(nType.Field(i).Name)
if ok {
fieldName = nConfField.Tag.Get("lopt")
} else {
fieldName = nType.Field(i).Name
}
if nType.Field(i).Type == reflect.TypeOf(node.Entry{}) {
entr := nVal.Field(i).Interface().(node.Entry)
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.Entry{}) {
entrMap := nVal.Field(i).Interface().(map[string]*node.Entry)
for key, val := range entrMap {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), key, val.Source(), val.Print()))
}
} else if nType.Field(i).Type == reflect.TypeOf(map[string]*node.NetDevEntry{}) {
netDevs := nVal.Field(i).Interface().(map[string]*node.NetDevEntry)
for netName, netWork := range netDevs {
netInfoType := reflect.TypeOf(*netWork)
netInfoVal := reflect.ValueOf(*netWork)
netConfType := reflect.TypeOf(node.NetDevs{})
for j := 0; j < netInfoType.NumField(); j++ {
netConfField, ok := netConfType.FieldByName(netInfoType.Field(j).Name)
if ok {
if netConfField.Tag.Get("lopt") != "nettagadd" {
fieldName = netName + ":" + netConfField.Tag.Get("lopt")
} else {
fieldName = netName + ":tag"
}
} else {
fieldName = netName + ":" + netInfoType.Field(j).Name
}
if netInfoType.Field(j).Type == reflect.TypeOf(node.Entry{}) {
entr := netInfoVal.Field(j).Interface().(node.Entry)
fieldSource = entr.Source()
fieldVal = entr.Print()
// only print fields with lopt
if netConfField.Tag.Get("lopt") != "" {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
} else if netInfoType.Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range netInfoVal.Field(j).Interface().(map[string]*node.Entry) {
keyfieldName := fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), keyfieldName, fieldSource, fieldVal))
}
}
}
}
} else if nType.Field(i).Type.Kind() == reflect.Ptr {
nestInfoType := reflect.TypeOf(nVal.Field(i).Interface())
nestInfoVal := reflect.ValueOf(nVal.Field(i).Interface())
// nestConfType := reflect.TypeOf(nConfField.Type.Elem().FieldByName())
for j := 0; j < nestInfoType.Elem().NumField(); j++ {
nestConfField, ok := nConfField.Type.Elem().FieldByName(nestInfoType.Elem().Field(j).Name)
if ok {
fieldName = nestConfField.Tag.Get("lopt")
} else {
fieldName = nestInfoType.Elem().Field(j).Name
}
if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(node.Entry{}) {
entr := nestInfoVal.Elem().Field(j).Interface().(node.Entry)
fieldSource = entr.Source()
fieldVal = entr.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
} else if nestInfoType.Elem().Field(j).Type == reflect.TypeOf(map[string]*node.Entry{}) {
for key, val := range nestInfoVal.Elem().Field(j).Interface().(map[string]*node.Entry) {
fieldName = fieldName + ":" + key
fieldSource = val.Source()
fieldVal = val.Print()
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), fieldName, fieldSource, fieldVal))
}
}
}
}
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", "NODE", "FIELD", "PROFILE", "VALUE"))
fields := n.GetFields(wwapiv1.GetNodeList_FullAll == nodeGet.Type)
for _, f := range fields {
nodeList.Output = append(nodeList.Output,
fmt.Sprintf("%s:=:%s:=:%s:=:%s", n.Id.Print(), f.Field, f.Source, f.Value))
}
}
}