is able to ignore 'standard' xml files

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

107
xml.go
View File

@ -194,6 +194,36 @@ func setRandomMacs(domcfg *libvirtxml.Domain) {
// there might be something interesting in a VM
// 'standard' here means what I think is standard
func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
// dump type
if domcfg.Type == "kvm" {
domcfg.Type = ""
} else {
fmt.Printf("type: %+v\n", domcfg.Type)
}
// dump normal OS settings
var standardOS bool = false
if domcfg.OS != nil {
if domcfg.OS.Type != nil {
// OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm}
t := domcfg.OS.Type
if t.Arch == "x86_64" || t.Machine == "pc-i440fx-5.2" {
standardOS = true
}
}
}
if standardOS {
domcfg.OS = nil
} else {
fmt.Printf("OS: %+v\n", domcfg.OS)
fmt.Printf("OS Type: %+v\n", domcfg.OS.Type)
}
// ignore XMLName and IOThreads probably
// skip is hard coded in isDomainEmpty() function
// fmt.Printf("XMLName: %+v\n", domcfg.XMLName)
// fmt.Printf("IOThreads: %+v\n", domcfg.IOThreads)
// dump all the clock stuff if it's standard
var normalclock bool = true
if domcfg.Clock.Offset != "utc" {
@ -472,13 +502,20 @@ func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
}
// dumpLibvirtxmlDomainNames()
if libvirtxmlDomainEmpty(*domcfg.Devices) {
fmt.Println("Domain Devices are empty")
if libvirtxmlDomainDevicesEmpty(*domcfg.Devices) {
// fmt.Println("Domain Devices are empty")
domcfg.Devices = nil
} else {
fmt.Println("Domain Devices are not empty")
}
if libvirtxmlDomainEmpty(*domcfg) {
// fmt.Println("Domain Devices are empty")
domcfg = nil
} else {
fmt.Println("Domain is not yet empty")
}
updatedXML, err := xml.MarshalIndent(domcfg, "", " ")
if err != nil {
fmt.Printf("Failed to marshal updated XML: %v\n", err)
@ -512,14 +549,14 @@ func dumpLibvirtxmlDomainNames() {
}
// dump out all the fields in libvirtxml.DomainDeviceList
func libvirtxmlDomainEmpty(mydom libvirtxml.DomainDeviceList) bool {
func libvirtxmlDomainDevicesEmpty(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:")
// fmt.Println("Fields and values in libvirtxml.DomainDeviceList:")
// Loop through each field in the struct
for i := 0; i < v.NumField(); i++ {
@ -569,3 +606,65 @@ func libvirtxmlDomainEmpty(mydom libvirtxml.DomainDeviceList) bool {
}
return empty
}
// dump out all the fields in libvirtxml.DomainDeviceList
func libvirtxmlDomainEmpty(mydom libvirtxml.Domain) 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
}
if (field == "IOThreads") || (field == "XMLName") {
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
}