completely dump libvirtxml.Domain.Devices if empty

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-10-23 15:01:10 -05:00
parent b77eb0db4e
commit 19880a81f3
1 changed files with 69 additions and 4 deletions

73
xml.go
View File

@ -458,7 +458,7 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
var normalVideo bool = true
if domcfg.Devices.Videos != nil {
for _, v := range domcfg.Devices.Videos {
if (v.Model.Type == "qxl") && (v.Model.VRam == 65536) {
if (v.Model.Type == "qxl") && (v.Model.VRam == 65536) {
// standard qxl video
} else {
fmt.Printf("? Video: %+v\n", v)
@ -471,7 +471,13 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
domcfg.Devices.Videos = nil
}
// dumpLibvirtxmlDomain()
// dumpLibvirtxmlDomainNames()
if libvirtxmlDomainEmpty(*domcfg.Devices) {
fmt.Println("Domain Devices are empty")
domcfg.Devices = nil
} else {
fmt.Println("Domain Devices are not empty")
}
updatedXML, err := xml.MarshalIndent(domcfg, "", " ")
if err != nil {
@ -481,11 +487,11 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
// Print the updated XML to verify
fmt.Println(string(updatedXML))
os.Exit(-1)
// os.Exit(-1)
}
// dump out all the fields in libvirtxml.DomainDeviceList
func dumpLibvirtxmlDomain() {
func dumpLibvirtxmlDomainNames() {
var domain libvirtxml.Domain
t := reflect.TypeOf(domain)
@ -504,3 +510,62 @@ func dumpLibvirtxmlDomain() {
fmt.Println("DomainDeviceList:", field.Name)
}
}
// dump out all the fields in libvirtxml.DomainDeviceList
func libvirtxmlDomainEmpty(mydom libvirtxml.DomainDeviceList) bool {
var empty bool = true
// Get the reflection object of the variable
v := reflect.ValueOf(mydom)
// Ensure that we are working with a struct
if v.Kind() == reflect.Struct {
fmt.Println("Fields and values in libvirtxml.DomainDeviceList:")
// Loop through each field in the struct
for i := 0; i < v.NumField(); i++ {
// Get field name
field := v.Type().Field(i).Name
// Get field value
value := v.Field(i)
if !value.IsValid() {
fmt.Printf("Field: %s is nil or invalid\n", field)
continue
}
// Check if the field is a string, array, or slice
switch value.Kind() {
case reflect.String:
if value.String() != "" {
fmt.Printf("Field: %s is a String with value: %s\n", field, value.String())
empty = false
}
case reflect.Slice:
if value.Len() != 0 {
fmt.Printf("Field: %s is a Slice with length: %d\n", field, value.Len())
empty = false
}
case reflect.Array:
if value.Len() != 0 {
fmt.Printf("Field: %s is an Array with length: %d\n", field, value.Len())
empty = false
}
case reflect.Ptr:
if !value.IsValid() {
fmt.Println("Field ptr: value:", value)
fmt.Printf("Field ptr: %s is of type: %s\n", field, value.Kind())
empty = false
}
default:
fmt.Printf("Field: %s is of type: %s\n", field, value.Kind())
empty = false
}
// Print the field name and value
// fmt.Printf("Field: %s, Value: %v\n", field, value)
}
} else {
fmt.Println("Provided variable is not a struct.")
}
return empty
}